简体   繁体   English

Cucumber - 带参数的小黄瓜注释

[英]Cucumber - gherkin annotation with parameter

I would like to be able to pass a parameter using a gherkin scenario annotation.我希望能够使用小黄瓜场景注释传递参数。 For example:例如:

@xxx.1
Scenario: Log in and log out

I would like to have annotation @xxx which will get the number 1 as parameter.我想要注释@xxx,它将获得数字 1 作为参数。 So far i am only able to get all annotation and filter it one by one.到目前为止,我只能获取所有注释并一一过滤。 I also tried to search for some plugin which can do such thing, but no luck.我也尝试搜索一些可以做这种事情的插件,但没有运气。

Any ideas?有任何想法吗? Thx.谢谢。

If you add "annotations" (like @xxxx.1 in your example) above your scenario, Cucumber will interpret those as tags.如果您在场景上方添加“注释”(如示例中的@xxxx.1 ),Cucumber 会将这些解释为标签。 You can use tags to select which scenarios to run.您可以使用标签来 select 运行哪些场景。

In order to pass a variable from Gherkin to your step definition, you will have to set the variable in your step, and specify in your step definitions which type of variable to use and how.为了将变量从 Gherkin 传递到步骤定义,您必须在步骤中设置变量,并在步骤定义中指定要使用的变量类型和方式。

For example例如

Scenario: Log in and log out
  Given Log in and out as xxx.1

You step definition would be something like您的步骤定义将类似于

@Given("Log in and out as {string}")

public void loginLogout(String user) { // log in and out as that user } public void loginLogout(String user) { // 以该用户身份登录和注销 }

In addition, I'd consider describing more meaningful behaviour in your Cucumber scenarios.此外,我会考虑在您的 Cucumber 场景中描述更有意义的行为。 For example, I image a user of a certain type wants to log into the system to do something and you want to test that that works, or that it works only if they have a certain role.例如,我想象某个类型的用户想要登录系统做某事,而您想测试它是否有效,或者只有当他们具有特定角色时它才有效。

For example:例如:

Scenario: User wants to accomplish a task Given User has a registered account When they change their address Then their address is updated场景:用户想要完成一个任务给定用户有一个注册帐户当他们改变他们的地址然后他们的地址被更新

Scenario: Admin is allowed to do a thing Given User has admin access When they do the thing the admin is allowed to do Then they are allowed to do the thing场景:允许管理员做某事 给定用户具有管理员访问权限 当他们做某事时,管理员可以做某事 然后他们被允许做这件事

Scenario: Regular user is NOT allowed to do a thing Given User does not have admin access When they do the thing the admin is allowed to do Then they are not allowed to do the thing场景:不允许普通用户做某事给定用户没有管理员访问权限当他们做某事时管理员可以做然后他们不被允许做某事

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM