使用rest-assured和testng处理异常/故障

dba5bblo  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(563)

我正试图找到一种通用的方法来处理测试中的错误,这种方法使用testng作为框架,rest assured作为库来进行rest调用。
在try/catch中包含的一些放心@test注解方法中:

@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来封装每个测试吗?或者有没有更好的方法来做一个“软Assert”,这样如果不单独处理测试,测试就不会轰炸我?

yfjy0ee7

yfjy0ee71#

一个简单的方法是简单地创建一个方法,该方法封装get和其他post调用。您也可以在这个方法中做进一步的异常处理。可以返回状态码,并在此之后进行软Assert。如。

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();
 }

相关问题