Programing

Maven에서 단일 테스트 만 건너 뛰는 방법이 있습니까?

lottogame 2020. 12. 31. 07:52
반응형

Maven에서 단일 테스트 만 건너 뛰는 방법이 있습니까?


시작하는 동안 하나의 테스트 만 건너 뛰고 싶습니다 mvn install.

그렇게하는 방법이 있습니까?


junit 4를 사용하면 @Ignore원할 때 주석을 추가 합니다. 때때로 테스트를 무시하거나 빌드가 maven에서 실행될 때만 무시하지 않는 한 이것은 당신에게 효과적입니다. 이 경우 "왜?"라고 묻습니다.

테스트 일관성이 있어야 하고 이식 가능 해야하며 항상 통과 해야합니다 . 특정 테스트에 문제가있는 경우 다시 작성하거나 완전히 제거하거나 다른 테스트 스위트 또는 프로젝트로 이동하는 것을 고려할 것입니다.


-Dtest옵션에 !(느낌표) 를 접두사 추가 하여 제외 패턴을 지정할 수 있습니다 . 예 :

mvn -Dtest=\!FlakyTest* install

그것을 발견 여기 와 작동하는 확인했습니다. 예를 들어 다음을 사용 하여이 색다른 Jenkins 테스트 를 건너 뛸 수있었습니다 .

mvn -Dtest=\!CronTabTest* package

통합 테스트를 제외해야하지만 단위 테스트를 포함해야하는 것은 정상입니다. 이를 달성하기 위해 모든 통합 테스트에 접미사 IntegrationTest (예 : AbcIntegrationTest.java)를 지정하는 것이 좋습니다.

그런 다음 maven 빌드에 다음을 입력하십시오.

  <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>**/*IntegrationTest.java</exclude>
                </excludes>
            </configuration>
        </plugin>

이것으로 빌드하면 모든 통합 테스트가 제외되지만 다른 모든 테스트 (예 : 단위 테스트)가 실행됩니다. 완벽한 :-)

테스트 실행 중 테스트 제외 및 포함에 대한 자세한 내용은

http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

PS 하나의 단일 테스트를 제외하려면 제외 목록에서 명시 적으로 이름을 지정하면됩니다. 쉬운.


주석을 사용 하여이 솔루션을 살펴보십시오.@Category

public class AccountTest {

    @Test
    @Category(IntegrationTests.class)
    public void thisTestWillTakeSomeTime() {
        ...
    }

    @Test
    @Category(IntegrationTests.class)
    public void thisTestWillTakeEvenLonger() {
        ...
    }

    @Test
    public void thisOneIsRealFast() {
        ...
    }
}

그런 다음 테스트 스위트를 사용하여 실행합니다.

@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses( { AccountTest.class, ClientTest.class })
public class LongRunningTestSuite {}

예를 들어 maven을 사용하여 테스트를 포함 ( groups) / 제외 ( excludedGroups) 할 수도 있습니다 .

mvn -DexcludedGroups=com.mycompany.tests.IntegrationTests test

이 명령을 사용하면 이것이 작동한다고 생각합니다.

mvn archetype:create -DgroupId=test -DartifactId=test

(테스트를 위해 pom.xml 및 test-class를 다음으로 변경하고 mvn install 사용)

pom.xml

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>test</name>
<url>http://maven.apache.org</url>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        test/AppTest.java
              </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.5</version>
        <scope>test</scope>
    </dependency>
</dependencies>

테스트 클래스 :

package test;
import org.junit.Test;
import static org.junit.Assert.fail;
public class AppTest 
{
    @Test
    public void test_it() {
        fail("not implemented");
    }
}

CLI를 사용하여 단일 테스트를 제외하려면 -Dtest-Dit.test플래그를 사용해야합니다 .

기본값을 재설정하도록주의하십시오. !표기법을 사용하면 모든 기본값이 지워지고 다시 넣어야합니다. surefire사용자가 실행하는 일반 테스트의 경우를 추가해야 *Test, Test*, *Tests, *TestCase하며 통합 테스트를 실행 failsafe하려면을 추가해야합니다 IT*, *IT, *ITCase.

You can find more info here https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html (normal tests)

and here https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html (integration tests)

-Dit.test='!ITsometestIT, IT*, *IT, *ITCase'

A complete mvn command could be this one:

mvn -e -B -Dtest='!unitTestABC, *Test, Test*, *Tests, *TestCase' -Dit.test='!ITintegrationTestABCIT, IT*, *IT, *ITCase' -DfailIfNoTests=false clean install

Remember to use ' and NOT ". When using double quotes any ! inside them will be evaluated by bash.

Remember also that integration tests are not run when mvn test. With mvn verify only integration tests will be run and not unit tests

ReferenceURL : https://stackoverflow.com/questions/835705/is-there-a-way-to-skip-only-a-single-test-in-maven

반응형