![](/img/trans.png)
[英]Error using Rest Assured RequestSpecification with testng
[英]Handling exceptions/failures using Rest Assured and TestNG
我试图在我的测试中找到一种处理错误的通用方法,它使用 TestNG 作为框架,并使用 Rest Assured 作为库来进行 REST 调用。
在一些 Rest Assured @Test 注释方法中,我将其包含在 try/catch 中:
@Test
public void readyForSend(String uid) {
try{
Response response =
given().header("X-AI-Test-ID","new-shop-user").
spec(requestSpecUserCreationService).
when().
get("/api/v11/new/createUser?uid=" + uid ).
then().
assertThat().statusCode(200);
catch(AssertionError ae){
logger.info("Unable to create new user");
}
}
在我的 TestNG 文件中,我有以下设置:
<suite name="create new user" verbose="1" configfailurepolicy="continue">
我有超过 30 个 REST 调用来进行每次迭代,并且正在使用例如invocationCount = 10
所以一个单一的 Rest 保证失败没有它自己的尝试/捕获并且整个失败。 我必须将每个都包含在 try/catch 中,还是有更好的方法来进行“软断言”,这样如果不单独处理,测试就不会轰炸我?
一种简单的方法是简单地创建一个方法,该方法封装了 get 和其他 post 调用。 您也可以在此方法中进行进一步的异常处理。 可以返回状态代码,并且可以在此之后进行软断言。 例如。
public static Response doGet(String endpoint) {
Response response = given(defaultRequestSpec).when().get(endpoint).andReturn();
//defaultRequestSpec can hold your headers, if they are common or pass as arguments
return response;
}
在您的 @Test 方法中,调用此方法
@Test(invocationCount=10) {
SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals(this.doGet("/api/v11/new/createUser?uid=...").statusCode(), 200, "Verify createUser response is 200");
....
//further api calls
softAssert.assertAll();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.