如何在循环中多次运行具有许多测试方法的 testng 类文件 [英] How to run a testng class file with many test methods multiple times in a loop

查看:75
本文介绍了如何在循环中多次运行具有许多测试方法的 testng 类文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试类,其中包含用 RestAssured 和 TestNG 编写的多种方法.我想在循环中顺序执行这些方法.我们该怎么做?

I have a test class with multiple methods written in RestAssured and TestNG. And I want to execute these methods sequentially in a loop. How can we do that?

要求是填满一列火车.我有一个 API,它为我提供了火车上可用的座位数.知道这个数字后,我想运行一个循环,以便它每次都按顺序执行一些测试方法,例如进行旅程搜索、创建预订、付款和确认预订.因此,假设我们有 50 个可用座位,我想运行测试 50 次,其中每个循环依次执行每个方法.

The requirement is to fill up a train. I have an API which gives me the number of seats available on a train. Knowing that number, I want to run a loop such that it executes a few test methods like do a journey search, create a booking, make the payment and confirm the booking sequentially every time. So lets say if we have 50 seats available, I want to run the test 50 times where each loop executes each of the methods sequentially.

这是我的示例代码:

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 次,但是我想先按顺序运行其他测试方法,然后第二次运行此测试.

I tried using invocationCount = n. But that executes the method n number of times however I want to run other test methods in sequence first and then run this test second time.

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

有人可以帮助我如何在循环中使用多个测试方法运行测试类吗?

Can someone help me on how I can run the test class with multiple test methods in a loop please?

推荐答案

您可以使用由数据提供程序提供支持的 @Factory 轻松完成此操作.

You can do this easily using a @Factory that is powered by a data provider.

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

Here's a working example that demonstrates how to work with @Factory (you can tweak this example to suite your needs).

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}};
  }
}

这篇关于如何在循环中多次运行具有许多测试方法的 testng 类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆