首页 > 解决方案 > 从变量设置 invocationCount

问题描述

我有一个测试类,其测试方法需要运行“n”次。数字 n 来自 API 响应。我尝试将'n'传递给测试方法的invocationCount,但它说invocationCount只能接受一个常量值而不是来自变量。

我试图通过 IAnnotationTransformers 文档,但我无法理解我需要在测试中更改什么来实现它。

这是代码

public class JourneySearch1PaxTest  {

.....


@BeforeClass
public void setup() {
    reqSpec = RestUtilities.getRequestSpecification();
    authtoken = RestUtilities.createAuthToken();
    // System.out.println("Auth_Token is " +authtoken);
    reqSpec.header("Authorization", "Bearer " + authtoken);
    reqSpec.basePath(Path.V2_APIS);
    resSpec = RestUtilities.getResponseSpecification();


}

...few methods.....

@Test   
  public void GetNumbers() throws Exception {

   Response response=
   given()
   //.log().all()
  .spec(reqSpec)
  .pathParams("service_name", ServiceName, "travel_date", TravelDate, "origin", Origin, "destination", Destination)
   .when()
   .get(EndPoints.SERVICE_DETAILS)
   .then()
   .log().all()
   .spec(resSpec)
   .extract().response()             
        JsonPath jsPath = RestUtilities.getJsonPath(response);
    BBucket = jsPath.getString("data.inventory_details[1].remaining_capacity");
        System.out.println("BBucketCapacity:" +BBucket);
    BBucketTBL=(Integer.parseInt(BBucket)*Integer.parseInt(LoadCapacity)/100);
        System.out.println("BBucketCapacityTBL:" +BBucketTBL);


 }

  @Test(invocationCount = BBucketTBL)
  public void CreateBookings() throws IOException {

    JSONObject jObject = PrepareJourneySearchRequestBody(Origin,Destination,TravelDate);

    Response response = 
    given()
    //.log().all()
    .spec(reqSpec)
    .body(jObject.toString())
    .when()
    .post(EndPoints.JOURNEY_SEARCH)
    .then()
    .spec(resSpec)
    .extract().response();

    JsonPath jsPath = RestUtilities.getJsonPath(response);
    TariffCode = GetTariffCode(jsPath);


    System.out.println("TariffCode = " +TariffCode);


    JSONObject BookingObject = PrepareProvBookingRequestBody(Origin,Destination,TravelDate,ServiceName,TariffCode);
     Response Bookingresponse=
               given()
               //.log().body()
                .spec(reqSpec)
                .body(BookingObject.toString())
                .when()
                .post(EndPoints.BOOKING)
                .then()
                .spec(resSpec)
                //.log().body()
                .extract().response();
               JsonPath jsP = RestUtilities.getJsonPath(Bookingresponse);
               BookingNumber = jsP.get("data.booking.booking_number");
               float TotalPrice=jsP.get("data.booking.total_price");
               System.out.println("Booking number from create: " + BookingNumber);
               System.out.println("Price from create: " + TotalPrice);

}

}

有人可以帮我理解如何让 CreateBookings() 测试方法上的 invocationCount 接受 BBucketTBL 值的值。

标签: testngrest-assured

解决方案


我设法通过使用 IAnnotationTransformer 和一些自学来部分实现这一点。

我使用以下内容创建了一个单独的类文件:

public class MyTransformer implements IAnnotationTransformer {

public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
    {
        // int n = JourneySearch1PaxTest.class.getField(name)
        if ("CreateBookings".equals(testMethod.getName())) {
            ((ITestAnnotation) annotation).setInvocationCount(5);
        }
    }

 }

}

添加了具有以下内容的新 xml 文件:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Smoke Tests Suite">
<listeners>
    <listener class-name="com.common.MyTransformer">
    </listener>
</listeners>
<test name="Smoke Tests">
    <classes>
        <class name="FullyLoadTrain.JourneySearch1PaxTest"></class>

        </classes>
 </test>

并作为 testng Suite 运行。

测试方法 CreateBookings() 按预期运行了 5 次,因为在 Transformer 类的 invocationCount 中提到了这个数字。但是我需要这个数字作为从测试类传递的变量。有没有办法我们可以做到这一点?


推荐阅读