运行 一个 testng class 文件,其中包含许多顺序依赖的测试方法
Running a testng class file with many dependent test methods multiple times sequentially
我有一个测试 class,其中包含多个用 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 {
我也尝试查看 @Factory 注释,但是我探索的每个 Factory 解决方案都解释了如何使用数据提供程序创建简单的数据集。我的数据集来自一个excelsheet。
此外,如前所述,如果我只是得到一个像 50 个可用席位这样的数字,并且想 运行 所有测试方法依次进行 50 次,请问有人可以建议最好的方法吗?
这不是可以接受吗?
@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);
}
我有一个测试 class,其中包含多个用 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 {
我也尝试查看 @Factory 注释,但是我探索的每个 Factory 解决方案都解释了如何使用数据提供程序创建简单的数据集。我的数据集来自一个excelsheet。
此外,如前所述,如果我只是得到一个像 50 个可用席位这样的数字,并且想 运行 所有测试方法依次进行 50 次,请问有人可以建议最好的方法吗?
这不是可以接受吗?
@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);
}