【问题标题】:Setting invocationCount from a variable从变量设置 invocationCount
【发布时间】:2019-12-25 14:17:34
【问题描述】:

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

【问题讨论】:

    标签: testng rest-assured


    【解决方案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 运行。

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

    【讨论】:

      【解决方案2】:

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

      这里是转换器类代码:

      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 倍,正是我所寻找的。​​p>

      【讨论】:

        猜你喜欢
        • 2013-08-30
        • 1970-01-01
        • 1970-01-01
        • 2013-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-24
        相关资源
        最近更新 更多