Java 使用 maven 运行单个测试方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1873995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run a single test method with maven
提问by BillMan
I know you can run all the tests in a certain class using:
我知道您可以使用以下方法在某个类中运行所有测试:
mvn test -Dtest=classname
But I want to run an individual method and -Dtest=classname.methodname doesn't seem to work.
但是我想运行一个单独的方法并且 -Dtest=classname.methodname 似乎不起作用。
采纳答案by Mudit Srivastava
To run a single test method in Maven, you need to provide the command as:
要在 Maven 中运行单个测试方法,您需要提供以下命令:
mvn test -Dtest=TestCircle#xyz test
where TestCircle
is the test class name and xyz
is the test method.
其中TestCircle
是测试类名,xyz
是测试方法。
Wild card characters also work; both in the method name and class name.
通配符也可以使用;在方法名和类名中。
If you're testing in a multi-module project, specify the module that the test is in with -pl <module-name>
.
如果您在多模块项目中进行测试,请使用-pl <module-name>
.
For integration tests use it.test=...
option instead of test=...
:
对于集成测试,请使用it.test=...
选项而不是test=...
:
mvn -pl <module-name> -Dit.test=TestCircle#xyz integration-test
回答by Pascal Thivent
To my knowledge, the surefire plugindoesn't provide any way to do this. But feel free to open an issue:)
据我所知,surefire 插件没有提供任何方法来做到这一点。但请随意打开一个问题:)
回答by Tim O'Brien
You can run a single test class, but not a single method within a test class. You use the simple name of the class not the fully-qualified name of the class. So, if you have a test in "org.sonatype.test.MyTest" and that is the only test you want to run, your command line would look like this:
您可以运行单个测试类,但不能运行测试类中的单个方法。您使用类的简单名称而不是类的完全限定名称。因此,如果您在“org.sonatype.test.MyTest”中有一个测试并且这是您想要运行的唯一测试,您的命令行将如下所示:
mvn test -Dtest=MyTest
回答by tunaranch
What I do with my TestNG, (sorry, JUnit doesn't support this) test cases is I can assign a group to the test I want to run
我用我的 TestNG 做的(对不起,JUnit 不支持这个)测试用例是我可以为我想要运行的测试分配一个组
@Test(groups="broken")
And then simply run 'mvn -Dgroups=broken'.
然后只需运行'mvn -Dgroups=broken'。
回答by Andriy Plokhotnyuk
New versions of JUnit contains the Categories runner: http://kentbeck.github.com/junit/doc/ReleaseNotes4.8.html
JUnit 的新版本包含类别运行程序:http: //kentbeck.github.com/junit/doc/ReleaseNotes4.8.html
But releasing procedure of JUnit is not maven based, so maven users have to put it manually to their repositories.
但是 JUnit 的发布过程不是基于 maven 的,因此 maven 用户必须手动将其放入他们的存储库。
回答by Wesley Hartford
The testparameter mentioned by tobrien allows you to specify a method using a #before the method name. This should work for JUnit and TestNG. I've never tried it, just read it on the Surefire Plugin page:
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
指定此参数以按文件名运行单个测试,覆盖包含/排除参数。您在此处指定的每个模式都将用于创建格式为 **/${test}.java 的包含模式,因此您只需键入“-Dtest=MyTest”即可运行名为“foo/MyTest.java”的单个测试。此参数覆盖包括/排除参数和 TestNG suiteXmlFiles 参数。从 2.7.3 开始,您可以通过添加 #myMethod 或 #my*ethod 在测试中执行有限数量的方法。junit 4.x 和 testNg 支持 Si 类型“-Dtest=MyTest#myMethod”
回答by Duccio Fabbri
There is an issue with surefire 2.12. This is what happen to me changing maven-surefire-plugin from 2.12 to 2.11:
万无一失 2.12 存在问题。这就是我将 maven-surefire-plugin 从 2.12 更改为 2.11 的情况:
mvn test -Dtest=DesignRulesTest
Result:
[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
Result: [INFO] --- maven-surefire-plugin:2.11:test (default-test) @ pmd --- ... Running net.sourceforge.pmd.lang.java.rule.design.DesignRulesTest Tests run: 5, Failures: 0, Errors: 0, Skipped: 4, Time elapsed: 4.009 sec
mvn test -Dtest=DesignRulesTest
结果:
[错误] 无法执行目标 org.apache.maven.plugins:maven-surefire-plugin: 2.12:test (default-test) on project pmd: 未执行任何测试!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 秒
回答by Prasanth Kumar Diddi
Running a set of methods in a Single Test Class With version 2.7.3, you can run only n tests in a single Test Class.
在单个测试类中运行一组方法 在版本 2.7.3 中,您只能在单个测试类中运行 n 个测试。
NOTE : it's supported for junit 4.x and TestNG.
注意:junit 4.x 和 TestNG 支持它。
You must use the following syntax
您必须使用以下语法
mvn -Dtest=TestCircle#mytest test
You can use patterns too
你也可以使用模式
mvn -Dtest=TestCircle#test* test
As of surefire 2.12.1, you can select multiple methods (JUnit4X only at this time, patches welcome)
从surefire 2.12.1开始,你可以选择多种方法(此时仅JUnit4X,欢迎补丁)
mvn -Dtest=TestCircle#testOne+testTwo test
Check this link about single tests
检查此链接有关单项测试
回答by vikas
This command works !!
mvn "-DTest=JoinTeamTestCases#validateJoinTeam" test
Note that "-DTest" starts with UPPER CASE 'T'.
这个命令有效!!
mvn "-DTest=JoinTeamTestCases#validateJoinTeam" test
请注意,“-DTest”以大写字母“T”开头。
回答by Nazmul Hoque Shafin
You can run specific test class(es) and method(s) using the following syntax:
您可以使用以下语法运行特定的测试类和方法:
full package : mvn test -Dtest="com.oracle.tests.**"
all method in a class : mvn test -Dtest=CLASS_NAME1
single method from single class :mvn test -Dtest=CLASS_NAME1#METHOD_NAME1
multiple method from multiple class : mvn test -Dtest=CLASS_NAME1#METHOD_NAME1,CLASS_NAME2#METHOD_NAME2
完整包:mvn test -Dtest="com.oracle.tests.**"
类中的所有方法:mvn test -Dtest=CLASS_NAME1
来自单个类的单个方法:mvn test -Dtest=CLASS_NAME1#METHOD_NAME1
来自多个类的多个方法:mvn test -Dtest=CLASS_NAME1#METHOD_NAME1,CLASS_NAME2#METHOD_NAME2