当在IDE中使用run选项运行时,使用TestNGXML的并行执行运行良好,但mvn-clean测试失败,并出现与参数相



我正在尝试使用两组浏览器进行并行执行。testng.xml是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" thread-count="2">
<test name="Test1" parallel="methods" thread-count="3">
<parameter name="browser" value="chrome"/>
<classes>
<class name="Test01" />
</classes>
</test>
<test name="Test2" parallel="methods" thread-count="3">
<parameter name="browser" value="firefox"/>
<classes>
<class name="Test02" />
</classes>
</test>
</suite>

因此,第一个测试类使用Chrome并行运行其方法。第二个测试类使用Firefox并行运行其方法。该套件并行运行测试。

当我在IDE(Intellij IDEA(中右键单击TestNG.xml时,它运行得很好,我可以看到所有测试都通过了

但是当我使用终端并使用"mvn-clean-test"运行它时,我希望它能运行并通过

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
Configuring TestNG with: TestNG652Configurator
Tests run: 12, Failures: 2, Errors: 0, Skipped: 10, Time elapsed: 0.862 sec <<< FAILURE! - in TestSuite
beforeTest(Test02)  Time elapsed: 0.69 sec  <<< FAILURE!
org.testng.TestNGException: 
Parameter 'browser' is required by BeforeMethod on method beforeTest but has not been marked @Optional or defined
at org.testng.internal.Parameters.createParams(Parameters.java:290)
at org.testng.internal.Parameters.createParametersForMethod(Parameters.java:359)
at org.testng.internal.Parameters.createParameters(Parameters.java:620)
at org.testng.internal.Parameters.createConfigurationParameters(Parameters.java:190)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:209)

这就是pom的样子:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>

这是@BeforeMethod:

@Parameters("browser")
@BeforeMethod
public void beforeTest(String browsers){
if(browsers.equals("chrome")){
logger.info("Starting Chrome Browser session in Headless mode");
ChromeOptions options = getChromeOptions();
setWebDriver(new ChromeDriver(options));
}else if(browsers.equals("firefox")){
logger.info("Starting Firefox Browser session in Headless mode");
FirefoxOptions firefoxOptions = getFirefoxOptions();
setWebDriver(new FirefoxDriver(firefoxOptions));
}
}

我做错了什么?在几次试图理解这个错误之后,我感到非常困惑。使用Intellij IDEA中的run按钮,testng xml如何运行良好,但mvn clean test因上述错误而失败?我想了解终端运行与IDE运行的区别背后的逻辑。

使用maven surefire插件,并尝试在构建标签下添加以下配置:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>

或作为参数传递:

mvn -Dsurefire.suiteXmlFiles=testng.xml clean test

要运行testNg.xml,我们可以在标记<suiteXmlFile>.中传递xml文件只有当您的项目遵循maven创建的相同结构时,它才会起作用:src/main和src/test。

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skip>false</skip>
<forkCount>0</forkCount>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>

如果您更改了结构,那么需要在<sourceDirectory>标签中的文件夹中提及,就像我举的例子一样src:

<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<!-- plugin executes the testng tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skip>false</skip>
<forkCount>0</forkCount>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>

最新更新