如何在 Cucumber 中预期失败的步骤并传递失败?

发布于 2024-10-15 10:34:28 字数 132 浏览 4 评论 0原文

我们想要测试黄瓜的步骤定义。我们希望能够检查的一件事是我们期望失败的测试实际上确实失败了。为此,我们希望编写我们知道会失败的场景,并将它们添加到我们的测试套件中,但标记或以其他方式表示它们,以便当且仅当它们失败时它们“通过”。人们将如何处理这一问题?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光礼记 2024-10-22 10:34:28

您应该测试负面状态。失败的步骤只是通过步骤的逆过程。因此,请执行以下操作:

Then /i should not be true/ do
  some_value.should_not be_true
end

这就是我测试失败的方式。您还可以捕获异常等,并验证块实际上是否抛出该异常。

lambda do
  something_that_horks
end.should raise_error(Specific::Error)

您只需反转测试用例中的测试即可测试负面结果,而不是正面结果。

You should be testing for the negative state. A failing step is simply the inverse of a passing step. So do something like:

Then /i should not be true/ do
  some_value.should_not be_true
end

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

lambda do
  something_that_horks
end.should raise_error(Specific::Error)

You simply reverse the tests in your test cases to test for negative results, not positive results.

梦幻的味道 2024-10-22 10:34:28

这是一个非常复杂的示例,但最终结果是一个非常干净的方法,可以预期黄瓜场景会失败。这些只是我正在从事的项目中的几个小组件。创建具有丢失数据的用户失败的原因是因为我的用户模型中有一些验证器。所有源代码都可以在此处找到。

特征/step_definitions/before_step.rb

Before("~@fails") do
  def assert_cucumber(assersion, msg = "an error was thrown")
    assert(assersion == true, msg)
  end
end

Before("@fails") do
  def assert_cucumber(assersion, msg = "an error was thrown")
    assert(assersion == false, msg)
  end
end

特征/step_definitions/user_step.rb

Given /^a user with$/ do |params|
  params = params.rows_hash
  unless User.find_by({username: params[:username]})
    assert_cucumber(User.new(params).save, "could not create user")
  end
end

特征/user.feature

Scenario: check if userers exsist
  Given a user with
    | username | johnsmith             |
    | email    | [email protected] |
    | password | password              |
  Then a user with username "johnsmith"

@fails
Scenario: create user with missing data
  Given a user with
    | username | johndoe |
  Then a user with username "johndoe"

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

Before("~@fails") do
  def assert_cucumber(assersion, msg = "an error was thrown")
    assert(assersion == true, msg)
  end
end

Before("@fails") do
  def assert_cucumber(assersion, msg = "an error was thrown")
    assert(assersion == false, msg)
  end
end

features/step_definitions/user_step.rb

Given /^a user with$/ do |params|
  params = params.rows_hash
  unless User.find_by({username: params[:username]})
    assert_cucumber(User.new(params).save, "could not create user")
  end
end

features/user.feature

Scenario: check if userers exsist
  Given a user with
    | username | johnsmith             |
    | email    | [email protected] |
    | password | password              |
  Then a user with username "johnsmith"

@fails
Scenario: create user with missing data
  Given a user with
    | username | johndoe |
  Then a user with username "johndoe"
青瓷清茶倾城歌 2024-10-22 10:34:28

您将 -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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文