[英]Parallel execution using TestNG XML runs fine when run using the RUN option in an IDE but mvn clean test fails with a parameter related error
I am trying out parallel execution using two sets of browsers. 我正在尝试使用两组浏览器进行并行执行。 This is how the testng.xml looks like:
这就是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>
So the first Test Class runs its methods using Chrome parallely. 因此,第一个测试类使用Chrome并行运行其方法。 And the second Test Class runs its methods using Firefox parallely.
第二个Test Class使用Firefox并行运行其方法。 The suite runs the Tests parallely.
该套件并行运行测试。
When I run the TestNG.xml by right clicking on it in the IDE(Intellij IDEA), it runs fine and I can see that all the tests pass. 当我在IDE(Intellij IDEA)中右键单击TestNG.xml时,它运行良好,并且可以看到所有测试都通过了。
But when I use the terminal and run it using “mvn clean test” I am expecting it to run and pass. 但是,当我使用终端并使用“ mvn clean test”运行它时,我期望它能够运行并通过。 Instead I get the following error:
相反,我得到以下错误:
-------------------------------------------------------
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)
This is how the pom looks like: 这是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>
This is the @BeforeMethod: 这是@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));
}
}
What am I doing wrong? 我究竟做错了什么? I am super confused after several attempts to understand the error.
尝试理解该错误后,我感到非常困惑。 How can the testng xml run fine using the RUN button in Intellij IDEA but mvn clean test fails with the above error?
如何使用Intellij IDEA中的“运行”按钮来使testng xml正常运行,但mvn clean测试失败并出现上述错误? I would like to understand the logic behind how the terminal runs differ from the IDE runs.
我想了解终端运行方式与IDE运行方式不同的逻辑。
Use maven surefire plugin and try to add the following configuration under build tag: 使用maven surefire插件,并尝试在build标签下添加以下配置:
<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>
Or pass as parameter: 或作为参数传递:
mvn -Dsurefire.suiteXmlFiles=testng.xml clean test
To run testNg.xml we can pass the xml file in the tag <suiteXmlFile>.
要运行testNg.xml,我们可以在标记
<suiteXmlFile>.
传递xml文件<suiteXmlFile>.
It would only work when your project follows the same structure which is created by maven: src/main and src/test. 仅当您的项目遵循由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>
If you changed the structure then need to mention in folder in <sourceDirectory>
tag, like as example I have taken src: 如果您更改了结构,则需要在
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.