Maven not discovering ScalaTest unit tests

岁酱吖の 提交于 2019-12-11 00:34:41

问题


I have the following 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
      }
    }
  }

}

When I run maven test, this compiles fine but the tests are not found. Here is the output:

[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.

As the output shows, I'm using the scalatest plugin for maven. Here is the relevant section of my 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>

I'm pretty new to getting this stuff set up, so I'm not sure if there is some other thing I'm not checking. I get the exact same results if I run maven clean and then maven test. I'm using ScalaTest version 3.0.1. I have another project with tests running successfully also using 3.0.1 (I've copied everything I can find between the two projects that seems even remotely related).


回答1:


You have placed <skipTest> true </skipTest> which is used for skipping the test cases. So basically you have disabled the surefire and it is used for:

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)

Update your pom with:

<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>

In include you have to provide the extension of your test file and you can change the version of pluggin.

Thanks




回答2:


Naturally, the problem seems to have stemmed from some information I didn't think was relevant, so I didn't include it my original post.

The source code I wrote was a tool for unit testing, so I put it in the namespace my.cool.package.testUtilities. The unit tests were thus in my.cool.package.testUtilities.test. Moving the source code out of testUtilities (to just my.cool.package) and moving the tests to just my.cool.package.test allowed the tests to be discovered.

Being new to the java ecosystem (previous experience in .NET), I'm not sure if this is some weird bug with the test runner, expected behavior, or if the act of moving the files to new namespaces did something else, and the namespaces themselves are irrelevant. So I'm not sure what I learned from this.



来源:https://stackoverflow.com/questions/47268060/maven-not-discovering-scalatest-unit-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!