Maven 没有发现 ScalaTest 单元测试

Maven not discovering ScalaTest unit tests

我有以下单元测试:

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。我还有另一个项目 运行ning 也成功地使用 3.0.1 进行了测试(我已经复制了我能在这两个项目之间找到的所有内容,这些内容似乎甚至是远程相关的)。

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

The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. It generates reports in two different file formats Plain text files (.txt) XML files (.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>

include 你必须提供你的测试文件的扩展名,你可以改变插件的版本。

谢谢

自然地,问题似乎源于一些我认为不相关的信息,所以我没有把它包括在我的原始 post.

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

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