【问题标题】:Handling exceptions/failures using Rest Assured and TestNG使用 Rest Assured 和 TestNG 处理异常/失败
【发布时间】:2021-03-08 13:26:27
【问题描述】:

我正在尝试找到一种通用的方法来处理我的测试中的错误,它使用 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 所以一个没有自己的 try/catch 的 Rest Assured 失败,整个失败。我必须将每个都包含在 try/catch 中,还是有更好的方法来进行“软断言”,这样如果不单独处理,测试就不会轰炸我?

【问题讨论】:

  • 你试过@Test(alwaysRun=true) 吗?

标签: java testng try-catch assert rest-assured


【解决方案1】:

一种简单的方法是简单地创建一个方法,该方法封装了 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();
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    相关资源
    最近更新 更多