在编写自动化 REST API 测试方面需要帮助?我该如何改进?

Need help on writing automated REST API tests ? How can I improve?

我正在尝试为我的 REST API

改进编写功能性自动化测试

这是一个示例:

Q1 有人可以批评我的测试或看看是否有改进的余地吗?

Q2 我还计划在 AppTest class 中编写一系列 API 测试...有没有一种方法或函数或实用程序可以将响应存储在当我执行一系列 API 测试时文件或其他东西?

 package com.jaway.restassured.rest_assured;

import static com.jayway.restassured.RestAssured.get;

import java.util.List;

import org.json.JSONException;
import org.json.JSONArray;
import org.testng.Assert;
import org.testng.*;
import org.testng.annotations.Test;
import org.testng.annotations.*;
import org.testng.annotations.DataProvider;
import org.testng.asserts.SoftAssert;


import com.jayway.restassured.response.*;

public class AppTest {

    String url = "http://restcountries.eu/rest/v1/name/";

    @Test(dataProvider = "getData")
    public void getRequestFindCapital(String country, String expected_capital, String expected_region, String expected_trans_it ) throws JSONException{

        SoftAssert softAssert = new SoftAssert();
        //Make a request to fetch the capital of norway
        Response resp = get(url + country);
        System.out.println(url + country);
        JSONArray jsonResponse =new JSONArray(resp.asString());
        System.out.println(resp.asString());

        //Declare variables
        String actual_capital = jsonResponse.getJSONObject(0).getString("capital"); 
        String actual_region = jsonResponse.getJSONObject(0).getString("region");
        List<Object> actual_translations = resp.jsonPath().getList("translations.it");
        String actual_translations_string  = actual_translations.toString().replaceAll("[\[\]]", "");
        System.out.println(actual_translations);
        System.out.println(actual_translations_string);

        softAssert.assertEquals(actual_capital, expected_capital);
        softAssert.assertEquals(actual_region, expected_region);
        softAssert.assertEquals(actual_translations_string, expected_trans_it);
        softAssert.assertAll();
    }

     @DataProvider
         public Object[][] getData() {
             return new Object[][]{
                 {"Norway", "Oslo","Europe", "Norvegia"}, 
                 {"Britain", "London","Europe","Regno Unito"}, 
                 {"Bangladesh","Dhaka","Asia","Bangladesh"}};
         }

}
  1. 你只会走幸福的路。因此,为了改进您的测试,您可能需要添加一些错误的输入并验证您是否会收到正确的错误。例如,您可以提供不存在的国家并验证答案。 通过这种方式,您可以添加一些功能测试。 我建议还添加一些非功能性测试,例如性能,以衡量您的 REST 延迟。
  2. 很少有实用程序可以保存 REST 调用和响应。我最喜欢的是 Chrome 的 PostMan 插件(只需在 Chrome 商店中搜索)。

您可能还想查看 Cucumber 编写功能测试的框架,它大大简化了测试用例的编写。我个人感觉JUnit或者TestNG只适合需要stubbing/mocking的单元测试。

您将能够用通俗易懂的英语编写测试用例,例如:

Feature: Countries API
  Scenario: Retrieve Capital City
    Given the specified country is "United States"
    When I make the API request
    Then the response should be valid JSON
     And it should contain property "city" with value "Washington, D.C."

然后您可以使用任何 Cucumber 实现为这些英语句子编写实现。在这种情况下,我最喜欢的是 Node.js,但它们有多种实现方式。这种方法的优点是现在您甚至可以让团队中的非技术成员编写端到端测试用例。

我通常用 OpenEJB 或 Arquillian 编写我的 ITest。使用 OpenEJB,测试 class 可以在启动测试之前启动嵌入式容器。您可以在此处查看示例:http://openejb.apache.org/examples-trunk/index.html。 JEE6兼容吗,JEE7还没发布...