Running a testng class file with many dependent te

2019-08-27 02:14发布

问题:

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?

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.

This is my sample code:

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();

}



}

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 {

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.

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?

回答1:

Isn't that acceptable?

@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);
}