Running a JUnit4 Test Suite in Maven using maven-failsafe-plugin

 ̄綄美尐妖づ 提交于 2019-12-21 01:57:31

问题


I've got a JUnit 4 test suite that contains a number of test classes in the order they need to be run (our Integration tests need to be run in a certain order).

If I use the maven-failsafe-plugin without any configuration it will run the test but not in the correct order. However, If I set the plugin to run the test suite no tests are run.

Is it possible to run a test suite using the failsafe plugin? if so, where have I gone wrong!!

The code is below:

@RunWith(Suite.class)
@SuiteClasses({ 
    TestCase1.class,
    TestCase2.class, 
       ...
    TestCaseN.class,
})
public class IntegrationSuite {
    //Do Nothing.
}

and from pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.9</version>
    <configuration>
        <includes>
            <include>IntegrationSuite.java</include>
        </includes>
    </configuration>
    <executions>
        <execution>
            <id>integration-test</id>
            <goals>
                <goal>integration-test</goal>
            </goals>
    </execution>
    <execution>
        <id>verify</id>
        <goals>
            <goal>verify</goal>
        </goals>
    </execution>
</executions>
</plugin>

Thanks :)


回答1:


Failsafe plugin supports runOrder (click) parameter since version 2.7 (quite recent). There aren't many options, you cannot specify the order explicitly, but you can set it to "alphabetical" and rename your test classes to reflect the run order.

May I also say on the occasion that the fact that test depend on each other is (test) code smell; it's not good, as it is a short path to developing an unmaintainable set of tests and abandoning it finally when its complexity skyrockets above human comprehension. Plus it may fail to expose bugs, as it is a result of one chosen execution path.

BTW, I prefer to include tests like this, with a double asterisk:

<includes>
     <include>**/IntegrationSuite.java</include>
</includes>



回答2:


maven-surefire-plugin can also be used as below code:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<includes>
<include>**/IntegrationSuite.java</include>
</includes>
</configuration>
</plugin>     


来源:https://stackoverflow.com/questions/7792886/running-a-junit4-test-suite-in-maven-using-maven-failsafe-plugin

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