Can you save a result (Given) to a variable in a Gherkin feature file, and then compare the variable with another result (Then)? (Cucumber for Java)

 ̄綄美尐妖づ 提交于 2021-01-28 08:58:10

问题


I am new to Cucumber for Java and trying to automate testing of a SpringBoot server backed by a MS SQL Server.

I have an endpoint "Get All Employees".

Writing the traditional feature file, I will have to list all the Employees in the @Then clause. This is not possible with thousands of employees.

So I just want to get a row count of the Employee table in the database, and then compare with the number of objects returned from the "Get All Employees" endpoint.

Compare

SELECT count(*) from EMPLOYEE

with size of the list returned from

List<Employee> getAllEmployees()

But how does one save the rowcount in a variable in the feature file and then pass it into the stepdefs Java method?

I have not found any way that Gherkin allows this.


回答1:


You could create a package for example named dataRun (with the corresponding classes in the package) and save there the details during the test via setters.

During the execution of the step "And I get the count of employees from the data base" you set this count via the corresponding setter, during the step "And I get all employees" you set the number via the dedicated setter. Then during the step "And I verify the number of employees is the same as the one in the data base" you get the two numbers via the getters and compare them.

Btw it's possible to compare the names of the employees (not just the count) if you put them into list and compare the lists.




回答2:


After writing a few scenario and feature files, I understood this about Cucumber and fixed the issue.

Gherkin/Cucumber is not a programming language. It is just a specification language. When keywords like Given, Then are reached by the interpreter, the matching methods in Java code are called. So they are just triggers.

These methods are part of a Java glue class. Data is not passed out of the Java class and into the gherkin feature file. The class is instantiated at the beginning and retained until the end. Because of this, it can store state.

So from my example in the question above, the Then response from a Spring endpoint call will be stored in a member variable in the glue class. The next Then invocation to verify the result will call the corresponding glue method which will access the data in the member variable to perform the comparison.

So Gherkin cannot do this, but Java at a lower level in the glue class, can.



来源:https://stackoverflow.com/questions/61663439/can-you-save-a-result-given-to-a-variable-in-a-gherkin-feature-file-and-then

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!