_ruby
Example
In RSpec, this metadata and filtered execution is referred to as tags (or tagging).
A tag is a simple key/value pair that can be specified within any test file. Alternatively, you can specify just a key (as a symbol) for the tag.
Here is an example spec file with a :smoke
tag applied to the entire file.
# filename: spec/example_spec.rb
require 'selenium-webdriver'
describe 'Login', :smoke do
before(:each) do
@driver = Selenium::WebDriver.for :firefox
end
after(:each) do
@driver.quit
end
it 'succeeded' do
# test code goes here
end
it 'failed' do
# test code goes here
end
end
To run this, you would specify --tag
and the tag name (without the :
) as a runtime parameter, like so:
rspec --tag smoke
Alternatively, you can ignore tags with a ~
prepended to the tag name.
rspec --tag ~smoke
You can also tag each individual test and specify multiple tags at run time.
# filename: spec/example_spec.rb
require 'selenium-webdriver'
describe 'Login' do
before(:each) do
@driver = Selenium::WebDriver.for :firefox
end
after(:each) do
@driver.quit
end
it 'succeeded', :smoke do
# test code goes here
end
it 'failed', :wip do
# test code goes here
end
end
To run just the smoke tag and negate the wip tag, you would do the following:
rspec --tag smoke --tag ~wip
Please note that if you're running RSpec 2 or earlier, the following config is required:
require 'rspec'
RSpec.configure { |c| c.treat_symbols_as_metadata_keys_with_true_values = true }
Otherwise, you'll have to specify tags using the standard key/value syntax (e.g., :smoke
would be smoke: true
).
Expected Behavior
- Run the test suite
- The test suite identifies which tests to run based on the tags provided
- Executes the filtered set of tests, ignoring the rest
Summary
By utilizing metadata tags and filtering a set of tests, you have learned to effectively create a dynamic test suite that takes into account all of your tests.
Happy Testing!