开发者

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开发者_JAVA百科 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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜