【问题标题】:How to run a testng class file with many test methods multiple times in a loop如何在循环中多次运行具有多种测试方法的 testng 类文件
【发布时间】:2019-08-06 11:19:54
【问题描述】:

我有一个测试类,其中包含用 RestAssured 和 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 {

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

【问题讨论】:

  • 您需要在每个测试序列之前调用@BeforeClass 还是只能调用一次?
  • @Fenio:只能调用一次。我有一些方法可以生成 authtoken 值,该值在我在类中的每个方法中进行的 API 调用的标头中使用。
  • @Fenio:您能否就以下提到的解决方案提供您的建议?
  • 尝试像往常一样提出一个新问题:)
  • 由于它是下面提供的解决方案的延续,我在此处添加了评论,以便任何阅读此内容的人都能获得解决方案的完整流程。如果我问一个新问题,它会重复同样的问题并寻求建议。你还会建议我问一个新问题@Fenio 吗?

标签: testng rest-assured


【解决方案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}};
  }
}

【讨论】:

  • 非常感谢您的解决方案。我会试试这个,让你知道。再次感谢。
  • 我要读取的数据是一个带有一些列和行的 Excel 表。我有 3 种测试方法,其中第 2 种取决于第 1 种的输出,第 3 种取决于第一种的输出。然而,只有第一种测试方法需要从 excel 表中读取数据。当我们从数据提供者返回一个新对象时,有没有办法从 excel 输入中返回它?@Krishnan
  • 你能帮我stackoverflow.com/questions/57572362/…吗? @克里希南
猜你喜欢
  • 2019-12-25
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-25
  • 2019-02-09
  • 2019-01-08
  • 1970-01-01
相关资源
最近更新 更多