REST保证:有用的技巧

在本文中,我收集了一些有关使用REST-assured的有用提示,这是用于自动化REST-API测试的最常见的Java库之一。

所有示例都是至关重要的,它们是根据我在50多个使用自动测试的项目中进行代码审查的实践而收集的。

将端点带到另一个地方


看来这是显而易见的。 但是,没有,很多时候您必须在请求中看到带有硬编码端点的代码。

最好将端点放入最终类的静态常量中。 同时,应避免使用反模式“常量接口”-这是一种不好的做法。

不要忘记,REST保证可以使您生成路径参数,例如:

public final class EndPoints { public static final String users = "/users/{id}"; ... } given().pathParams("id", someId).get(EndPoints.users)...; //   given().get(EndPoints.users, someId).... 

另外,如果在许多查询中使用相同的基本路径,则将其放入酒店常量并将其传递给basePath是一个好习惯,例如:

 //   url  http://host:port/appname/rest/someEndpoints private static final basePath = "/appname/rest/"; .. //       , //      : RestAssured.basePath = basePath; //     : given().basePath(basePath)... //    ,     

这同样适用于被测应用程序的主机和端口。

ContentType /接受


这些标头几乎用于所有HTTP请求中。 REST保证的作者意识到这一点,可以通过调用特殊方法来安装它们:

 //   : given().header("content-type", "application/json").header("accept", "application/json")...; //   : given().contentType(ContentType.JSON).accept(ContentType.JSON)...; 

最好在规范中或全局设置这些头。 这将增加代码的可读性。

StatusCode等


REST保证的语法为检查HTTP响应的每个组件提供了方便的语法,但实际上,您仍然会遇到类似的代码:

 //   : Response response = given()...when().get(someEndpoint); Assert.assertEquals(200, response.then().extract().statusCode()); //   : given()...when().get(someEndpoint).then().statusCode(200); 

使用规格


代码重复不好。 使用规范来减少重复。 在REST保证下,您可以为请求和响应创建规范。 在请求的规范中,我们提出了可以在请求中重复的所有内容。

 RequestSpecification requestSpec = new RequestSpecBuilder() .setBaseUri("http://localhost") .setPort(8080) .setAccept(ContentType.JSON) .setContentType(ContentType.ANY) ... .log(LogDetail.ALL) .build(); //       : RestAssured.requestSpecification = requestSpec; //   : given().spec(requestSpec)...when().get(someEndpoint); 

在响应的说明中,我们对请求进行所有重复的检查。

 ResponseSpecification responseSpec = new ResponseSpecBuilder() .expectStatusCode(200) .expectBody(containsString("success")) .build(); //       : RestAssured.responseSpecification = responseSpec; //   : given()...when().get(someEndpoint).then().spec(responseSpec)...; 

您可以为不同类型的请求/响应创建多个规范,并在正确的情况下使用它们。

不要写拐杖来变换对象


您不应该使用Jackson ObjectMapper将POJO转换为JSON,然后将结果字符串传输到请求主体。 REST保证在这方面做得很好。 为此,根据类路径中的内容,使用相同的Jackson或Gson。 JAXB用于转换为XML。 原始格式由Content-Type的值自动确定。

 given().contentType(ContentType.JSON).body(somePojo) .when().post(EndPoints.add) .then() .statusCode(201); //        : SomePojo pojo = given(). .when().get(EndPoints.get) .then().extract().body().as(SomePojo.class); 

此外,确保REST的出色工作将HashMap转换为JSON,反之亦然。

利用Groovy的力量


REST保证的库本身是用Groovy编写的,它允许您将Groovy的各种方法应用于接收到的JSON / XML响应。 例如:

 //  find, findAll         ,  collect       . //  it         Map<String, ?> map = get(EndPoints.anyendpoint).path("rootelement.find { it.title =~ 'anythingRegExp'}"); //     ,     Map<String, ?> map = get(EndPoints.anyendpoint).path("rootelement.findAll { element -> element.title.length() > 4 }"); //     sum, max, min     ,        String expensiveCar = get(EndPoints.cars).path("cars.find { it.title == 'Toyota Motor Corporation'}.models.max { it.averagePrice }.title"); 

使用Groovy中的方法可以大大减少您编写的从答案中查找所需值的代码量。

就这样,如果您有更多的提示和示例,请在评论中写下。

Source: https://habr.com/ru/post/zh-CN421005/


All Articles