用testng放心

时间:2016-12-14 04:38:26

标签: java testng rest-assured

目前,我正在进行有保证的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"}};
    }

}

1 个答案:

答案 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"}
            };
    }

}