Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven surefire 2.12 does not run a specific test using -Dtest parameter

After upgrading all my Maven plugins version for a project, I encountered the following problem: when I run the basic command mvn test -Dtest=SomeTest, the build finishes with no test executed at all. In fact, I am not able to run any test using -Dtest parameter (of course the test exist, and is run when I simply execute mvn test).

After some searches, it appears that the problem is due to the usage of surefire 2.12 plugin. I've tested several versions of Maven (2.2.1 / 3.0.2) and JUnit (4.7.x, 4.8, 4.10, or even 3.8.x), but they have no impact on my issue.

So maybe my project has some specific configurations that may have an impact on that? Anyway, I created a new project, using mvn archetype:generate (using the basic org.apache.maven.archetypes:maven-archetype-quickstart).

I modified only 2 things in the pom.xml: using JUnit 4.10 (but it didn't change anything, I've tried with others versions), and defining the version of surefire:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>foo</groupId>
    <artifactId>bar</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.11</version>
            </plugin>
        </plugins>
    </build>
</project>

I run mvn test -Dtest=AppTest (the default JUnit test created by the archetype):

-------------------------------------------------------  T E S T S
------------------------------------------------------- Running foo.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time
elapsed: 0.031 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO]

Now, I modify the pom.xml to use version 2.12 for Surefire and run the command again:

-------------------------------------------------------  T E S T S

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO]
------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO]
------------------------------------------------------------------------ [INFO] Total time: 0.907s [INFO] Finished at: Fri Mar 02 10:37:12 CET
2012 [INFO] Final Memory: 3M/15M [INFO]
------------------------------------------------------------------------ [ERROR] Failed to execute goal
org.apache.maven.plugins:maven-surefire-plugin:2.12:test
(default-test) on project bar: No tests were executed!  (Set -D
failIfNoTests=false to ignore this error.) -> [Help 1]

The test is not run this time :(

As far as I am concerned, I think it's a regression, but that's quite surprising. Indeed, a JIRA defect is logged on Surefire 2.12 version, and in this description, they succeed in using -Dtest parameter.

Am I doing something wrong? Or is it a real regression (in this case, I will create the JIRA)?

Thanks.

like image 967
Romain Linsolas Avatar asked Mar 02 '12 09:03

Romain Linsolas


People also ask

How do I run only failed test cases in Maven?

During development, you may re-run failing tests because they are flaky. To use this feature through Maven surefire, set the rerunFailingTestsCount property to be a value larger than 0. Tests will be run until they pass or the number of reruns has been exhausted.

Why your JUnit 4 tests are not running under Maven?

Until JUnit 4, Maven will only run the test classes which are marked as public. We should note, though, that this won't be an issue with JUnit 5+. However, the actual test methods should always be marked as public. Here, we can notice that the test method is marked as private.

How do I exclude test cases in Maven?

To skip running the tests for a particular project, set the skipTests property to true. You can also skip the tests via the command line by executing the following command: mvn install -DskipTests.

How do I run a specific test in Maven?

The Maven surefire plugin provides a test parameter that we can use to specify test classes or methods we want to execute. If we want to execute a single test class, we can execute the command mvn test -Dtest=”TestClassName”. As the output shows, only the test class we've passed to the test parameter is executed.


1 Answers

It's a bug in 2.12 version - SUREFIRE-827.

like image 57
Andrew Logvinov Avatar answered Sep 20 '22 15:09

Andrew Logvinov