How to ignore or skip a test method using RSpec? - ruby

please guide how to disable one of the below test methods using RSpec. I am using Selenuim WebDriver + RSpec combinations to run tests.
require 'rspec'
require 'selenium-webdriver'
describe 'Automation System' do
before(:each) do
###
end
after(:each) do
#driver.quit
end
it 'Test01' do
#positive test case
end
it 'Test02' do
#negative test case
end
end

You can use pending() or change it to xit or wrap assert in pending block for wait implementation:
describe 'Automation System' do
# some code here
it 'Test01' do
pending("is implemented but waiting")
end
it 'Test02' do
# or without message
pending
end
pending do
"string".reverse.should == "gnirts"
end
xit 'Test03' do
true.should be(true)
end
end

Another way to skip tests:
# feature test
scenario 'having js driver enabled', skip: true do
expect(page).to have_content 'a very slow test'
end
# controller spec
it 'renders a view very slow', skip: true do
expect(response).to be_very_slow
end
source: rspec 3.4 documentation

Here is an alternate solution to ignore (skip) the above test method (say, Test01) from sample script.
describe 'Automation System' do
# some code here
it 'Test01' do
skip "is skipped" do
###CODE###
end
end
it 'Test02' do
###CODE###
end
end

Pending and skip are nice but I've always used this for larger describe/context blocks that I needed to ignore/skip.
describe Foo do
describe '#bar' do
it 'should do something' do
...
end
it 'should do something else' do
...
end
end
end if false

There are a number of alternatives for this. Mainly marking it as pending or skipped and there is a subtle difference between them. From the docs
An example can either be marked as skipped, in which is it not executed, or pending in which it is executed but failure will not cause a failure of the entire suite.
Refer the docs here:
https://relishapp.com/rspec/rspec-core/v/3-4/docs/pending-and-skipped-examples/pending-examples
https://relishapp.com/rspec/rspec-core/v/3-4/docs/pending-and-skipped-examples/skip-examples

There are two ways to skip a specific block of code from being running while testing.
Example : Using xit in place of it.
it "redirects to the index page on success" do
visit "/events"
end
Change the above block of code to below.
xit "redirects to the index page on success" do #Adding x before it will skip this test.
visit "/event"
end
Second way: By calling pending inside the block.
Example:
it "should redirects to the index page on success" do
pending #this will be skipped
visit "/events"
end

Related

Run after block after specific test in Rspec

Is there a way I can run the after/before block after/before a specific test using labels?
I have 3 it blocks
describe "describe" do
it "test1" do
end
it "test2" do
end
after(<<what goes here??>>) do
end
end
How do I run the after block only after test2? Is that possible?
You should use contexts to do this. Something like:
describe "describe" do
context 'logged in' do
before(:each) do
# thing that happens in logged in context
end
after(:each) do
# thing that happens in logged in context
end
it "test1" do
end
end
context 'not logged in' do
# No before/after hooks here. Just beautiful test isolation
it "test2" do
end
end
end
Having if/else conditions in before/after blocks is a code smell. Don't do it that way. It'll only make your tests brittle, error prone, and hard to change.
The best way to do this is just use a context. For your example:
describe "AutomateFr33k's fr33ky tests" do
it "runs test1" do
expect(true).to be_true
end
context "do something afterwards" do
after { puts "running something after test2!" }
it "runs test2" do
expect(5).not_to eq(4)
end
end
end
Yes you can do that, have a look here
You can achieve that using metadata in rspec
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
end
describe "Skip hook demo" do
# If prior to RSpec 2.99.0.beta1
after do
puts "before hook" unless example.metadata[:skip]
end
# If RSpec 2.99.0.beta1 or later
after do |example|
puts "before hook" unless example.metadata[:skip]
end
it "will use before hook" do
end
it "will not use before hook", :skip do
end
end

Run cleanup step if any it block failed

When one of my it blocks fails, I want to run a cleanup step. When all of the it blocks succeed I don't want to run the cleanup step.
RSpec.describe 'my describe' do
it 'first it' do
logic_that_might_fail
end
it 'second it' do
logic_that_might_fail
end
after(:all) do
cleanup_logic if ONE_OF_THE_ITS_FAILED
end
end
How do I implement ONE_OF_THE_ITS_FAILED?
Not sure if RSpec provides something out of the box, but this would work:
RSpec.describe 'my describe' do
before(:all) do
#exceptions = []
end
after(:each) do |example|
#exceptions << example.exception
end
after(:all) do |a|
cleanup_logic if #exceptions.any?
end
# ...
end
I digged a little into the RSpec Code and found a way to monkey patch the RSpec Reporter class. Put this into your spec_helper.rb:
class RSpecHook
class << self
attr_accessor :hooked
end
def example_failed(example)
# Code goes here
end
end
module FailureDetection
def register_listener(listener, *notifications)
super
return if ::RSpecHook.hooked
#listeners[:example_failed] << ::RSpecHook.new
::RSpecHook.hooked = true
end
end
RSpec::Core::Reporter.prepend FailureDetection
Of course it gets a little more complex if you wish to execute different callbacks depending on the spec you're running at the moment.
Anyway, this way you do not have to mess up your testing code with exceptions or counters to detect failures.

RSpec before blocks not being called before contexts or describes

Given the following code:
RSpec.configure do |config|
config.before(:all) { puts 'before all' }
config.before(:suite) { puts 'before suite'}
config.before(:context) { puts 'before context'}
config.before(:each) { puts 'before each'}
end
RSpec.describe "SomeClass" do
it 'matches some regex' do
puts 'in first it block'
expect('some string').to match(/.*/)
end
describe 'some group of tests' do
puts 'in some group'
context 'when some thing happens' do
puts 'in context'
it 'does something' do
expect(true).to be_truthy
end
end
end
end
I would expect the following output:
before suite
before all
before context
before each
in some group
in context
in first it block
.before each
But instead I get:
in some group
in context
before suite
before all
before context
before each
in first it block
.before each
Meaning that context or describe gets run before any before configuration I've set up.
I expect it to be the first output because of what I've read here and here.
What do I do when I absolutely need code to run before absolutely anything else in the test files? Including (nested) context or describes? And why doesn't it work the way I expect?
Note: I see the same behavior when I include the before :something statements within the scope of the uppermost describe.
(This question is similar to this question, but not the same. I would like to know why my tests are running this way and what the proper RSpec convention is to run a piece of code before absolutely anything else.)
Version info:
RSpec 3.6
- rspec-core 3.6.0
- rspec-expectations 3.6.0
- rspec-mocks 3.6.0
- rspec-support 3.6.0
UPDATE:
It may be helpful to know some context: I'm writing selenium front end automated tests using the selenium-webdriver gem. Before any and all it blocks run, I need to call a function called navigate() (in order to take me to the web page I'm writing the tests for, this function takes about 30 seconds to run because it takes me through two login pages before it gets to where it needs to go) to be called and complete before anything else happens. In my RSpec file I'm using before blocks in an attempt to make this happen, however rspec keeps running tests before the before blocks, and failing.
If you were to put puts "in some group" and puts "in context" into before(:all) blocks, then the output is closer to what you're expecting.
RSpec.configure do |config|
config.before(:all) { puts 'before all' }
config.before(:suite) { puts 'before suite'}
config.before(:context) { puts 'before context'}
config.before(:each) { puts 'before each'}
end
RSpec.describe "SomeClass" do
it 'matches some regex' do
puts 'in first it block'
expect('some string').to match(/.*/)
end
describe 'some group of tests' do
before(:all) { puts 'in some group' }
context 'when some thing happens' do
before(:all) { puts 'in context' }
it 'does something' do
expect(true).to be_truthy
end
end
end
end
outputs
before suite
before all
before context
before each
in first it block
.in some group
in context
before each
.
or, if you did before(:each) you would get
before suite
before all
before context
before each
in first it block
.before each
in some group
in context
.
The reason for the current output is your puts statements for "in some group" and "in context" are being executed when the file is being parsed, not waiting for RSpec at all. If we gave a different example, without Rspec in the mix, imagine we had a file with just
class SomeClass
puts "in class"
def do_something
puts "doing something"
end
end
if we load that file into an irb session or run it on the command line with ruby, we would see "in class" output in the console even though we haven't done anything with that class.

Detect Rspec test failure on after each method

I am trying to run an RSpec test, and I want to detect if the test failed in the after method. I have something like this right now:
after(:each) do
cc = ConnectController.new()
cc.update(<TEST-SERVER-CONTROLLER>, <TC-RESULT-ID>, result?)
end
As you can see, the result? function is what I need to replace, to detect if the test fails or not, and to also get information about the test that failed.
In addition to Daniel's answer, in Rspec3 the example method was deleted (see here for more info).
You will have to do something like this:
after(:each) do |example|
if example.exception
# ...
end
end
EDIT: this answer is only valid for RSpec 2. for RSpec 3 see geekazoid's answer.
The after each block runs in the context of class which exposes example and you can detect failures by checking the exception method on example thusly:
after(:each) do
if example.exception != nil
# Failure only code goes here
end
end
I was looking for how to check if success for all examples in a group in a after(:context) / after(:all) block. Here's what I came up with:
after(:all) do |example_group|
all_groups = example_group.class.descendants
failed_examples = all_groups.map(&:examples).flatten.select(&:exception)
if failed_examples.empty?
# runs only if there are no failures
do('something')
end
end

How to verify that "puts" has been called with a certain message?

I'm trying to make this test fail :)
it "should display the question" do
#ui.should_receive(:puts).with("What's your name?").once
#ui.ask_question("What's your name?")
end
At the moment it passes even if I don't call puts in my function.
Basically, #ui should call .puts on an object that probably defaults to $stdout. Then in your tests, you can replace $stdout with a StringIO object that you can set expectations on. This has the added benefit of making your #ui object more flexible.
Given the code:
require 'rubygems'
require 'spec'
class UI
def ask_question(q)
end
end
describe UI do
before do
#ui = UI.new
end
it "should display the question" do
#ui.should_receive(:puts).with("Whats your name?").once
#ui.ask_question("Whats your name?")
end
end
I get the failure:
F
1) Spec::Mocks::MockExpectationError in 'UI should display the question'
#<UI:0xb738effc> expected :puts with ("Whats your name?") once, but received it 0 times /home/avdi/tmp/puts_spec.rb:15:
Finished in 0.002575 seconds
1 example, 1 failure
What version of RSpec are you using?
You can try stringio or ZenTest, the following ruby-talk thread has more info.

Resources