Java 使用Rest Assured和TestNG处理异常/故障
我试图找到一种处理测试中错误的通用方法,它使用TestNG作为框架,Rest Assured作为库来进行Rest调用 在try/catch中包含的一些Rest-Assured@Test注释方法中:Java 使用Rest Assured和TestNG处理异常/故障,java,testng,try-catch,assert,rest-assured,Java,Testng,Try Catch,Assert,Rest Assured,我试图找到一种处理测试中错误的通用方法,它使用TestNG作为框架,Rest Assured作为库来进行Rest调用 在try/catch中包含的一些Rest-Assured@Test注释方法中: @Test public void readyForSend(String uid) { try{ Response response = given().header("X-AI-Test-ID","
@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
,所以只有一个没有自己的try/catch的REST-Assured失败,整个过程都失败了。我必须用try/catch来封装每个测试吗?或者是否有更好的方法来执行“软断言”,这样如果不单独处理测试,测试就不会轰炸我?一个简单的方法就是简单地创建一个方法,该方法封装get和其他post调用。您还可以在此方法中执行进一步的异常处理。
可以返回状态代码,并在此之后进行软断言。
例如
在@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();
}
您是否尝试过@Test(alwaysRun=true)
?
@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();
}