【问题标题】:Running a testng class file with many dependent test methods multiple times sequentially按顺序多次运行具有许多依赖测试方法的 testng 类文件
【发布时间】: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


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

【讨论】:

  • 我其实没想到这可以做到。此外,我想将它们分开,因为它们是不同的端点。如果其中一个失败,整个测试方法将被报告为失败。您的建议现在对我来说很容易,但是我们可以将它们分开吗?@Fenio
  • @Mihir 即使PrepareProvBookingRequest 失败,您也可以继续使用PreparePaymentRequest
  • 没有。准备方法使用响应先前测试方法中的值,并在当前请求的输入正文中使用它们。所以 PrepareJourneySearchRequest 使用模板,值由 Excel 表驱动。 JourneySearch 测试的响应参数用作 PrepareProvBookingRequest 的输入。如果通过,则来自该响应的响应用作 PreparePaymentRequest 的输入,如果通过,则响应值用于确认。它都是顺序的,并且依赖于它上面的方法。@Fenio
  • 更清楚地解释这一点。我们在旅行日期进行旅行搜索,例如伦敦到巴黎。那就是旅程搜索。然后从许多选项的结果中,我们选择其中一列火车并进行预订。那是临时预订。接下来,我们通过不同的付款方式(例如信用卡或借记卡或.vouchers)进行付款,以便进行付款,最后一旦付款通过,我们就会得到确认。这就是整个流程,我希望为所有可用座位完成此操作。如果还有 50 个席位。我想通过测试来填满。@Fenio
  • @Mihir 我认为您不应该将这些测试分开。似乎它更像是测试一个过程,而不是一个模块/单元。端到端测试往往采用一种带有@Test 注释的方法。在测试之间建立依赖关系被认为是一种不好的做法。
猜你喜欢
  • 1970-01-01
  • 2016-01-22
  • 2023-03-10
  • 1970-01-01
  • 2011-10-17
  • 2021-09-01
  • 2015-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多