首页 > 解决方案 > Maven + jUnit4:根据测试名称包含测试

问题描述

我试图根据测试用例名称从我的测试类中运行一些选定的测试。

下面是我的测试课的大纲

----GlobalTests.java----

@Test
public void myTest_India(){ --- }

@Test
public void myTest_Germany() { --- }

----GlobalIntegrationTests.java----

@Test
public void myIntegrationTest_India(){ --- }

@Test
public void myIntegrationTest_Germany() { --- }

如何指示我的 Maven 只运行“印度”测试?

我在maven surefire插件中试过如下

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <includes>
                        <include>%regex[*Test_India*]</include>
                    </includes>                    
                </configuration>
            </plugin>

但似乎此配置用于选择测试类名称而不是测试名称。

提前致谢。

标签: mavenjunit4maven-surefire-plugin

解决方案


这可以通过使用 maven 的“测试”属性来完成 要运行的测试可以定义为 testClass#testName 下面的方法有效

<project>
    <properties>
        <test>*#*India</test>
    </properties>
</project>

并运行为

mvn test

或直接从命令行作为mvn -Dtest="*#*India"


推荐阅读