maven可以同时执行cucumber-junit和testng跑步者吗?

2022-02-24 00:00:00 cucumber java testng maven

我有两节黄瓜跑步课。其中一个是基于JUnit的运行器,另一个是基于TestNG的运行器。Junit one是我为API测试实现的第一个运行器,并通过maven按预期运行。我在项目中添加了一个Web测试,并使用TestNG帮助我实现跨浏览器测试。因此,我创建了一个额外的黄瓜跑步者(基于TestNG)。当我通过testng.xml文件运行Web测试时,它会按预期运行。此时没有问题。

JUnit Cucumber Runner:

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@IncludeTags("api")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "stepdefinitions")
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "json:target/surefire-reports/cucumber.json")


public class RunCucumberTest {
}

测试黄瓜跑步机

@CucumberOptions(
        plugin = {"pretty", "html:target/surefire-reports/cucumber",
                "json:target/surefire-reports/cucumberOriginal.json"},
        glue = {"stepdefinitions"},
        tags = "@web-1",
        features = {"src/test/resources/features/web.feature"})
public class RunCucumberNGTest extends AbstractTestNGCucumberTests {

public final static ThreadLocal<String> BROWSER = new ThreadLocal<>();

  @BeforeTest
  @Parameters({"Browser"})
  public void defineBrowser(String browser) {
    //put browser value to thread-safe container
    RunCucumberNGTest.BROWSER.set(browser);
    System.out.println(browser);
  }

}

测试NG配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite" parallel="tests" thread-count="5">

    <test name="Chrome Test">
        <parameter name="Browser" value="CHROME"/>
        <classes>
            <class name="config.RunCucumberNGTest"/>
        </classes>
    </test>
    <test name="Firefox Test">
        <parameter name="Browser" value="FF"/>
        <classes>
            <class name="config.RunCucumberNGTest"/>
        </classes>
    </test>
</suite>

JUnit运行器通过maven surefire插件嵌入到POM中,因此如果我执行maven命令(例如mvn test),则如预期的那样只执行JUnit运行器。

现在,为了同时执行两个运行器,我使用<suiteXmlFiles>标记在maven surefire插件中添加了TestNG运行器。在运行时,只执行Junit运行器。没有失败。未拾取testng.xml

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <properties>
                        <configurationParameters>
                            cucumber.junit-platform.naming-strategy=long
                        </configurationParameters>
                    </properties>

                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>

                </configuration>
            </plugin>
            <plugin>
                <groupId>net.masterthought</groupId>
                <artifactId>maven-cucumber-reporting</artifactId>
                <version>5.6.2</version>
                <executions>
                    <execution>
                        <id>execution</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <projectName>AutomationFrameworkSkeleton</projectName>
                            <!-- optional, per documentation set this to "true" to bypass generation of Cucumber Reports entirely, defaults to false if not specified -->
                            <skip>false</skip>
                            <outputDirectory>${project.build.directory}
                            </outputDirectory>
                            <inputDirectory>${project.build.directory}
                            </inputDirectory>
                            <jsonFiles>
                                <param>**/*.json</param>
                            </jsonFiles>

                            <!-- optional, set true to group features by its Ids -->
                            <mergeFeaturesById>false</mergeFeaturesById>
                            <!-- optional, set true to get a final report with latest results of the same test from different test runs -->
                            <mergeFeaturesWithRetest>false
                            </mergeFeaturesWithRetest>
                            <!-- optional, set true to fail build on test failures -->
                            <checkBuildResult>false</checkBuildResult>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

有没有办法和maven一起运行cucumber Junit和TestNG类?我想是有的。我可能错误地将testng.xml文件添加到POM。

api

从您的问题看,您似乎试图同时运行推荐答案和跨浏览器Web测试。如果由于某些其他原因,您不必使用两个Runner,则可以通过以下方式实现跨浏览器配置。我用船坞容器对其进行了测试,但它可以在没有船坞的情况下使用

DriverFactory类中执行此操作。该类应该与您的stepdefs一起使用,因为它正在利用@Before

public class DriverFactory {

    public static Collection tagList;

    @Before
        public void getScenarioTags(Scenario scenario) {
            tagList =  scenario.getSourceTagNames();            
         }
         
    
        public RemoteWebDriver getManager() throws MalformedURLException , IOException {          
        if (tagList.contains("@firefox")){
              //implement FirefoxManager class to return firefox instance . class may be outside your stepdefs
               return new FirefoxManager().getDriver();
                }
          else if (tagList.contains("@chrome")){
          //implement ChromeManager class to return chrome instance. class may be outside your stepdefs
              return new ChromeManager().getDriver();
             }
            else{
                return null;
              }
        } 
                    
}

相应地在功能文件中标记场景,并将其包含在@cucumberOptions中。我以这种方式包含标记tags = {"@firefox or @chrome"},

我正在为在docker实例上运行的场景级别的并行测试运行此安装程序。但是,我使用的是courgette-jvmtestNG。如果您想通过testNG获取有关courgette-jvm的更多信息,请查看this问题。另请参阅courgette-jvm

相关文章