我们如何使用基于Rest Assured的框架并行执行API测试用例GET / POST

问题描述

请为API请求找到以下基类。由于静态请求/响应,我无法执行并行测试执行。我的框架基于Cucumber,Junit,Java,Json的请求。

 public class APIreq {
private static Integer statusCode = 0;
private static String statusLine = null;
private static String resource = null;
protected static Response getResponse;
protected static Response postResponse;
public static JSONObject request = null;
public static JSONObject fileRequest = null;

protected static void postRequest(JSONObject requestBody,String endpoint) throws IOException {
    resource1 = endpoint;
    request = requestBody;
    HashMap<String,String> headerMap = new HashMap<>();
    headerMap.put( "Content-Type","application/json" );
    headerMap.put("Authorization",ABCD);

    try {
        postResponse = RestAssured.given()
                .log()
                .all()
                .headers( headerMap )
                .body( request.toString() )
                .post( Hooks.targetURL + resource1 )
                .then()
                .extract()
                .response();
        postResponse.prettyPrint();
        statusCode = postResponse.getStatusCode();
        statusLine = postResponse.getStatusLine();
    } catch (Exception e) {
        logger.error( "Error: " + e.getMessage() );
    }
}
public static void assertJsonPostReq(String attribute,String expectedValue) {
    io.restassured.path.json.JsonPath jsonPathEvaluator = postResponse.jsonPath();
    String attributeValue = jsonPathEvaluator.get(attribute);
    SYSO( attribute + ": Expected: " + expectedValue + ",Actual: " + attributeValue );
    Assert.assertTrue(attributeValue.contains(expectedValue));
}

}

解决方法

诚实的意见

如果您只是想尝试自动化并行API测试,那么最好尝试空手道,这可能会带来一些开销,但从长远来看还是值得的

Karate - Parallel Execution process

GitHub link

Example of Karate BDD test case scenarios

基于当前您将Cucumber用作测试框架的事实,由于Karate使用BDD语法,因此很容易理解。您还可以使用Cucumber HTML报告工具来创建报告。