gpt4 book ai didi

cucumber - 在 cucumber 的特征文件中传递对象的名称和列表

转载 作者:行者123 更新时间:2023-12-04 06:32:33 25 4
gpt4 key购买 nike

我想在 cucumber 的功能文件中传递这样的东西

Feature: Testing different requests on the XLR CD API
Scenario: Check if the student application can be accessed by users
Scenario Outline: Create a new student & verify if the student is added
When I create a new student by providing the information studentcollege <studentcollege> studentList <studentList>
Then I verify that the student with <student> is created
Examples:
| studentcollege | studentList |
| abcd | [{student_name": "student1","student_id": "1234"},{student_name": "student1","student_id": "1234"}] |

我有课
Class Student{
String name;
String id;
}

步骤定义文件是
    @When("^When I create a new student by providing the information studentCollege (.*) studentList (.*)$")
public void generatestudent(String studentOwner, List<Student> listOfstudent) {
// need to fetch values in here from whatever is given in feature file

}

如何在功能文件示例中传递这些值。以便可以在步骤定义函数中检索。

最佳答案

这可以通过使用 @Transform 来完成。步骤定义中的注释。此外,功能文件中的学生列表字符串看起来像 Json字符串,因此使用 Gson 最容易解析。

相关场景

  Scenario Outline: Create a new student & verify if the student is added
When I create a new student by providing the information studentcollege <studentcollege> studentList <studentList>

Examples:
| studentcollege | studentList |
| abcd | [{"student_name": "student111","student_id": "1234"},{"student_name": "student222","student_id": "5678"}] |

Stef定义类
@When("^I create a new student by providing the information studentcollege (.*?) studentList (.*?)$")
public void iCreateANewStudentByProvidingTheInformation(String arg1, @Transform(StudentListTransformer.class)List<Student> arg3) {
System.out.println(arg1);
System.out.println(arg3);
}

变压器类
public class StudentListTransformer extends Transformer<List<Student>>{

@Override
public List<Student> transform(String value) {
//Sample json -- [{'name': 'student100','id': '1234'},{'name': 'student200','id': '5678'}]

return new Gson().fromJson(value, ArrayList.class);
}
}

学生数据对象-
public class Student {
private String name;
private String id;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + "]";
}
}

关于cucumber - 在 cucumber 的特征文件中传递对象的名称和列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49908720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com