按顺序多次运行具有许多相关测试方法的 testng 类文件 [英] Running a testng class file with many dependent test methods multiple times sequentially
问题描述
我有一个测试类,其中包含用 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 {
我还尝试查看 @Factory 注释,但是我探索的每个 Factory 解决方案都解释了如何使用数据提供程序创建简单的数据集.我的数据集来自 Excel 工作表.
I also tried looking at the @Factory annotation however every Factory solution that I explored explains how to create a simple data set using a data provider. My data set comes from an excel sheet.
此外,就像之前提到的那样,如果我只是得到一个像 50 个座位这样的数字,并且想要按顺序运行所有测试方法 50 次,请有人建议最好的方法吗?
Further, like mention before, if I just get a mere number like 50 seats available and want to run all test methods sequentially 50 times, can someone kindly suggest the best way to do it please?
推荐答案
这不能接受吗?
@Test
public void test() throws IOException, ParseException {
JSONObject jObject = PrepareProvBookingRequestBody();
given()
.log().all()
.spec(reqSpec)
.body(jObject.toString())
.when()
.post(EndPoints.BOOKING)
.then()
.spec(resSpec);
JSONObject jObject = PreparePaymentRequestBody();
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));
given()
.spec(reqSpec)
.pathParam("booking_number", BookingNumber)
.when()
.post(EndPoints.CONFIRM_BOOKING)
.then()
.spec(resSpec);
}
这篇关于按顺序多次运行具有许多相关测试方法的 testng 类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!