gpt4 book ai didi

java - 我可以让 @BeforeTest 和 @AfterTest 仅针对一组测试运行吗?

转载 作者:行者123 更新时间:2023-12-02 01:16:35 26 4
gpt4 key购买 nike

我正在为一个具有多个模块的项目构建一个测试框架。目前,我的 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”)运行,并为第二个测试(“Backend”)运行不同的 @BeforeTest 和 @AfterTest 方法。

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

这是我的两个测试运行者:

@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 方法。

最佳答案

我为您整理了一个示例 Maven 项目,该项目保留单个测试运行程序文件,并允许您将前端和后端文件分开(或者如果需要,您可以将它们组合起来)

在 src/test/java 中:

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


<小时/>

后端.feature

Feature: Back End.

@backend
Scenario: Back end scenario.
Given Back end given
When Back end when
Then Back end then
<小时/>


<小时/>

前端.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
===============================================
<小时/>


关于java - 我可以让 @BeforeTest 和 @AfterTest 仅针对一组测试运行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58455181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com