我有一个测试类,其中包含用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解决方案都说明了如何使用数据提供程序创建简单的数据集。我的数据集来自Excel工作表。
进一步,如前所述,如果我只得到50个席位,并且想按顺序运行所有测试方法50次,请问有人可以建议最好的方法吗?
答案 0 :(得分:1)
那不可以吗?
@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);
}