Run a single test method with maven
Asked Answered
H

14

710

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.

Hepplewhite answered 9/12, 2009 at 13:44 Comment(4)
I would be interested in knowing how to do this, too. However, if I found myself doing it quite often, I think that test might be a candidate to be split out into its own class, so you can use the mvn test -Dtest=classname syntax.Carolecarolee
Do you want to know how to do it via command line only?? Or using an IDE (eclipse) would work for you?Gildus
I was looking at a command line. I think the junit eclipse plugin will allow you to do this.Hepplewhite
I did this for Maven 1. As I recalled, it involved making changes to JUnit, which is responsible for introspecting the test class.Charlatanism
A
942

To run a single test method in Maven, you need to provide the command as:

mvn test -Dtest=TestCircle#xyz test

where TestCircle is the test class name and xyz is the test method.

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>.

For integration tests use it.test=... option instead of test=...:

mvn -pl <module-name> -Dit.test=TestCircle#xyz integration-test
Alkalinity answered 13/5, 2011 at 6:53 Comment(16)
I wish there was a way to merge answers... this is correct, but the link its taken from is in another post: maven.apache.org/plugins/maven-surefire-plugin/examples/…Crowson
I was wondering if the class name should be a fully qualified class name, with the package name, and the answer is, that while a fully qualified name works, also only the class name works, Maven looks the class up for you. I guess that if the name is ambiguous, it will emit an error.Suwannee
Fixed the "No Tests Were Executed" error by downgrading to surefire 2.9Tyler
So wait, the "solution" is to revert to an old version? What about those of us who actually require more recent versions of surefire?Endeavor
I fixed "No Tests Were Executed!" by upgrading to 2.14:Marivaux
If you're testing in a multi-module project, you also need to specify the module that the test is in with -pl <module-name>.Jungle
I had the same problem for testng with surefire 2.9, but version 2.14 and current last 2.17 work.Roam
Use -DfailIfNoTests=false to skip projects without test. No Tests Were Executed happens when you try to run test from root project and there is modules without tests at all.Dispend
I got No Tests Were Executed ! too, because i have jUnit and TestNg together in maven project dependencies. Removing TestNg or changing @Test annotation to @org.testng.annotations.Test solved problem for me.Skald
When running integration tests the phase has to be "integration-test" instead of "test".Briannebriano
This command works !! mvn "-DTest=JoinTeamTestCases#validateJoinTeam" test Note that "-DTest" starts with UPPER CASE 'T'.Fazio
I just spent 45min with no success until I realized I was missing the "Test" at the end of my test class name.Lightner
Dont forget the : in front of the module name ... mvn -pl :<module-name> -Dtest=TestCircle#xyz testCanaveral
For the first example, I found the test hanging off the end of the command to be unnecessaryHurling
What is the last test word being used for?Matejka
that is the goal maven needs to executeEmbree
S
36

There is an issue with surefire 2.12. This is what happen to me changing maven-surefire-plugin from 2.12 to 2.11:

  1. 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!

  2. 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

Stent answered 14/10, 2012 at 22:10 Comment(3)
Works again in 2.12.1 or later.Randeerandel
Fixed "No Tests Were Executed!" by upgrading to 2.14Marivaux
If you use @Category(IntegrationTest.class), use -Dit.test=ClassName, otherwise you will get No tests were executed! even with the latest plugin versions.Giselagiselbert
A
27

Run a single test method from a test class.

mvn test -Dtest=Test1#methodname


Other related use-cases

  • mvn test // Run all the unit test classes

  • mvn test -Dtest=Test1 // Run a single test class

  • mvn test -Dtest=Test1,Test2 // Run multiple test classes

  • mvn test -Dtest=Test1#testFoo* // Run all test methods that match pattern 'testFoo*' from a test class.

  • mvn test -Dtest=Test1#testFoo*+testBar* // Run all test methods match pattern 'testFoo*' and 'testBar*' from a test class.

Ancestor answered 20/7, 2020 at 16:12 Comment(1)
superb solutionFlotilla
C
18

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

@Test(groups="broken")

And then simply run 'mvn -Dgroups=broken'.

Controversy answered 9/12, 2009 at 23:13 Comment(3)
This is a good answer. In my case, the test tried to run but some Seam components weren't set up properly so it looks like this is skipping some portion of the setup code as well.Observe
Correct. You need to either put groups=broken in in your @BeforeMethod, or do @BeforeMethod(alwaysRun=true)Controversy
Please let me know what is group here, since I am getting compilation issue.Selinski
E
17

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.

NOTE : it's supported for junit 4.x and 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)

mvn -Dtest=TestCircle#testOne+testTwo test

Check this link about single tests

Ephemeron answered 28/2, 2013 at 14:22 Comment(3)
In my case i had to double quote the TestClass#test name like mvn -Dtest="TestCircle#myTest" testCrinkly
@PratikKhadloya Any idea how it works for dependent tests? Say, I have test1(), test2(), test3() methods in TestClass and test2 depends on test1. How can I run just test2()?Adenoid
I think if you have dependencies amongst your tests, you are doing something wrong. Each test should be independent of other tests. You need to remove the coupling between them.Crinkly
F
15

This command works !! mvn "-DTest=JoinTeamTestCases#validateJoinTeam" test Note that "-DTest" starts with UPPER CASE 'T'.

Fazio answered 7/8, 2017 at 11:14 Comment(2)
Does not work with SureFire 2.18.x; it just runs all tests.Afterburning
This made a difference to me when using Java17Preoccupied
P
12

You can run specific test class(es) and method(s) using the following syntax:

  1. full package : mvn test -Dtest="com.oracle.tests.**"

  2. all method in a class : mvn test -Dtest=CLASS_NAME1

  3. single method from single class :mvn test -Dtest=CLASS_NAME1#METHOD_NAME1

  4. multiple method from multiple class : mvn test -Dtest=CLASS_NAME1#METHOD_NAME1,CLASS_NAME2#METHOD_NAME2

Pigment answered 2/7, 2018 at 3:10 Comment(0)
D
10

First you need to clean your maven project

mvn clean

then you can run specific file and function using

mvn test -Dtest=testClassName#testCaseName

Durmast answered 23/11, 2021 at 18:59 Comment(0)
H
9

I tried several solutions provided in this thread, however they were not working for module which depends on a different one. In that case I had to run mvn from the root-module with additional parameters: -am (--also-make), which tells maven to built modules which your test module depends on and -DfailIfNoTests=false, otherwise "No tests were executed!" error appears.

mvn test -pl B -Dtest=MyTestClass#myTest -am -DfailIfNoTests=false

pom.xml section in root:

<modules>
    <module>A</module>
    <module>B</module>
<modules>

B depends on A.

Handmaiden answered 17/7, 2020 at 12:54 Comment(0)
E
4

The test parameter 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:

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

Embassy answered 13/4, 2011 at 17:27 Comment(0)
S
4

You need to specify the JUnit test class and its method to be executed.

mvn test -Dtest=com.mycompany.AppTest#testMethod

https://metamug.com/article/java/build-run-java-maven-project-command-line.html#running-unit-tests

Shadbush answered 23/10, 2021 at 10:21 Comment(0)
C
3

New versions of JUnit contains the Categories runner: 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.

Curse answered 10/12, 2009 at 14:10 Comment(2)
Nice. But I don't see how this answers the question.Murtha
Subj is supported from 2.7.3 version of maven-surefire-plugin: maven.apache.org/plugins/maven-surefire-plugin/examples/…Curse
G
3

As of surefire plugin version 2.22.1 (possibly earlier) you can run single test using testnames property when using testng.xml

Given a following testng.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> 

with the 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>

From command line

mvn clean test -DselectedTests=run-B-test

Further reading - Maven surefire plugin using testng

Globoid answered 8/11, 2018 at 1:45 Comment(0)
C
-4

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:

mvn test -Dtest=MyTest
Communicable answered 9/12, 2009 at 15:36 Comment(2)
I tested solution described on previous comments in a single module project, so what this comment is saying is not true.Taffeta
It may have been true in 2009 when originally answered.Cacomistle

© 2022 - 2024 — McMap. All rights reserved.