【问题标题】:Maven not discovering ScalaTest unit testsMaven 没有发现 ScalaTest 单元测试
【发布时间】:2018-04-26 08:31:54
【问题描述】:

我有以下单元测试:

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 成功运行了测试(我已经复制了我在两个项目之间可以找到的所有内容,这些项目看起来甚至是远程相关的)。

【问题讨论】:

    标签: scala maven scalatest scalatest-maven-plugin


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      您已放置&lt;skipTest&gt; true &lt;/skipTest&gt;,用于跳过测试用例。所以基本上你已经禁用了万能,它用于:

      在构建的测试阶段使用 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>
      

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

      谢谢

      【讨论】:

      • 此更改将要求对所有测试进行注释,而不是使用 scalatest-maven-plugin。我知道 maven 插件可以工作,因为另一个项目能够成功找到测试。
      • 在这种情况下,您必须检查所有设置,因为 pom 中应该有不同的设置,或者如果可能,您可以共享设置以便我们查看。
      • 我有我认为是帖子中pom.xml 的相关部分-您还想看其他部分吗?是否有其他文件可以包含一些相关信息?
      猜你喜欢
      • 2020-09-05
      • 2011-07-28
      • 2012-02-04
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 2019-04-29
      • 2011-12-05
      相关资源
      最近更新 更多