从变量设置invocationCount

时间:2019-08-20 11:12:41

标签: testng rest-assured

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

2 个答案:

答案 0 :(得分:1)

我通过使用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。

由于Transformer类的invocationCount中提到了该数字,因此测试方法CreateBookings()的运行预期达到了5倍。但是我需要这个数字作为从测试类传递的变量来输入。有没有办法可以做到这一点?

答案 1 :(得分:0)

我设法破解了!现在,我希望用于调用计数的变量存储在一个外部文件中,并且在我的转换器类中,我有一种方法,该方法首先读取该数字,并在设置调用计数时将其用作参数。

这是变压器类代码:

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倍。