我知道您可以使用以下方法在某个类中运行所有测试:
mvn test -Dtest=classname
但我想运行一个单独的方法和-Dtest=classname。Methodname似乎不起作用。
我知道您可以使用以下方法在某个类中运行所有测试:
mvn test -Dtest=classname
但我想运行一个单独的方法和-Dtest=classname。Methodname似乎不起作用。
当前回答
surefire 2.12有一个问题。 这是我把maven-surefire-plugin从2.12修改到2.11的结果:
mvn test -Dtest=DesignRulesTest 结果: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project pmd: No tests were executed! mvn test -Dtest=DesignRulesTest 结果: [INFO]——maven-surefire-plugin:2.11:test (default-test) @ pmd—— ... 运行net.sourceforge.pmd.lang.java.rule.design.DesignRulesTest 测试运行:5,失败:0,错误:0,跳过:4,时间流逝:4.009秒
其他回答
您需要指定JUnit测试类及其要执行的方法。
mvn test -Dtest=com.mycompany.AppTest#testMethod
https://metamug.com/article/java/build-run-java-maven-project-command-line.html#running-unit-tests
tobrien提到的test参数允许您在方法名称前使用#来指定方法。这应该适用于JUnit和TestNG。我从来没有尝试过,只是在Surefire插件页面上阅读:
Specify this parameter to run individual tests by file name, overriding the includes/excludes parameters. Each pattern you specify here will be used to create an include pattern formatted like **/${test}.java, so you can just type "-Dtest=MyTest" to run a single test called "foo/MyTest.java". This parameter overrides the includes/excludes parameters, and the TestNG suiteXmlFiles parameter. since 2.7.3 You can execute a limited number of method in the test with adding #myMethod or #my*ethod. Si type "-Dtest=MyTest#myMethod" supported for junit 4.x and testNg
新版本的JUnit包含Categories运行器: http://kentbeck.github.com/junit/doc/ReleaseNotes4.8.html
但是JUnit的发布过程不是基于maven的,因此maven用户必须手动将其放入他们的存储库中。
在surefire插件2.22.1版本(可能更早),你可以在使用testng.xml时使用testnames属性运行单个测试
给定下面的test .xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="all-tests">
<classes>
<class name="server.Atest"/>
<class name="server.Btest"/>
<class name="server.Ctest"/>
</classes>
</test>
<test name="run-A-test">
<classes>
<class name="server.Atest"/>
</classes>
</test>
<test name="run-B-test">
<classes>
<class name="server.Btest"/>
</classes>
</test>
<test name="run-C-test">
<classes>
<class name="server.Ctest"/>
</classes>
</test>
</suite>
使用pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
[...]
<properties>
<selectedTests>all-tests</selectedTests>
</properties>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<suiteXmlFiles>
<file>src/test/resources/testng.xml</file>
</suiteXmlFiles>
<properties>
<property>
<name>testnames</name>
<value>${selectedTests}</value>
</property>
</properties>
</configuration>
</plugin>
</plugins>
[...]
</project>
从命令行
mvn clean test -DselectedTests=run-B-test
进一步阅读- Maven surefire插件使用testng
我尝试了这个线程中提供的几个解决方案,但它们不适用于依赖于不同的模块。在这种情况下,我必须使用额外的参数从根模块运行mvn: -am(——same -make),它告诉maven构建您的测试模块所依赖的模块,并且-DfailIfNoTests=false,否则会出现“没有测试被执行!”错误。
mvn test -pl B -Dtest=MyTestClass#myTest -am -DfailIfNoTests=false
root目录下的Pom.xml部分:
<modules>
<module>A</module>
<module>B</module>
<modules>
B取决于A。