我正在尝试修改我的依赖测试,以便它们以特定的方式运行并且还找到了一种方法。例如,假设我有以下两个测试和定义的数据提供者:
@Dataprovider(name = "apiResponses")
Public void queryApi(){
return getApiResponses().entrySet().stream().map(response -> new Object[]{response.getKey(), response.getValue()}).toArray(Object[][]::new);
}
@Test(dataprovider = "apiResponses")
Public void validateApiResponse(Object apiRequest, Object apiResponse){
if(apiResponse.statusCode != 200){
Assert.fail("Api Response must be that of a 200 to continue testing");
}
}
@Test(dataprovider = "apiResponses", dependsOnMethod="validateApiResponse")
Public void validateResponseContent(Object apiRequest, Object apiResponse){
//The following method contains the necessary assertions for validating api repsonse content
validateApiResponseData(apiResponse);
}
假设我有100个api请求我要验证,如果上述100个请求中的一个请求返回200以外的任何状态代码,那么所有100都会跳过validateResponseContent。我和# 39; m试图实现的是,只有在没有状态代码200的情况下返回的api响应才会跳过相关测试,并且对于返回状态代码为200的响应运行所有测试。
答案 0 :(得分:0)
是否会添加if / else块解决此问题?
@Test(dataprovider = "apiResponses")
Public void validateApiResponse(Object apiRequest, Object apiResponse){
if(apiResponse.statusCode != 200){
Assert.fail("Api Response must be that of a 200 to continue testing");
} else {
validateApiResponseData(apiResponse);
}
}
答案 1 :(得分:0)
您应该使用TestNG Factory
,为每个实例创建同时包含apiRequest
和apiResponse
的实例。现在每个实例基本上首先在状态代码上运行一个断言,然后再继续验证实际的api响应。
以下是一个示例,说明了这种情况:
public class TestClassSample {
private Object apiRequest, apiResponse;
@Factory(dataProvider = "apiResponses")
public TestClassSample(Object apiRequest, Object apiResponse) {
this.apiRequest = apiRequest;
this.apiResponse = apiResponse;
}
@Test
public void validateApiResponse() {
Assert.assertEquals(apiResponse.statusCode, 200, "Api Response must be that of a 200 to continue testing");
}
@Test(dependsOnMethods = "validateApiResponse")
public void validateResponseContent() {
//The following method contains the necessary assertions for validating api repsonse content
validateApiResponseData(apiResponse);
}
@DataProvider(name = "apiResponses")
public static java.lang.Object[][] queryApi() {
return getApiResponses().entrySet()
.stream().map(
response -> new java.lang.Object[]{
response.getKey(), response.getValue()
})
.toArray(Object[][]::new);
}
}