我可以只对一组测试运行@BeforeTest和@AfterTest吗?

时间:2019-10-18 16:54:13

标签: java selenium cucumber testng

我正在为具有多个模块的项目构建测试框架。目前,我的testng.xml中有两个测试。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <listeners>
    <listener class-name = "listener-class" />
  </listeners>
  <test thread-count="5" name="frontEnd">
  <parameter name="URL" value="front-end-url" />
    <classes>
      <class name="frontendTestRunner"/>
    </classes>
  </test>
  <test thread-count="5" name="Backend">
  <parameter name="URL" value="back-end-url" /> 
    <classes>
      <class name="backendtestrunner"/>
    </classes>
  </test>  <!-- Test -->
</suite> <!-- Suite -->

对于这两个测试,之前和之后的条件都不同。

是否有办法让@BeforeTest和@AfterTest方法仅在一个测试中运行,例如说“ frontEnd”,而对第二个测试方法则使用不同的@BeforeTest和@AfterTest方法-“后端”。

当前,我正在使用两个不同的Cucumber测试运行程序来完成此任务。但是我只想使用一个testrunner来做。

这是我的两个TestRunner:

@CucumberOptions(
            features = "src/test/java/com/frontEnd",
            glue = "com/stepDefinitions/frontEnd",
            tags = {"~@ignore"}
        )
public class FrontEndTestRunner extends AbstractTestNGCucumberTests {

    @Parameters( {"URL"})
    @BeforeTest
    public static void setup(String url) throws IOException {
        TestConfiguration.initialise(true);
        Pages.initialise();
        TestConfiguration.getHomePageUrl(url);
    }

    /**
     * This class runs after the completion of the final test. It populates the reports and closes the browser driver. 
     */
    @AfterTest
    public static void after() {
        Browser.close();        
    }

}

@CucumberOptions(
        features = "src/test/java/com/backEnd",
        glue = "com/stepDefinitions/backEnd"
    )
public class BackEndTestRunner extends AbstractTestNGCucumberTests {

    @BeforeTest
    public static void setup() throws IOException {
        TestConfiguration.initialise(true);
        Pages.initialise();
    }

    /**
     * This class runs after the completion of the final test. It logs the user out and closes the browser driver. 
     */
    @AfterTest
    public static void after() {
        Browser.close();        
    }
}

有没有办法我只能使用一个TestRunner,并且仍然能够以某种方式针对这两组功能运行正确的Begin和After条件?

基本上,我需要能够对两组功能进行分组,并根据功能所属的组来调用两个不同的@BeforeTest和@AfterTest方法。

2 个答案:

答案 0 :(得分:1)

我为您整理了一个示例maven项目,该项目保留一个测试运行器文件,并允许您将前端文件和后端文件分开(或者可以根据需要将它们组合在一起)

在src / test / java中:

  1. 添加一个名为“功能”的程序包
    • 创建一个名为“ backend.feature”的文件
    • 创建一个名为“ frontend.feature”的文件
  2. 添加一个名为“ runners”的软件包
    • 创建一个名为“ myRunner.java”的类文件
  3. 添加一个名为“ stepDefinitions”的包
    • 创建一个名为“ backendSteps.java”的类文件
    • 创建一个名为“ frontendSteps.java”的类文件
  4. 更新您的maven和testng xml文件



后端功能

Feature: Back End.

@backend
Scenario: Back end scenario.
Given Back end given
When  Back end when
Then  Back end then



frontend.feature

Feature: Front End.

@frontend
Scenario: Front end scenario.
Given Front end given
When  Front end when
Then  Front end then



myRunner.java

package runners;



import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;



@CucumberOptions(features   = "src/test/java/features",
                 glue       = "stepDefinitions")
public class myRunner extends AbstractTestNGCucumberTests
{
}



backendSteps.java

package stepDefinitions;



import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.cucumber.java.After;
import io.cucumber.java.Before;



public class backendSteps
{
    @Before("@backend")
    public void setUp()
    {
        System.out.println("@Before for back end");
    }


    @After("@backend")
    public void tearDown()
    {
        System.out.println("@After for back end");
    }


    //  back end tests
    @Given("^Back end given$")
    public void Back_end_given()
    {
        System.out.println("Back end given");
    }


    @When("^Back end when$")
    public void Back_end_when()
    {
        System.out.println("Back end when");
    }


    @Then("^Back end then$")
    public void Back_end_then()
    {
        System.out.println("Back end then");
    }
}



frontendSteps.java

package stepDefinitions;



import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.cucumber.java.After;
import io.cucumber.java.Before;



public class frontendSteps
{
    @Before("@frontend")
    public void setUp()
    {
        System.out.println("@Before for front end");
    }


    @After("@frontend")
    public void tearDown()
    {
        System.out.println("@After for front end");
    }


    //  front end tests
    @Given("^Front end given$")
    public void Front_end_given()
    {
        System.out.println("Front end given");
    }


    @When("^Front end when$")
    public void Front_end_when()
    {
        System.out.println("Front end when");
    }


    @Then("^Front end then$")
    public void Front_end_then()
    {
        System.out.println("Front end then");
    }
}



pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


    <modelVersion>4.0.0</modelVersion>
    <groupId>YourGroupID</groupId>
    <artifactId>YourArtifactID</artifactId>
    <version>0.0.1-SNAPSHOT</version>


    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>


    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>TestNgCucumber.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>

        </plugins>
    </build>


    <dependencies>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.0.0</version>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>4.8.0</version>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>4.8.0</version>
        </dependency>

    </dependencies>


</project>



TestNgCucumber.xml

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


<suite name="Suite">
    <test name="Test">
        <classes>
            <class name="runners.myRunner" />
        </classes>
    </test>
</suite>



输出到控制台

[RemoteTestNG] detected TestNG version 7.0.0
@Before for back end
Back end given
Back end when
Back end then
@After for back end
@Before for front end
Front end given
Front end when
Front end then
@After for front end

===============================================
Suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================


或者,您可以从2个步骤定义文件中注释掉@Before和@After方法,并用“ stepDefinitions”包中的单独文件替换它们,如下所示:


Hooks.java

package stepDefinitions;



import io.cucumber.java.After;
import io.cucumber.java.Before;



public class Hooks
{
    @Before("@frontend")
    public void setUpFrontend()
    {
        System.out.println("@Before for front end");
    }


    @Before("@backend")
    public void setUpBackend()
    {
        System.out.println("@Before for back end");
    }


    @After("@frontend or @backend")
    public void tearDown()
    {
        System.out.println("@After for front end and back end");
    }
}



输出到控制台

[RemoteTestNG] detected TestNG version 7.0.0
@Before for back end
Back end given
Back end when
Back end then
@After for front end and back end
@Before for front end
Front end given
Front end when
Front end then
@After for front end and back end

===============================================
Suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================


答案 1 :(得分:1)

我可以看到您使用的是testng钩子而不是黄瓜钩子。您可以使用黄瓜钩轻松实现这一目标。将黄瓜钩类放在“步骤定义”包中。可以对@before和@after块进行标记,以便仅对具有该特定标记的方案调用挂钩中的方法。例如检查下面的代码 beforeFrontEndScenario afterFrontEndScenario 仅针对标记为@frontEnd的场景调用。通过执行此操作,您将能够拥有单个运行器文件,并且仍然具有针对叶状体和后端方案的独立钩子。

这是cucucmber钩子类的示例

public class Hooks {

 @Before
    public void beforeScenario(){
        System.out.println("method is executed before all scenario");
    } 

 @After
    public void afterScenario(){
        System.out.println("method is executed after all scenario");
    }

 @Before("@frontEnd")
public void beforeFrontEndScenarios(){
    System.out.println("before block called for scenario with tag frontEnd");
} 

  @After("@frontEnd")
  public void afterFrontEndScenarios(){
    System.out.println("after block called for scenario with tag frontEnd");
  } 


}