如何在 Cucumber 中预期失败的步骤并传递失败?
我们想要测试黄瓜的步骤定义。我们希望能够检查的一件事是我们期望失败的测试实际上确实失败了。为此,我们希望编写我们知道会失败的场景,并将它们添加到我们的测试套件中,但标记或以其他方式表示它们,以便当且仅当它们失败时它们“通过”。人们将如何处理这一问题?
We want to test our step definitions for cucumber. One thing we would like to be able to check is that tests which we expect to fail actually do fail. To do this, we would like to write scenarios that we know will fail and add them to our test suite, but tag or otherwise denote them so that they "pass" if and only if they fail. How would one approach this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该测试负面状态。失败的步骤只是通过步骤的逆过程。因此,请执行以下操作:
这就是我测试失败的方式。您还可以捕获异常等,并验证块实际上是否抛出该异常。
您只需反转测试用例中的测试即可测试负面结果,而不是正面结果。
You should be testing for the negative state. A failing step is simply the inverse of a passing step. So do something like:
That is how I would go about testing for failure. You can also catch exceptions and such, and verify that a block does in fact throw that exception
You simply reverse the tests in your test cases to test for negative results, not positive results.
这是一个非常复杂的示例,但最终结果是一个非常干净的方法,可以预期黄瓜场景会失败。这些只是我正在从事的项目中的几个小组件。创建具有丢失数据的用户失败的原因是因为我的用户模型中有一些验证器。所有源代码都可以在此处找到。
特征/step_definitions/before_step.rb
特征/step_definitions/user_step.rb
特征/user.feature
This is a pretty complex example, but the end result is a really clean method to expect cucumber scenarios to fail. These are just a few small components from a project I am working on. The reason creating a user with the missing data fails is because there are some validators in my user model. All the source code can be found here.
features/step_definitions/before_step.rb
features/step_definitions/user_step.rb
features/user.feature
您将
-w
开关传递到 Cucumber 命令中。它将输出正常格式,但最后它将给出一个摘要,详细说明所有测试用例是否失败,如果有任何通过,它将指定哪些测试用例。
You pass the
-w
switch into the Cucumber command.It will output the normal format however at the end it will give a summary detailing whether all test cases failed and if any passed it will specify which ones.