您可以将结果(Given)保存到Gherkin功能文件中的变量,然后将该变量与另一个结果(Then)进行比较吗?(适用于Java的黄瓜) [英] 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)

查看:65
本文介绍了您可以将结果(Given)保存到Gherkin功能文件中的变量,然后将该变量与另一个结果(Then)进行比较吗?(适用于Java的黄瓜)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触Cucumber for Java,并试图自动测试由MS SQL Server支持的SpringBoot服务器.

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".

编写传统功能文件,我将不得不在 @Then 子句中列出所有雇员.数千名员工无法做到这一点.

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.

所以我只想获取数据库中Employee表的行数,然后与从"Get All 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.

比较

从EMPLOYEE中选择SELECT(*)

具有从中返回的列表大小

with size of the list returned from

列表< Employee>getAllEmployees()

但是如何将行计数保存在功能文件的变量中,然后将其传递到 stepdefs Java方法中?

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.

推荐答案

在编写了一些方案和功能文件后,我了解了有关Cucumber的知识并解决了该问题.

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

小黄瓜/黄瓜不是一种编程语言.它只是一种规范语言.当解释器访问诸如 Given Then 之类的关键字时,将调用Java代码中的匹配方法.因此,它们只是触发器.

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.

这些方法是Java glue 类的一部分.数据不会从Java类传递到小黄瓜功能文件中.该类在开始时实例化,并保留到结束.因此,它可以存储状态.

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.

因此,在上述问题的示例中,Spring端点调用的 Then 响应将存储在 glue 类.用于验证结果的下一个 Then 调用将调用相应的 glue 方法,该方法将访问成员变量中的数据以执行比较.

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.

因此,Gherkin无法做到这一点,但是在 glue 类中较低级别的Java可以.

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

这篇关于您可以将结果(Given)保存到Gherkin功能文件中的变量,然后将该变量与另一个结果(Then)进行比较吗?(适用于Java的黄瓜)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆