目前,我正在进行有保证的api测试。我的示例网址为http://eampleUrl/register/device?s=123&m=23ewre&d=123 这里参数s =设备秘密,m是mac地址,d是设备id。 我想知道如何在上面的url中编写参数,在代码中以及如何使用dataprovider注释。以及如何检查assert命令?
{" msg":" REG_DEV_01"," level":" info"," meta":{这就是 成功回应
package com.qber.test;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;
/**
* Created by naveen on 14/12/16.
*/
public class testDemo {
@Test(dataProvider = "circuitLocations")
public void circuitLocationTest(String circuitId, String location) {
given().
pathParameters("circuitId",circuitId).
when().
get("http://ergast.com/api/f1/circuits/{circuitId}.json").
then().
assertThat().
body("MRData.CircuitTable.Circuits[0].Location.country",equalTo(location));
}
@DataProvider(name = "circuitLocations")
public static Object[][] createCircuitTestData() {
return new Object[][] {{"xt@xt.xom", "xtxtxt"}, {"xt@xt.xom", "xtxtxt"}};
}
}
答案 0 :(得分:0)
主原因是它无效,可能是通过DataProvider
提供的数据。
<强>附加强>:
statusCode(equalTo(200))
; extract()
以将字体提取为String。
然后可以显示。这是更正后的代码。
package com.qber.test;
import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.jayway.restassured.http.ContentType;;
public class DemoTest {
@Test(dataProvider = "circuitLocations")
public void circuitLocationTest(final String circuitId, final String location) {
System.out.println("circuitId '" + circuitId + "' location '" + location + "'");
final String body =
given()
.pathParameters("circuitId", circuitId)
.when()
.get("http://ergast.com/api/f1/circuits/{circuitId}.json")
.then()
.contentType(ContentType.JSON)
.assertThat()
.body("MRData.series", equalTo("f1"))
.body("MRData.CircuitTable.Circuits[0].circuitId", equalTo(circuitId))
.body("MRData.CircuitTable.Circuits[0].Location.country", equalTo(location))
.extract()
.body().asString();
System.out.println(body);
}
@DataProvider(name = "circuitLocations")
public static Object[][] createCircuitTestData() {
return new Object[][] {
{"monza", "Italy"}
, {"donington", "UK"}
};
}
}