从变量设置 invocationCount [英] Setting invocationCount from a variable
问题描述
我有一个测试类,其中的测试方法需要运行n"次.数字 n 来自 API 响应.我尝试将n"传递到测试方法的 invocationCount 中,但它说 invocationCount 只能接受一个常量值,而不能接受来自变量的值.
I have a test class with a test method that needs to run 'n' number of times. The number n is got from a API response. I tried passing 'n' into the invocationCount of the test method but it says invocationCount can only accept a constant value and not from a variable.
我尝试阅读 IAnnotationTransformers 文档,但我无法理解在我的测试中到底需要更改什么才能实现它.
I tried to go through IAnnotationTransformers documentation but I wasn't able to understand what exactly I need to change in my tests to implement it.
这是代码
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 值的值.
Can someone kindly help me understand how can I get the invocationCount on the CreateBookings() test method to accept the value of BBucketTBL value.
推荐答案
我成功破解了这个问题!我想考虑用于调用计数的变量现在存储在一个外部文件和我的转换器类中,我有方法首先读取该数字,并在设置调用计数时将其用作参数.
I managed to crack this! The variable that I wanted to be considered for the invocation count is now stored in an external file and in my transformer class, I have method that reads that number first and uses that as a parameter when I set the invocation count.
这是转换器类代码:
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 文件类似于:
The BucketNumber file has something like:
BBucketTBL:25
该测试方法准确地运行了我想要的 25 倍.
The test method runs 25 times exactly what I was looking for.
这篇关于从变量设置 invocationCount的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!