2

我有以下单元测试:

import org.scalatest.FunSpec
import org.scalatest.Matchers._

class MyClassSpec extends FunSpec {

  describe("MyClass"){
    describe("Scenario 1"){
      it("Condition 1") {
        true shouldEqual false
      }

      it("Condition 2"){
        true shouldEqual false
      }
    }
  }

}

当我运行 maven 测试时,它编译得很好,但没有找到测试。这是输出:

[INFO] --- maven-surefire-plugin:2.7:test (default-test) @ project-name ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- scalatest-maven-plugin:1.0:test (test) @ project-name ---
Discovery starting.
Discovery completed in 202 milliseconds.
Run starting. Expected test count is: 0
DiscoverySuite:
Run completed in 236 milliseconds.
Total number of tests run: 0
Suites: completed 1, aborted 0
Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0
No tests were executed.

如输出所示,我正在使用 maven 的 scalatest 插件。这是我的 pom.xml 的相关部分:

<!-- disable surefire -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.7</version>
    <configuration>
        <skipTests>true</skipTests>
    </configuration>
</plugin>
<!-- enable scalatest -->
<plugin>
    <groupId>org.scalatest</groupId>
    <artifactId>scalatest-maven-plugin</artifactId>
    <version>1.0</version>
    <configuration>
        <reportsDirectory>${project.build.directory}/scalatest-reports</reportsDirectory>
        <junitxml>junit</junitxml>
        <filereports>report.txt</filereports>
    </configuration>
    <executions>
        <execution>
            <id>test</id>
            <goals>
                <goal>test</goal>
            </goals>
        </execution>
    </executions>
</plugin>

我对设置这些东西很陌生,所以我不确定是否还有其他我没有检查的东西。如果我运行 maven clean 然后运行 ​​maven 测试,我会得到完全相同的结果。我正在使用 ScalaTest 3.0.1 版。我有另一个项目也使用 3.0.1 成功运行了测试(我已经复制了我在两个项目之间可以找到的所有内容,这些项目看起来甚至是远程相关的)。

4

2 回答 2

0

您放置<skipTest> true </skipTest>了用于跳过测试用例的位置。所以基本上你已经禁用了万能,它用于:

Surefire 插件在构建生命周期的测试阶段用于执行应用程序的单元测试。它以两种不同的文件格式生成报告 纯文本文件 (.txt) XML 文件 (.xml)

更新你的pom:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
       <useFile>false</useFile>
       <disableXmlReport>true</disableXmlReport>
       <!-- If you have classpath issue like NoDefClassError,...-->
       <!-- useManifestOnlyJar>false</useManifestOnlyJar -->
       <includes>
           <include>**/*Test.*</include>
            <include>**/*Suite.*</include>
            <include>**/*Spec.*</include>
       </includes>
     </configuration>
</plugin>

包含中,您必须提供测试文件的扩展名,并且您可以更改插件的版本。

谢谢

于 2017-11-13T18:20:45.157 回答
0

自然,问题似乎源于一些我认为不相关的信息,所以我没有将它包含在我的原始帖子中。

我写的源代码是一个单元测试的工具,所以我把它放在了命名空间中my.cool.package.testUtilities。因此,单元测试在my.cool.package.testUtilities.test. 将源代码移出testUtilities(to just my.cool.package) 并将测试移至 justmy.cool.package.test允许发现测试。

作为 java 生态系统的新手(以前在 .NET 中的经验),我不确定这是否是测试运行器的一些奇怪的错误、预期的行为,或者将文件移动到新命名空间的行为是否做了其他事情,以及命名空间本身是无关紧要的。所以我不确定我从中学到了什么。

于 2017-11-16T13:40:59.330 回答