简体   繁体   中英

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:

<?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. And the second Test Class runs its methods using Firefox parallely. 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.

But when I use the terminal and run it using “mvn clean test” I am expecting it to run and pass. 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:

<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:

@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? I would like to understand the logic behind how the terminal runs differ from the IDE runs.

Use maven surefire plugin and try to add the following configuration under build tag:

    <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>. It would only work when your project follows the same structure which is created by maven: src/main and 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:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM