我有一个带有 PrivateApiTest 类的 Maven 项目,它从我的应用程序中测试一些私有 API。
在这个类中,我有一个带有注释 @BeforeAll 的方法 setup() ,它从我存储在 JSON 文件中的 Api 集合中获取所有 Api 的响应。
这是功能:
@BeforeAll
public void setup() {
//Creating the report
report = new ExtentReports("src/main/ressources/test_results/apiTest.html",true);
//Create object mapper and list
objectMapper = new ObjectMapper();
listType = objectMapper.getTypeFactory().constructCollectionType(List.class, Api.class);
try {
apiList = objectMapper.readValue(new File(ressourcesPath + jsonFile), listType);
apiResponseMap = new HashMap<>();
//function to get the cookie
cookie = UtilApi.login(domain, "test", "test");
if( cookie == null ) {
fail("Failed to log");
}
//We get all the api's response and put them into a map to reuse them for the tests
for (Api api : apiList) {
endpoint = UtilApi.generateEndpoint(api.getVersion(), api.getName(), api.getParametres());
response = UtilApi.getResponse(testResult, url, endpoint, cookie);
apiResponseMap.put(api, response);
}
} catch (IOException e) {
// TODO Auto-generated catch block
UtilTest.testFail(testResult, failString + e.getMessage());
}
}
之后,我创建了一个动态测试流,以在一个名为 ResponseCode_Test 的独特函数中测试所有这些 api 响应
这是我的函数代码:
@TestFactory
public Stream<DynamicTest> ResponseCode_Test(){
Iterator<Map.Entry<Api,Response>> inputStream = apiResponseMap.entrySet().iterator();
Function<? super Map.Entry<Api, Response>, String> displayNameGenerator = input -> input.getKey().getName() + " v" + input.getKey().getVersion();
ThrowingConsumer<Map.Entry<Api,Response>> testExecutor = input -> {
Api api = input.getKey();
Response response = input.getValue();
testResult = report.startTest( input.getKey().getName() + " v" + input.getKey().getVersion() + " response code");
try {
assertEquals(response.getStatusCode(), 200);
testResult.log(LogStatus.PASS, input.getKey().getName() + " v" + input.getKey().getVersion() + " response code OK");
} catch (AssertionError e) {
failString += "The api" + api.getName() + " returned code " + response.getStatusCode();
UtilTest.testFail(testResult, failString);
}
};
return DynamicTest.stream(inputStream, displayNameGenerator, testExecutor);
}
还有另一个动态测试流,用于测试我的 API 上是否存在字符串“success”
@TestFactory
public Stream<DynamicTest> Success_Test() {
Iterator<Map.Entry<Api,Response>> inputStream = apiResponseMap.entrySet().iterator();
Function<? super Map.Entry<Api, Response>, String> displayNameGenerator = input -> input.getKey().getName() + " v" + input.getKey().getVersion();
ThrowingConsumer<Map.Entry<Api,Response>> testExecutor = input -> {
failString = "The test failed ";
Api api = input.getKey();
Response response = input.getValue();
JsonPath jsonPath = UtilApi.getJsonPath(response);
testResult = report.startTest( input.getKey().getName() + " v" + input.getKey().getVersion() + " success");
try {
assertEquals(jsonPath.getString("success"), "true");
testResult.log(LogStatus.PASS, input.getKey().getName() + " v" + input.getKey().getVersion() + " success OK");
} catch (AssertionError e) {
failString += "The API" + api.getName() + " in version " + api.getVersion() + " returned success = false ";
UtilTest.testFail(testResult, failString);
}
};
return DynamicTest.stream(inputStream, displayNameGenerator, testExecutor);
}
我的项目存储在 TeamCity 上,用于自动构建和 CI/CD。
我想在 TeamCity 上如何可能,而不是始终具有相同的方法名称,每个测试都有一个动态名称。 (见下面的截图)