英文:
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")
。 -
你可以从文件中提供输入,可以使用
objectMapper
或Jackson
库编写通用映射器来获取数据并在运行时转换为对象,然后将其用作输入参数。
英文:
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 usingJUnit
and you can provide data with@Parameters
-
Use
@DataProvider (name = “name_of_dataprovider”)
and create data-provider method in case if you are usingTestNG
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
orJackson
lib fetch data and convert into the object at runtime and use it as an input parameter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论