【发布时间】:2019-12-25 07:23:47
【问题描述】:
我有一个测试类,其中包含用 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 次,有人可以建议最好的方法吗?
【问题讨论】:
-
我没有理由将此脚本分成 3 个单独的测试用例,因为您不做不同的断言
-
断言相同,但端点不同,每个请求的正文也不同。第二种方法的输入也来自第一种方法的输出,依此类推。@Fenio
标签: testng rest-assured testng-dataprovider