Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for failures in an RSpec test suite?

Tags:

ruby

rspec

I am experimenting with RSpec and considering a system that would change the random seed only when the test suite passes. I am trying to implement this within an after(:suite) block, which executes in the context of an RSpec::Core::ExampleGroup object.

While RSpec::Core::Example has a method "exception" that allows you to check whether any of the tests have failed, there does not seem to be a similar method on RSpec::Core::ExampleGroup or any accessor for the list of Examples. So, how can I check whether the tests have passed or failed?

I understand that this is possible using a custom formatter that keeps track of whether any tests have failed, but it seems like a bad idea for the formatting process to affect the actual running of the tests.

like image 950
pdg137 Avatar asked Sep 20 '13 18:09

pdg137


People also ask

Is RSpec BDD or TDD?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.

Does RSpec clean database?

I use the database_cleaner gem to scrub my test database before each test runs, ensuring a clean slate and stable baseline every time. By default, RSpec will actually do this for you, running every test with a database transaction and then rolling back that transaction after it finishes.

Is RSpec used for unit testing?

RSpec is a unit test framework for the Ruby programming language. RSpec is different than traditional xUnit frameworks like JUnit because RSpec is a Behavior driven development tool. What this means is that, tests written in RSpec focus on the "behavior" of an application being tested.

How do I run an RSpec on a specific file?

To run a single Rspec test file, you can do: rspec spec/models/your_spec. rb to run the tests in the your_spec. rb file.


1 Answers

I poked around in the RSpec source code and figured out that the following would work. Just put this code in spec_helper.rb or some other file that gets loaded when you run the tests:

RSpec.configure do |config|
  config.after(:suite) do
    examples = RSpec.world.filtered_examples.values.flatten
    if examples.none?(&:exception)
      # change the seed here
    end
  end
end

The RSpec.world.filtered_examples hash associates example groups to an array of examples from that group. Rspec has features for filtering out certain examples, and this hash appears to only contain the examples that were actually run.


One alternative way you could set up your system would be to check the return code of the rspec process. If it is 0, then all the tests passed and you can change the seed.

In a shell script, you could define a command that changes the seed and run:

rspec && change_seed

If your project has a Rakefile, you could set up something like this:

task "default" => "spec_and_change_seed"

task "spec" do
  sh "rspec spec/my_spec.rb"
end

task "spec_and_change_seed" => "spec" do
  # insert code here to change the file that stores the seed
end

If the specs fail, then rake's "spec" task will fail and it will not go on to change the seed.

like image 180
David Grayson Avatar answered Oct 24 '22 02:10

David Grayson