тестирование Clojure в Maven - PullRequest
       31

тестирование Clojure в Maven

8 голосов
/ 04 апреля 2010

Я новичок в Maven и еще новее в Clojure. В качестве упражнения для изучения языка я пишу программу для игрока-паука. Я также планирую написать аналогичную программу на Scala для сравнения реализаций (см. Мой пост https://stackoverflow.com/questions/2571267/modern-java-alternatives-closed).

Я настроил структуру каталогов Maven, содержащую обычные каталоги src / main / clojure и src / test / clojure. Мой файл pom.xml содержит плагин clojure-maven-plugin. Когда я запускаю "mvn test", он отображает "Нет тестов для запуска", несмотря на то, что у меня есть тестовый код в каталоге src / test / clojure. Как я что-то неправильно называю? Вот мой файл pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>SpiderPlayer</groupId>
    <artifactId>SpiderPlayer</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <inceptionYear>2010</inceptionYear>

    <packaging>jar</packaging>

    <properties>
        <maven.build.timestamp.format>yyMMdd.HHmm</maven.build.timestamp.format>
        <main.dir>org/dogdaze/spider_player</main.dir>
        <main.package>org.dogdaze.spider_player</main.package>
        <main.class>${main.package}.Main</main.class>
    </properties>

    <build>
        <sourceDirectory>src/main/clojure</sourceDirectory>
        <testSourceDirectory>src/test/clojure</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>com.theoryinpractise</groupId>
                <artifactId>clojure-maven-plugin</artifactId>
                <version>1.3.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <tasks>
                                <echo file="${project.build.sourceDirectory}/${main.dir}/Version.clj"
                                      message="(ns ${main.package})${line.separator}"/>
                                <echo file="${project.build.sourceDirectory}/${main.dir}/Version.clj" append="true"
                                      message="(def version &quot;${maven.build.timestamp}&quot;)${line.separator}"/>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <phase>package</phase>
                        <configuration>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                            <archive>
                                <manifest>
                                    <mainClass>${main.class}</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <redirectTestOutputToFile>true</redirectTestOutputToFile>
                    <skipTests>false</skipTests>
                    <skip>false</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>surefire-it</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <skip>false</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.2</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>

Вот мой исходный файл Clojure (src / main / clojure / org / dogdaze / spider_player / Deck.clj):

; Copyright 2010 Dogdaze

(ns org.dogdaze.spider_player.Deck
  (:use
    [clojure.contrib.seq-utils
     :only (shuffle)]))

(def suits [:clubs :diamonds :hearts :spades])
(def ranks [:ace :two :three :four :five :six :seven :eight :nine :ten :jack :queen :king])

(defn suit-seq
  "Return 4 suits:
  if number-of-suits == 1: :clubs :clubs :clubs :clubs
  if number-of-suits == 2: :clubs :diamonds :clubs :diamonds
  if number-of-suits == 4: :clubs :diamonds :hearts :spades."
  [number-of-suits]
  (take 4 (cycle (take number-of-suits suits))))

(defstruct card :rank :suit)

(defn unshuffled-deck
  "Create an unshuffled deck containing all cards from the number of suits specified."
  [number-of-suits]
  (for
    [rank ranks suit (suit-seq number-of-suits)]
    (struct card rank suit)))

(defn deck
  "Create a shuffled deck containing all cards from the number of suits specified."
  [number-of-suits]
  (shuffle (unshuffled-deck number-of-suits)))

Вот мой тестовый пример (src / test / clojure / org / dogdaze / spider_player / TestDeck.clj):

; Copyright 2010 Dogdaze

(ns org.dogdaze.spider_player
  (:use
    clojure.set
    clojure.test
    org.dogdaze.spider_player.Deck))

(deftest test-suit-seq
  (is (= (suit-seq 1) [:clubs :clubs :clubs :clubs]))
  (is (= (suit-seq 2) [:clubs :diamonds :clubs :diamonds]))
  (is (= (suit-seq 4) [:clubs :diamonds :hearts :spades])))

(def one-suit-deck
  [{:rank :ace, :suit :clubs} {:rank :ace, :suit :clubs} {:rank :ace, :suit :clubs} {:rank :ace, :suit :clubs}
   {:rank :two, :suit :clubs} {:rank :two, :suit :clubs} {:rank :two, :suit :clubs} {:rank :two, :suit :clubs}
   {:rank :three, :suit :clubs} {:rank :three, :suit :clubs} {:rank :three, :suit :clubs} {:rank :three, :suit :clubs}
   {:rank :four, :suit :clubs} {:rank :four, :suit :clubs} {:rank :four, :suit :clubs} {:rank :four, :suit :clubs}
   {:rank :five, :suit :clubs} {:rank :five, :suit :clubs} {:rank :five, :suit :clubs} {:rank :five, :suit :clubs}
   {:rank :six, :suit :clubs} {:rank :six, :suit :clubs} {:rank :six, :suit :clubs} {:rank :six, :suit :clubs}
   {:rank :seven, :suit :clubs} {:rank :seven, :suit :clubs} {:rank :seven, :suit :clubs} {:rank :seven, :suit :clubs}
   {:rank :eight, :suit :clubs} {:rank :eight, :suit :clubs} {:rank :eight, :suit :clubs} {:rank :eight, :suit :clubs}
   {:rank :nine, :suit :clubs} {:rank :nine, :suit :clubs} {:rank :nine, :suit :clubs} {:rank :nine, :suit :clubs}
   {:rank :ten, :suit :clubs} {:rank :ten, :suit :clubs} {:rank :ten, :suit :clubs} {:rank :ten, :suit :clubs}
   {:rank :jack, :suit :clubs} {:rank :jack, :suit :clubs} {:rank :jack, :suit :clubs} {:rank :jack, :suit :clubs}
   {:rank :queen, :suit :clubs} {:rank :queen, :suit :clubs} {:rank :queen, :suit :clubs} {:rank :queen, :suit :clubs}
   {:rank :king, :suit :clubs} {:rank :king, :suit :clubs} {:rank :king, :suit :clubs} {:rank :king, :suit :clubs}])

(def two-suits-deck
  [{:rank :ace, :suit :clubs} {:rank :ace, :suit :diamonds} {:rank :ace, :suit :clubs} {:rank :ace, :suit :diamonds}
   {:rank :two, :suit :clubs} {:rank :two, :suit :diamonds} {:rank :two, :suit :clubs} {:rank :two, :suit :diamonds}
   {:rank :three, :suit :clubs} {:rank :three, :suit :diamonds} {:rank :three, :suit :clubs} {:rank :three, :suit :diamonds}
   {:rank :four, :suit :clubs} {:rank :four, :suit :diamonds} {:rank :four, :suit :clubs} {:rank :four, :suit :diamonds}
   {:rank :five, :suit :clubs} {:rank :five, :suit :diamonds} {:rank :five, :suit :clubs} {:rank :five, :suit :diamonds}
   {:rank :six, :suit :clubs} {:rank :six, :suit :diamonds} {:rank :six, :suit :clubs} {:rank :six, :suit :diamonds}
   {:rank :seven, :suit :clubs} {:rank :seven, :suit :diamonds} {:rank :seven, :suit :clubs} {:rank :seven, :suit :diamonds}
   {:rank :eight, :suit :clubs} {:rank :eight, :suit :diamonds} {:rank :eight, :suit :clubs} {:rank :eight, :suit :diamonds}
   {:rank :nine, :suit :clubs} {:rank :nine, :suit :diamonds} {:rank :nine, :suit :clubs} {:rank :nine, :suit :diamonds}
   {:rank :ten, :suit :clubs} {:rank :ten, :suit :diamonds} {:rank :ten, :suit :clubs} {:rank :ten, :suit :diamonds}
   {:rank :jack, :suit :clubs} {:rank :jack, :suit :diamonds} {:rank :jack, :suit :clubs} {:rank :jack, :suit :diamonds}
   {:rank :queen, :suit :clubs} {:rank :queen, :suit :diamonds} {:rank :queen, :suit :clubs} {:rank :queen, :suit :diamonds}
   {:rank :king, :suit :clubs} {:rank :king, :suit :diamonds} {:rank :king, :suit :clubs} {:rank :king, :suit :diamonds}])

(def four-suits-deck
  [{:rank :ace, :suit :clubs} {:rank :ace, :suit :diamonds} {:rank :ace, :suit :hearts} {:rank :ace, :suit :spades}
   {:rank :two, :suit :clubs} {:rank :two, :suit :diamonds} {:rank :two, :suit :hearts} {:rank :two, :suit :spades}
   {:rank :three, :suit :clubs} {:rank :three, :suit :diamonds} {:rank :three, :suit :hearts} {:rank :three, :suit :spades}
   {:rank :four, :suit :clubs} {:rank :four, :suit :diamonds} {:rank :four, :suit :hearts} {:rank :four, :suit :spades}
   {:rank :five, :suit :clubs} {:rank :five, :suit :diamonds} {:rank :five, :suit :hearts} {:rank :five, :suit :spades}
   {:rank :six, :suit :clubs} {:rank :six, :suit :diamonds} {:rank :six, :suit :hearts} {:rank :six, :suit :spades}
   {:rank :seven, :suit :clubs} {:rank :seven, :suit :diamonds} {:rank :seven, :suit :hearts} {:rank :seven, :suit :spades}
   {:rank :eight, :suit :clubs} {:rank :eight, :suit :diamonds} {:rank :eight, :suit :hearts} {:rank :eight, :suit :spades}
   {:rank :nine, :suit :clubs} {:rank :nine, :suit :diamonds} {:rank :nine, :suit :hearts} {:rank :nine, :suit :spades}
   {:rank :ten, :suit :clubs} {:rank :ten, :suit :diamonds} {:rank :ten, :suit :hearts} {:rank :ten, :suit :spades}
   {:rank :jack, :suit :clubs} {:rank :jack, :suit :diamonds} {:rank :jack, :suit :hearts} {:rank :jack, :suit :spades}
   {:rank :queen, :suit :clubs} {:rank :queen, :suit :diamonds} {:rank :queen, :suit :hearts} {:rank :queen, :suit :spades}
   {:rank :king, :suit :clubs} {:rank :king, :suit :diamonds} {:rank :king, :suit :hearts} {:rank :king, :suit :spades}])

(deftest test-unshuffled-deck
  (is (= (unshuffled-deck 1) one-suit-deck))
  (is (= (unshuffled-deck 2) two-suits-deck))
  (is (= (unshuffled-deck 4) four-suits-deck)))

(deftest test-shuffled-deck
  (is (= (set (deck 1)) (set one-suit-deck)))
  (is (= (set (deck 2)) (set two-suits-deck)))
  (is (= (set (deck 4)) (set four-suits-deck))))

(run-tests)

Есть идеи, почему тест не запущен? Кстати, не стесняйтесь предлагать улучшения для кода Clojure.

Спасибо, Ральф

Ответы [ 4 ]

7 голосов
/ 05 апреля 2010

Ключевой бит, который вам не хватает в вашем pom.xml (просто извлечение из clojure-contrib pom.xml ) - это выполнение под плагином clojure-maven-plugin:

<plugin>
    <groupId>com.theoryinpractise</groupId>
    <artifactId>clojure-maven-plugin</artifactId>
    <version>1.3.2</version>
    <!-- Current Config -->
    <executions>
        <!-- ... -->
        <execution>
            <id>test-clojure</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Также может потребоваться добавить что-то подобное в <build/>:

<testResources>
    <testResource>
      <directory>src/test/clojure</directory>
    </testResource>
</testResources>
2 голосов
/ 05 апреля 2010

Я бы не ожидал, что плагин surefire будет запускать что-то еще, кроме тестов Java, поэтому, с вашей текущей настройкой, действительно есть "Нет тестов для запуска". Я предлагаю использовать clojure-maven-plugin здесь. Также проверьте Почему использование Maven для сборок Clojure не составляет труда .

1 голос
/ 05 апреля 2010

Вы можете посмотреть этот пример файла pom.xml с помощью clojure-maven-plugin & tests. По умолчанию clojure-maven-plugin должен автоматически генерировать тестовый запуск, как описано в документации.

И лучше использовать последнюю версию clojure-maven-plugin - 1.3.2, в которой исправлено несколько ошибок

0 голосов
/ 04 апреля 2010

Возможно, вы хотите указать testSourceDirectory на src/test/clojure:

<sourceDirectory>src/main/clojure</sourceDirectory>
<testSourceDirectory>src/test/clojure</testSourceDirectory>
...