寻求将动态数据作为 JSONPath 请求传递。

huangapple go评论143阅读模式
英文:

Looking to pass dynamic data as jsonpath request

问题

@Test
public void basicAuthLogin() {
//language=JSON
String jsonBody = "{\n" +
" "name": "Foo"\n" +
"}";

given().auth().preemptive().basic(username, password)
        .body(jsonBody)
        .contentType(ContentType.JSON)
        .when()
        .post("http://localhost:8080/secured/hello")
        .then()
        .statusCode(200);

}
我想要传递动态数据而不是 "Foo"。我该如何做到这一点?

英文:
 @Test
public void basicAuthLogin() {
    //language=JSON
    String jsonBody = "{\n" +
            "  \"name\": \"Foo\"\n" +
            "}";

    given().auth().preemptive().basic(username, password)
            .body(jsonBody)
            .contentType(ContentType.JSON)
            .when()
            .post("http://localhost:8080/secured/hello")
            .then()
            .statusCode(200);
}

I wanted to pass dynamic data for name instead of "Foo". How can I do that?

答案1

得分: 1

你需要在这里使用DataProvider。它实际上在运行时为你的测试用例提供数据。在测试用例之前、之后和测试用例期间提供动态数据有多种方法。

  • 如果你使用JUnit,可以使用@RunWith(Parameterized.class),然后使用@Parameters提供数据。

  • 如果你使用TestNG,可以使用@DataProvider(name = “name_of_dataprovider”),然后创建数据提供方法,并在你的测试用例中添加属性@Test(dataProvider = "data-provider")

  • 你可以从文件中提供输入,可以使用objectMapperJackson库编写通用映射器来获取数据并在运行时转换为对象,然后将其用作输入参数。

英文:

You need to use DataProvider here. Which actually provides data to your test case in the runtime. There are multiple ways to provide dynamic data to the test case before, after, and during the test case.

  • Use @RunWith(Parameterized.class) in case if you are using JUnit and you can provide data with @Parameters

  • Use @DataProvider (name = “name_of_dataprovider”) and create data-provider method in case if you are using TestNG and you can add property @Test (dataProvider = "data-provider") in your testCase.

  • You can provide input from a file, you can write generic mapper with the help of objectMapper or Jackson lib fetch data and convert into the object at runtime and use it as an input parameter.

huangapple
  • 本文由 发表于 2020年8月12日 21:41:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63377796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定