从变量设置 invocationCount

Setting invocationCount from a variable

我有一个测试 class,测试方法需要 运行 '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 值的值。

我通过使用一些自学的 IAnnotationT运行sformer 设法部分实现了这一点。

我创建了一个单独的 class 文件,其中包含以下内容:

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 套件。

测试方法 CreateBookings() 运行 是预期的 5 倍,因为在 T运行sformer Class 的 invocationCount 中提到了这个数字。但是我需要这个数字作为从测试 class 传递的变量进来。有什么办法可以实现吗?

我成功破解了!我想考虑用于调用计数的变量现在存储在外部文件和我的转换器 class 中,我有方法首先读取该数字并在设置调用计数时将其用作参数。

这里是变压器class代码:

public class MyTransformer implements IAnnotationTransformer {

  public int getbucketno() throws IOException {

  FileReader fr = new FileReader("C:\Misc\BucketNumber");
  String line;
  String bucketno;
  int bucketnum=0;
  int i=0;
  BufferedReader br = new BufferedReader(fr);

  while((line=br.readLine())!=null)
 {
    if (line.contains("BBucketTBL")) {
        bucketno = line.split(":")[1].trim();
        bucketnum=Integer.parseInt(bucketno);
        break;
    }
}
return bucketnum;
}

 //public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod, int i) {


 public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
    {
        int i = 0;
        try {
            i= getbucketno();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // int n = JourneySearch1PaxTest.class.getField(name)
        if ("CreateBookings".equals(testMethod.getName())) {
            //int i =0;
            ((ITestAnnotation) annotation).setInvocationCount(i);
        }
    }

}

}

BucketNumber 文件包含如下内容:

 BBucketTBL:25

测试方法运行了 25 次,正是我想要的。