簡體   English   中英

在IDE中使用RUN選項運行時,使用TestNG XML並行執行運行良好,但是mvn clean測試失敗,並出現與參數相關的錯誤

[英]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

我正在嘗試使用兩組瀏覽器進行並行執行。 這就是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並行運行其方法。 第二個Test Class使用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中的“運行”按鈕來使testng xml正常運行,但mvn clean測試失敗並出現上述錯誤? 我想了解終端運行方式與IDE運行方式不同的邏輯。

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

或作為參數傳遞:

mvn -Dsurefire.suiteXmlFiles=testng.xml clean test

要運行testNg.xml,我們可以在標記<suiteXmlFile>.傳遞xml文件<suiteXmlFile>. 僅當您的項目遵循由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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM