简体   繁体   English

如何期待失败的步骤并传递黄瓜的失败?

[英]How to expect failed step and pass on failure in 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? 怎么会接近这个?

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 特征/ 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 特征/ 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 功能/ user.feature

Scenario: check if userers exsist
  Given a user with
    | username | johnsmith             |
    | email    | johnsmith@example.com |
    | 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"

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. 您只需在测试用例中反转测试以测试阴性结果,而不是阳性结果。

You pass the -w switch into the Cucumber command. -w开关传递给Cucumber命令。

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. 它将输出正常格式,但最后它将给出一个摘要,详细说明所有测试用例是否失败,如果有任何测试用例,它将指定哪些测试用例。

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

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