【问题标题】:Rest-assured with testng放心使用 testng
【发布时间】:2016-12-14 04:38:25
【问题描述】:

目前我正在使用放心的 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"}};
    }

}

【问题讨论】:

  • 不清楚您的要求:如何使用DataProvider 传递参数或如何在 RA 中进行断言?请先编辑您的问题并阅读文档。

标签: java testng rest-assured


【解决方案1】:

它不起作用的主要原因可能是通过DataProvider提供的数据。

额外

  • 为返回的statusCode(equalTo(200)) 添加检查;
  • 添加了更多的正文消息验证;
  • 添加extract() 以将正文提取为字符串。 然后可以显示。

这是更正后的代码。

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

}

【讨论】:

    猜你喜欢
    • 2021-06-25
    • 2014-01-21
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多