当前位置: 首页 > 面试题库 >

在Maven中测试Clojure

袁鸿畴
2023-03-14
问题内容

我是Maven的新手,甚至是Clojure的新手。作为一种学习语言的练习,我正在编写蜘蛛纸牌播放器程序。

我已经配置了一个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代码进行改进。

谢谢拉尔夫


问题答案:

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>


 类似资料:
  • 以下测验提供与Maven相关的多项选择题(MCQ)。 您必须阅读所有给定的答案并单击正确的答案。 如果您不确定答案,则可以使用“ Show Answer按钮Show Answer 。 您可以使用“ Next Quiz按钮在Next Quiz中检查新的问题集。 问题1 - 关于POM,以下是什么? A - 它始终作为pom.xml驻留在项目的基本目录中。 B - 它包含有关项目的信息以及Maven用

  • 我已经使用selenium创建了测试用例,之前我可以使用maven(从命令行和eclipse)执行我的测试用例。但是现在它不起作用了。请帮助 请在下面找到控制台日志:(运行为- 我没有得到任何错误,它成功地构建了一个项目。但是跳过所有测试用例 请找到我的pom。xml配置如下: 请找到E2E。xml如下:

  • 我有一个多模块maven项目,在同一个文件夹(src/test/java)中包含集成和单元测试。集成测试标记为。我想以以下设置结束: 如果我运行,我希望所有测试都能编译,但我不想执行任何测试 重要的一点是,我希望在中配置它,而没有任何额外的命令行参数。 目前,我在我的父母pom中提出了以下设置。xml,其中唯一的问题是#1,其中执行所有测试: 所有子模块的pom中都有以下插件配置。xml,我认为应

  • 我试图用jenkins execution命令测试我的java项目,但是我得到了以下错误消息: 有关单个测试结果,请参阅/var/jenkins_home/workspace/test1/target/surefire-reports。请参阅转储文件(如果存在)[date].dump,[date]-jvmrun[N].dump和[date].dumpstream。分叉的VM在没有正确地说再见的情况

  • 由于一个我不知道的事件,调用在我的Ubuntu18.04中不再起作用。(自4月开始运行)。或I阶段尝试的任何其他生命周期都运行良好。 例如,如果我在克隆到中的Apache Commons IO的最新版本中运行,If显示 target/surefire-reports包含以下内容的许多重复 我尝试的任何其他项目都表现出类似的行为。 我尝试下载maven 3.5.4,解压缩它,通过将它添加到路径中,并

  • 问题内容: 当我使用以下命令在Maven中运行单个测试时: 我得到以下结果: 它在几分钟前工作了,但是现在由于某种原因停止了工作。在运行测试之前,我尝试运行几次,但没有帮助。 测试看起来像这样: }} 更新: 这是由于将此依赖项添加到POM: 当我删除它时,一切正常。即使我添加以下两个依赖关系而不是上一个依赖关系,一切也都可以正常工作: 真奇怪 问题答案: 您可能正在类路径上的某个地方拾取了JUn