提问者:小点点

如何在循环中多次运行具有许多测试方法的testng类文件


我有一个测试类,其中有多个方法是用RestAs的和TestNG编写的。我想在循环中顺序执行这些方法。我们怎么做呢?

要求是填满一列火车。我有一个API,它给了我一列火车上可用的座位数量。知道了这个数字,我想运行一个循环,这样它就可以执行一些测试方法,比如每次顺序执行旅程搜索、创建预订、付款和确认预订。所以假设我们有50个座位,我想运行测试50次,每个循环顺序执行每个方法。

这是我的示例代码:

public class BookingEndToEnd_Test {

RequestSpecification reqSpec;
ResponseSpecification resSpec;
String authtoken = "";
String BookingNumber = "";
........few methods....

@BeforeClass
public void setup() {
    ......
}

@Test
public void JourneySearch_Test() throws IOException {

    JSONObject jObject = PrepareJourneySearchRequestBody();

    Response response = 
            given()
            .spec(reqSpec)
            .body(jObject.toString())
            .when()
            .post(EndPoints.JOURNEY_SEARCH)
            .then()
            .spec(resSpec)
            .extract().response();


    }

@Test(dependsOnMethods = { "JourneySearch_Test" })
public void MakeBooking_Test() throws IOException, ParseException {

    JSONObject jObject = PrepareProvBookingRequestBody();

    Response response = 

     given()
     .log().all()
    .spec(reqSpec)
    .body(jObject.toString())
    .when()
    .post(EndPoints.BOOKING)
    .then()
    .spec(resSpec)
    .extract().response();

}

@Test(dependsOnMethods = { "MakeBooking_Test" })
public void MakePayment_Test() throws IOException, ParseException {

    JSONObject jObject = PreparePaymentRequestBody();

    Response response = 
     given()
    .spec(reqSpec)
    .pathParam("booking_number", BookingNumber)
    .body(jObject.toString())
    .when()
    .post(EndPoints.MAKE_PAYMENT)
    .then()
    .spec(resSpec)
    .body("data.booking.total_price_to_be_paid", equalTo(0) )
    .extract().response();



}

@Test(dependsOnMethods = { "MakePayment_Test" })
public void ConfirmBooking_Test() throws IOException {
    Response response = 
            (Response) given()
    .spec(reqSpec)
    .pathParam("booking_number", BookingNumber)
    .when()
    .post(EndPoints.CONFIRM_BOOKING)
    .then()
    .spec(resSpec)
    .extract().response();

}



}

我尝试使用invocationCount=n。但这会执行方法n次,但我想先按顺序运行其他测试方法,然后第二次运行此测试。

@Test(invocationCount = 3)
public void JourneySearch_Test() throws IOException {

有人能帮助我如何在一个循环中运行多个测试方法的测试类吗?


共1个答案

匿名用户

您可以使用由数据提供者提供支持的@Factory轻松做到这一点。

这是一个演示如何使用@Factory的工作示例(您可以调整此示例以满足您的需求)。

import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

public class SampleTestClass {

  private int iteration;

  @Factory(dataProvider = "dp")
  public SampleTestClass(int iteration) {
    this.iteration = iteration;
  }

  // Change this to @BeforeClass if you want this to be executed for every instance
  // that the factory produces
  @BeforeTest
  public void setup() {
    System.err.println("setup()");
  }

  @Test
  public void t1() {
    System.err.println("t1() => " + iteration);
  }

  @Test
  public void t2() {
    System.err.println("t2() ==>" + iteration);
  }

  @DataProvider(name = "dp")
  public static Object[][] getData() {
    return new Object[][] {{1}, {2}, {3}, {4}};
  }
}