How to run a testng class file with many test meth

2019-08-30 07:22发布

问题:

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 {

Can someone help me on how I can run the test class with multiple test methods in a loop please?

回答1:

You can do this easily using a @Factory that is powered by a data provider.

Here's a working example that demonstrates how to work with @Factory (you can tweak this example to suite your needs).

import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

public class SampleTestClass {

  private int iteration;

  @Factory(dataProvider = "dp")
  public SampleTestClass(int iteration) {
    this.iteration = iteration;
  }

  // Change this to @BeforeClass if you want this to be executed for every instance
  // that the factory produces
  @BeforeTest
  public void setup() {
    System.err.println("setup()");
  }

  @Test
  public void t1() {
    System.err.println("t1() => " + iteration);
  }

  @Test
  public void t2() {
    System.err.println("t2() ==>" + iteration);
  }

  @DataProvider(name = "dp")
  public static Object[][] getData() {
    return new Object[][] {{1}, {2}, {3}, {4}};
  }
}