2009-12-09 100 views
394

我知道你可以使用运行在某一类的所有测试:运行单个测试方法与Maven

mvn test -Dtest=classname 

但我想运行一个单独的方法和-Dtest = classname.methodname似乎并不上班。

+3

我也很想知道如何做到这一点。但是,如果我发现自己经常这样做,我认为这个测试可能会被划分到自己的类中,所以你可以使用'mvn test -Dtest = classname'语法。 – 2009-12-09 13:57:03

+0

你想知道如何通过命令行? 或者使用IDE(eclipse)会对你有用吗? – 2009-12-09 14:04:37

+0

我在看命令行。我认为junit eclipse插件可以让你做到这一点。 – BillMan 2009-12-09 14:21:48

回答

519

要运行Maven的一个测试方法,您需要提供命令:

mvn -Dtest=TestCircle#xyz test 

其中TestCircle是测试班级名称和xyz是测试方法。

外卡字符也有效;无论是在方法名称和类名称。

如果您正在测试多模块项目,请指定测试所在的模块-pl <module-name>

为了能够集成测试使用it.test=...选项,而不是test=...

mvn -pl <module-name> -Dit.test=TestCircle#xyz integration-test 
+0

我希望有一种合并方式答案...这是正确的,但它采取的链接是在另一篇文章中:http://maven.apache.org/plugins/maven-surefire-plugin/examples/single-test.html – cjstehno 2011-06-28 11:54:01

+0

我想知道如果类名应该是完全合格的类名,包名,答案是,尽管完全限定名称的作用,也只有类名可用,但Maven会为您查找该类。我猜如果名字不明确,它会发出错误。 – stivlo 2011-11-04 02:57:14

+59

a总是得到错误:**没有测试被执行!** – 2011-11-21 10:12:33

-7

据我所知,surefire plugin没有提供任何方式来做到这一点。但随时打开issue :)

+4

-1,因为这是可能的,请参阅主要答案 – stivlo 2011-11-04 05:22:06

-5

您可以运行一个测试类,但不是测试类中的单个方法。您使用类的简单名称而不是类的完全限定名称。所以,如果你在“org.sonatype.test.MyTest”有一个测试,这是要运行的唯一的测试,你的命令行应该是这样的:

mvn test -Dtest=MyTest 
+0

我在单个模块项目中测试了先前评论中描述的解决方案,因此该评论所说的内容并非如此。 – tonio 2015-05-11 18:49:32

+1

原本回答时,2009年可能是真的。 – flurdy 2017-08-24 15:09:18

14

我与我的TestNG的做什么, (对不起,JUnit的不支持此)测试案例是我一个组分配给测试,我想运行

@Test(groups="broken") 

然后只需运行“MVN -Dgroups =破”。

+1

这是一个很好的答案。在我的情况下,测试试图运行,但一些Seam组件没有正确设置,因此它看起来像跳过了一部分安装代码。 – 2010-08-11 20:32:21

+1

正确。您需要将groups = broken放入您的@BeforeMethod中,或者执行@BeforeMethod(alwaysRun = true) – tunaranch 2010-08-12 05:36:27

+0

请让我知道什么是组,这是因为我正在编译问题。 – 2015-01-23 13:37:55

3

的JUnit的新版本中包含的类别亚军: http://kentbeck.github.com/junit/doc/ReleaseNotes4.8.html

但JUnit的释放过程不Maven的基础,所以Maven用户必须手动把它自己的存储库。

+1

不错。但我不明白这是如何回答这个问题的。 – 2009-12-10 14:33:49

+2

从maven-surefire-plugin的2.7.3版本支持Subj:http://maven.apache.org/plugins/maven-surefire-plugin/examples/single-test.html – 2011-04-04 00:36:08

3

通过tobrien提到的测试参数允许您使用方法名前指定的方法。这应该适用于JUnit和TestNG。我从来没有尝试过,只是读它的Surefire Plugin page

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

19

没有与神火2.12的问题。 这就是我在将Maven-surefire-plugin从2.12更改为2时发生的情况。11:

  1. MVN测试-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!

  2. MVN测试-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

+4

在2.12.1或更高版本中再次使用。 – yurez 2012-11-15 14:18:09

+0

固定“没有测试被执行!”通过升级到2.14 – alfonx 2013-03-17 22:41:53

12

运行一组在 单个测试类的方法使用2.7.3版本,您只能在单个测试类中运行n个测试。

注意:它支持junit 4.x和TestNG。

您必须使用以下语法

mvn -Dtest=TestCircle#mytest test 

可以使用的模式太

mvn -Dtest=TestCircle#test* test 

由于万无一失2.12.1的,你只能在这个时候选择多种方法(JUnit4X,补丁欢迎)

mvn -Dtest=TestCircle#testOne+testTwo test 

入住这link about single tests

+0

在我的情况下,我不得不双引号TestClass#测试名称,如mvn -Dtest =“TestCircle#myTest”test – 2014-06-27 23:12:58

+0

@PratikKhadloya任何想法如何工作的依赖测试?说,我在TestClass和test2中的test1(),test2(),test3()方法依赖于test1。我怎样才能运行test2()? – Ziska 2014-10-28 20:05:45

+1

我想如果你在测试中有依赖关系,那么你做错了什么。每个测试应独立于其他测试。您需要删除它们之间的耦合。 – 2014-10-29 18:41:25

2

该命令有效! mvn "-DTest=JoinTeamTestCases#validateJoinTeam" test请注意“-DTest”以大写'T'开头。