Uruchom jedną metodę testową z maven

Wiem, że możesz uruchomić wszystkie testy w określonej klasie używając:

mvn test -Dtest=classname

Ale chcę uruchomić indywidualną metodę i-Dtest=classname.methodname nie działa.

Author: Naman, 2009-12-09

13 answers

Aby uruchomić pojedynczą metodę testową w Maven, musisz podać polecenie:

mvn test -Dtest=TestCircle#xyz test

Gdzie TestCircle jest nazwą klasy testowej, a {[3] } jest metodą testową.

Znaki Dzikiej Karty również działają; zarówno w nazwie metody, jak i w nazwie klasy.

Jeśli testujesz w projekcie wielomodułowym, określ moduł, w którym znajduje się test -pl <module-name>.

Dla testów integracyjnych użyj opcji it.test=... zamiast test=...:

mvn -pl <module-name> -Dit.test=TestCircle#xyz integration-test
 752
Author: Mudit Srivastava,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-01-29 16:54:43

Jest problem z surefire 2.12. Tak się stało z moją zmianą maven-surefire-plugin z 2.12 na 2.11:

  1. mvn test -Dtest=DesignRulesTest

    Wynik:
    [Błąd] nie udało się wykonać goal org.Apacz.maven.wtyczki: maven-surefire-plugin:2.12:test (domyślnie-test) na projekcie pmd: nie przeprowadzono żadnych testów!

  2. mvn test -Dtest=DesignRulesTest

    Wynik: [INFO]-- - Maven-surefire-plugin:2.11:test (default-test) @ pmd --- ... Running net.sourceforge.pmd.lang.java.zasada.design.DesignRulesTest Testów: 5, błędów: 0, błędów: 0, pominięto: 4, czas upłynął: 4.009 sek

 30
Author: Duccio Fabbri,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-28 13:33:09

Co robię z moim TestNG, (sorry, JUnit nie obsługuje tego) przypadki testowe to mogę przypisać grupę do testu, który chcę uruchomić

@Test(groups="broken")

I po prostu uruchom 'mvn-Dgroups=broken'.

 18
Author: tunaranch,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-12-09 23:13:54

Uruchamianie zestawu metod w jednej klasie testowej W wersji 2.7.3 można uruchomić tylko n testów w jednej klasie testowej.

Uwaga: jest obsługiwana dla junit 4.x i TestNG.

Musisz użyć następującej składni

mvn -Dtest=TestCircle#mytest test

Możesz również używać wzorców

mvn -Dtest=TestCircle#test* test

Od wersji surefire 2.12.1, możesz wybrać wiele metod (JUnit4X tylko w tej chwili, patche mile widziane)

mvn -Dtest=TestCircle#testOne+testTwo test

Sprawdź ten link o pojedynczych testach

 14
Author: Prasanth Kumar Diddi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-28 14:46:13

Możesz uruchomić określone klasy testowe i metody używając następującej składni:

  1. Pełny pakiet: mvn test-Dtest= " com.wyrocznia.testy.**"

  2. Wszystkie metody w klasie: mvn test-Dtest=CLASS_NAME1

  3. Jedna metoda z jednej klasy: mvn test-Dtest=CLASS_NAME1#METHOD_NAME1

  4. Metoda wielokrotna z wielu klas: mvn test-Dtest=CLASS_NAME1#METHOD_NAME1, CLASS_NAME2 # METHOD_NAME2

 12
Author: Nazmul Hoque Shafin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-02 03:10:08

To polecenie działa !! mvn "-DTest=JoinTeamTestCases#validateJoinTeam" test zauważ, że "- DTest "zaczyna się wielkimi literami "T".

 8
Author: vikas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-07 11:14:13

Parametr test wymieniony przez tobrien pozwala określić metodę wykorzystującą # przed nazwą metody. To powinno działać dla JUnit i TestNG. Nigdy nie próbowałem, po prostu przeczytaj to na stronie Surefire plugin :

Określ ten parametr, aby uruchamiać poszczególne testy według nazwy pliku, nadpisując parametry includes / excludes. Każdy wzorzec, który tutaj podasz, zostanie użyty do utworzenia wzorca include sformatowanego jak ** / ${test}.java, więc możesz po prostu wpisać "- Dtest=MyTest", aby uruchomić pojedynczy test o nazwie " foo/mytest.java". Parametr ten nadpisuje parametry includes/excludes oraz parametr TestNG suiteXmlFiles. od wersji 2.7.3 można wykonać ograniczoną liczbę metod w teście dodając #myMethod lub # my * ethod. Typ Si "-Dtest=MyTest#myMethod" obsługiwany dla junit 4.x i testNg

 4
Author: Wesley Hartford,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-13 17:27:22

Nowe wersje JUnit zawierają kategorie runner: http://kentbeck.github.com/junit/doc/ReleaseNotes4.8.html

Ale procedura wydania JUnit nie jest oparta na mavenie, więc użytkownicy maven muszą umieścić go ręcznie w swoich repozytoriach.

 3
Author: Andriy Plokhotnyuk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-12-10 14:10:01

Od wersji wtyczki surefire 2.22.1 (prawdopodobnie wcześniej) można uruchomić pojedynczy test przy użyciu właściwości testnames podczas korzystania z testng.xml

Podano następujący 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> 
Z 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>

Z linii poleceń

mvn clean test -DselectedTests=run-B-test

Czytaj dalej- Maven surefire plugin using testng

 3
Author: bsk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-11-08 01:45:48

Próbowałem kilku rozwiązań dostarczonych w tym wątku, jednak nie działały one dla modułu, który zależy od innego. W takim przypadku musiałem uruchomić mvn z głównego modułu z dodatkowymi parametrami: -am (--also-make), który mówi maven do zbudowanych modułów, od których zależy twój moduł testowy i -DfailIfNoTests=false, w przeciwnym razie " żadne testy nie zostały wykonane!"pojawia się błąd.

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

Pom.sekcja xml w korzeniu:

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

B zależy od A.

 2
Author: jwpol,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-07-17 12:54:37

Uruchom jedną metodę testową z klasy testowej.

mvn test -Dtest=Test1#methodname


Inne powiązane przypadki użycia

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

 2
Author: Yatendra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-07-20 16:12:34

Można uruchomić jedną klasę testową, ale nie jedną metodę w ramach klasy testowej. Używasz prostej nazwy klasy, a nie w pełni kwalifikowanej nazwy klasy. Tak więc, jeśli masz test w " org.sonatype.test.MyTest " i to jest jedyny test, który chcesz uruchomić, twój wiersz poleceń wyglądałby tak:

mvn test -Dtest=MyTest
 -5
Author: Tim O'Brien,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-27 14:23:59

Według mojej wiedzy, surefire plugin nie zapewnia żadnego sposobu, aby to zrobić. Ale nie krępuj się otworzyć Problem :)

 -9
Author: Pascal Thivent,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-12-09 15:14:33