代码之家  ›  专栏  ›  技术社区  ›  Max

Maven没有发现ScalaTest单元测试

  •  2
  • Max  · 技术社区  · 7 年前

    我有以下单元测试:

    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成功运行(我复制了这两个项目之间的所有内容,这些内容看起来甚至有点遥远的联系)。

    2 回复  |  直到 7 年前
        1
  •  0
  •   Learner    7 年前

    您已放置 <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>
    

    在里面 包括 您必须提供测试文件的扩展名,并且可以更改pluggin的版本。

    谢谢

        2
  •  0
  •   Max    7 年前

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

    我写的源代码是用于单元测试的工具,所以我把它放在命名空间中 my.cool.package.testUtilities . 因此,单元测试处于 my.cool.package.testUtilities.test . 将源代码移出 testUtilities (仅限于 my.cool.package )将测试转移到 my.cool.package.test 允许发现测试。

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