Фильтруйте JUnit 5 тестовых случаев с аннотацией @Tag ("name_test"), используя Maven - PullRequest
0 голосов
/ 14 января 2019

Я хочу отфильтровать свои тесты Junit 5, я использую аннотацию @Tag ("type_test"). Я запускаю свой тест с maven с помощью команды "mvn -Dtests = a test", но он запускает все случаи. Например, у меня есть два метода, и я хочу запустить только метод с @Tag ("a"), но результат в консоли - "Hello word 1" и "Hello word 2". Смотрите пример кода.


    static Properties properties = null;

    @BeforeAll
        public static void setUp() throws Throwable {
        properties = CommonMethods.loadConfigPropertiesFile();
        RestAssured.baseURI = properties.getProperty("BASE_HOST");
    }

    @Test
    @Tag("a")
    public void test1() {
        System.out.println("hello word 1");
    }   


    @Test
    @Tag("b")
    public void test2() {
        System.out.println("hello word 2");
    }
}

pom.xml

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <configuration>
                    <properties>
                        <includeTags>${tests}</includeTags>
                    </properties>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.0-M2</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>5.0.0-M2</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    <dependencies>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.0.0-M2</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.0.0-M2</version>
        </dependency>
    </dependencies>


Ответы [ 2 ]

0 голосов
/ 15 января 2019

Я нашел решение. Важно проверить и попробовать последнюю версию каждой зависимости. В этом примере:

Maven-безошибочный-плагин (3.0.0-M3) junit-platform-surefire-провайдера (1.3.0-M1) двигатель Юнит-Юпитер - (5.4.0-M1) junit-jupiter-api - (5.4.0-M1)

* Решения 1006 *

Без профилей:

pom.xml

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M3</version>
            <configuration>
                <properties>
                    <includeTags>${tests}</includeTags>
                </properties>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.3.0-M1</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.4.0-M1</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.4.0-M1</version>
    </dependency>
</dependencies>

И вы можете использовать команду "mvn test -Dtests = a", чтобы выполнять только методы с аннотацией @Tag ("a")

с профилями:

Добавьте этот пример кода в pom.xml

<profiles>
    <profile>
        <id>serverdevelop</id>
        <properties>
            <tests>develop</tests>
        </properties>
    </profile>
    <profile>
        <id>servertesting</id>
        <properties>
            <tests>testing</tests>
        </properties>
    </profile>
    <profile>
        <id>serverproduction</id>
        <properties>
            <tests>production</tests>
        </properties>
    </profile>
</profiles>

И, например, вы можете использовать команду "mvn test -Pserverdevelop" для выполнения только методов с аннотацией @Tag ("развернуть"). Это очень полезно для разделения тестовых случаев по средам.

0 голосов
/ 15 января 2019

Версии и библиотеки, которые вы используете, устарели. Пожалуйста, повторите с:

  • Maven Surefire 2.22.1 (лучше: 3.0.0-M3)
  • Юнит Юпитер 5.3.2 (лучше: 5.4.0-M1)

Смотрите этот образец pom.xml файла, который также описывает, как фильтровать теги:

<build>
    <plugins>
        <!-- JUnit 5 requires Surefire version 2.22.1 or higher -->
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
            <configuration>
                <groups>a</groups>
                <!-- excludedGroups>slow</excludedGroups -->
            </configuration>
        </plugin>
    </plugins>
</build>

Источник: https://github.com/junit-team/junit5-samples/blob/master/junit5-migration-maven/pom.xml

Подробнее о настройке платформы JUnit с Maven см. В руководстве пользователя JUnit 5 https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven или документации Maven Surefire: https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html

...