提问人:Serhii 提问时间:10/24/2023 最后编辑:Serhii 更新时间:10/24/2023 访问量:63
配置 Maven 以在一个项目中运行 TestNG 和 JUnit5 测试
Configuring Maven to run TestNG and JUnit5 tests in one project
问:
我需要能够在我的项目中分别运行 TestNG 和 JUnit5 测试类,并使用 testng.xml 文件一起运行它们。
以前我使用 JUnit4,但需要切换到更新的 JUnit 版本才能与 .现在,JUnit 测试类已成功执行,但是当我运行 TestNG 测试类时 - 它尝试使用 org.apache.maven.surefire.junitplatform.JUnitPlatformProvider 执行。@ParameterizedTest
@CsvSourse
为了运行测试类,我使用了以下命令:
mvn clean test -Dtest='src/test/java/tests/FiltersTest'
如何解决问题并使两个运行器都按预期工作?
P.S. 我知道在同一个项目中使用两个不同的运行器不是最好的方法,但我需要根据我正在参加的自动化课程中的任务来做到这一点。
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>
<groupId>org.example</groupId>
<artifactId>report-portal</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.14.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.24.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.5.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.4.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng-report_portal.xml</suiteXmlFile>
</suiteXmlFiles>
<systemPropertyVariables>
<browser>chrome</browser>
</systemPropertyVariables>
<systemProperties>
<property>
<name>allure.results.directory</name>
<value>target/allure-results</value>
</property>
<property>
<name>allure.report.directory</name>
<value>target/allure-report</value>
</property>
</systemProperties>
</configuration>
</plugin>
<plugin>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-maven</artifactId>
<version>2.10.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<allure.testng.version>2.22.2</allure.testng.version>
</properties>
</project>
testng.xml套件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Report Portal Tests" verbose="1">
<test name="ReportPortalTest">
<classes>
<class name="tests.FiltersTest"/>
<class name="tests.LaunchViewTest"/>
</classes>
</test>
<test name="JunitTest">
<classes>
<class name="tests.JunitTest"/>
</classes>
</test>
<listeners>
<listener class-name="io.qameta.allure.testng.AllureTestNg"/>
</listeners>
</suite>
TestNG 测试类示例:
package tests;
import core.TestDataProvider;
import jdk.jfr.Description;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import java.util.List;
import static org.testng.AssertJUnit.*;
public class FiltersTest extends CommonConditions {
@Test(priority = 1)
@Description("Login testing")
public void loginTest() {
loginAction.doLogin();
assertTrue(loginAction.isLoginSuccessful());
}
@Test(priority = 2, dataProvider = "Launch name + Launch Number", dataProviderClass = TestDataProvider.class)
@Description("Add filter: Launch name + Launch number")
public void setLaunchNameAndLaunchNumber(String launchName, String filterInput, int expectedResult) throws InterruptedException {
loginAction.doLogin();
sidebarAction.openLaunchesPage();
filtersAction.activateFiltering();
filtersAction.addLaunchNameFilter(launchName);
filtersAction.addFilterFromDrpDwn("Launch Number", filterInput);
filtersAction.switchLaunchNumberToEqual();
Thread.sleep(3000);
List<WebElement> objectList = driver.findElements(By.xpath("//td[2]//a//span[contains(., '"+launchName+"')]"));
assertEquals(expectedResult, objectList.size());
}
@Test(priority = 3, dataProvider = "Launch name + Passed", dataProviderClass = TestDataProvider.class)
@Description("Add filter: Launch name + Passed")
public void setLaunchNameAndPassed(String launchName, String filterInput, int expectedResult) throws InterruptedException {
loginAction.doLogin();
sidebarAction.openLaunchesPage();
filtersAction.activateFiltering();
filtersAction.addLaunchNameFilter(launchName);
filtersAction.addFilterFromDrpDwn("Passed", filterInput);
Thread.sleep(3000);
List<WebElement> objectList = driver.findElements(By.xpath("//td[2]//a//span[contains(., '"+launchName+"')]"));
assertEquals(expectedResult, objectList.size());
}
}
如果您需要其他信息,请告诉我。
答:
1赞
Slawomir Jaranowski
10/24/2023
#1
您可以尝试以下配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.1</version>
<configurations>
<!-- ... your configurations ... -->
</configurations>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit-platform</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
</plugin>
在一个构建中启用 Junit 5 和 TestNg 执行器/提供程序。
来源:https://maven.apache.org/surefire/maven-surefire-plugin/examples/providers.html
评论
0赞
Serhii
10/24/2023
这种方法帮助我分别运行每个测试类,但在执行过程中,两种类型的测试运行程序都被使用,因此 - 除了已执行测试类的测试结果外,我还看到一个额外的空测试执行,其中有 0 个测试结果(测试运行:0)。
1赞
khmarbaise
10/24/2023
不是真的......只需使用 testng junit 引擎 youtu.be/Xy6m9rNYBhc?si=hsUB54rlXnDXa9RS github.com/khmarbaise/youtube-videos/blob/main/episode-3/...
评论