许多 GET 请求对 REST 放心 - JAVA

Many GET request on REST assured - JAVA

我有一些 url 的列表字符串:

可以有 2 个、5 个或更多 url。我必须检查哪些地址以错误 500 响应我。

@Test()  //bad test
public void checkErrorStatusForURLs() {
    for(String url : urlList()) {
        given().when().get(url)
               .then().statusCode(CoreMatchers.not(500));
    }
}

我不想为每个 url 编写测试。我可以在一次测试中完成吗?如何正确操作?

您可以提取 statusCode 然后检查您想要的每个条件,如下所示:

for(String url : urlList()) {
    int statusCode = given().when().get(url).then().extract().statusCode();
    if(500 == statusCode) {
        //add it to a list??
    }
}

查看此处所述的 Junit 4 参数化测试:https://github.com/junit-team/junit4/wiki/parameterized-tests

与此类似的内容可能对您有用:

@RunWith(Parameterized.class)
public class FibonacciTest {

@Parameters
public static Iterable<? extends Object> urls() {
    return Arrays.asList("localhost:80/my/first/url", "localhost:80/my/second/url" );
}

@Parameter 
public /* NOT private */ String url;

@Test
public void checkErrorStatusForURLs() {
        given().when().get(url)
               .then().statusCode(CoreMatchers.not(500));
 ...
}
}