Appearance
Getting started with Allure RSpec
Generate beautiful HTML reports using Allure Report and your RSpec tests.
INFO
Check out the example project at github.com/allure-examples/allure-rspec-example to see Allure RSpec in action.
Setting up
1. Prepare your project
Install the Allure Report command-line tool, if it is not yet installed in your operating system. Note that Allure Report requires Java, see the installation instructions.
Add Allure RSpec to your project's
Gemfile
. For example:rubysource 'https://rubygems.org' gem 'allure-rspec', '~> 2.23.0' gem 'rspec', '~> 3.12'
Open a terminal, go to the project directory and install the dependencies from the
Gemfile
. For example, if you use Bundler:bashcd /home/user/myproject bundle install
In the project's
.rspec
file, specifyAllureRspecFormatter
as the formatter for RSpec.plain--format AllureRspecFormatter
2. Run tests
Run your RSpec tests same way as your would run them usually. For example:
bash
bundle exec rspec
This will save necessary data into reports/allure-results
or other directory, according to the Configuration. If the directory already exists, the new files will be added to the existing ones, so that a future report will be based on them all.
3. Generate a report
Finally, run Allure to convert the test results into an HTML report. This will automatically open your browser to view the report.
bash
allure serve reports/allure-results
If necessary, replace reports/allure-results
with the path to the directory specified in the Configuration.
There are some options that can affect how the report is generated. Run allure --help
for the full list of options.
Writing tests
The Allure RSpec adapter extends the standard reporting features of RSpec by providing additional capabilities for crafting more informative and structured tests. This section highlights key enhancements that can be utilized:
- Metadata Annotation: Enhance test reports with descriptions, links, and other metadata.
- Test Organization: Structure your tests into clear hierarchies for better readability and organization organize tests.
- Step Division: Break down tests into smaller test steps for easier understanding and maintenance.
- Parametrized Tests: Clearly describe the parameters for parametrized tests to specify different scenarios.
- Attachments: Automatically capture screenshots and other files during test execution.
- Test Selection: Use a test plan file to select which tests to run, allowing for flexible test execution.
- Environment Details: Include comprehensive environment information to accompany the test report.
Adding Metadata
Allure allows you to enrich your reports with a variety of metadata. This additional information provides context and details for each test, enhancing the report's usefulness. Refer to the metadata reference section for an exhaustive list of what can be added.
ruby
describe 'Test my website' do
it 'test authentication', issue: 'AUTH-123' do
Allure.description_html 'This test attempts to log into the website using a login and a password.'
Allure.label 'owner', 'John Doe'
# ...
end
end
Organize tests
As described in Improving navigation in your test report, Allure supports multiple ways to organize tests into hierarchical structures.
To specify a test's location in the behavior-based hierarchy:
ruby
describe 'Test my website' do
it 'test authentication' do
Allure.epic 'Web interface'
Allure.feature 'Essential features'
Allure.story 'Authentication'
# ...
end
end
ruby
describe 'Test my website' do
it 'test authentication',
epic: 'Web interface',
feature: 'Essential features',
story: 'Authentication' do
# ...
end
end
To specify a test's location in the suite-based hierarchy:
ruby
describe 'Test my website' do
it 'test authentication' do
Allure.label 'parentSuite' 'Web interface'
Allure.suite 'Essential features'
Allure.label 'subSuite', 'Authentication'
# ...
end
end
Divide a test into steps
Allure RSpec provides three ways of creating steps and sub-steps: “annotated steps”, “block-based steps” and “no-op steps”, see the reference.
ruby
describe 'Test my website' do
it 'test authentication' do
Steps.step_1
Steps.step_2
end
end
class Steps
extend AllureStepAnnotation
step 'Step 1'
def self.step_1
step_1_1
step_1_2
end
step 'Step 1.1'
def self.step_1_1
# ...
end
step 'Step 1.2'
def self.step_1_2
# ...
end
step 'Step 2'
def self.step_2
step_2_1
step_2_2
end
step 'Step 2.1'
def self.step_2_1
# ...
end
step 'Step 2.2'
def self.step_2_2
# ...
end
end
ruby
describe 'Test my website' do
it 'test authentication' do
Allure.run_step 'Step 1' do
# ...
Allure.step name: 'Step 1.1', status: :passed
# ...
Allure.step name: 'Step 1.2', status: :passed
end
Allure.run_step 'Step 2' do
# ...
Allure.step name: 'Step 2.1', status: :passed
# ...
Allure.step name: 'Step 2.2', status: :passed
end
end
end
Describe parametrized tests
With Allure RSpec, it is very easy to implement the parametrized tests pattern, i.e. to run the same test logic with different test data. To do so, just write the test inside a loop and use the variable parameters in both its title and its body.
To display a parameter value in the test report, pass it to the Allure.parameter()
function.
ruby
describe 'Test my website' do
auth_data = [
['johndoe', 'qwerty'],
['[email protected]', 'qwerty'],
]
auth_data.each do |login, password|
it "Test authentication as '#{login}'" do
Allure.parameter 'Login', login
Allure.parameter 'Password', password
# ...
end
end
end
Attach screenshots and other files
In Allure reports, you have the ability to attach various types of files, which can greatly enhance the comprehensibility of the report. A common practice is to attach screenshots that capture the state of the user interface at specific moments during test execution.
For detailed instructions on how to implement attachments, refer to the attachments section in the Allure RSpec reference.
ruby
describe 'Test my website' do
it 'test authentication' do
# ...
Allure.add_attachment name: 'Screenshot',
source: File.new('/path/to/image.png'),
type: Allure::ContentType::PNG
Allure.add_attachment name: 'Data',
source: 'This is the file content.',
type: Allure::ContentType::TXT
end
end
Select tests via a test plan file
If the ALLURE_TESTPLAN_PATH
environment variable is defined and points to an existing file, RSpec will only run tests listed in this file.
Here's an example of running tests according to a file named testplan.json
:
bash
export ALLURE_TESTPLAN_PATH=testplan.json
bundle exec rspec
plain
setx ALLURE_TESTPLAN_PATH "testplan.json"
bundle exec rspec
Environment information
For the main page of the report, you can collect various information about the environment in which the tests were executed. To do so, specify the information in the environment_properties
configuration parameter.
For example, it is a good idea to use this to remember the OS version and Ruby version. This may help the future reader investigate bugs that are reproducible only in some environments.
ruby
require 'allure-rspec'
AllureRspec.configure do |config|
config.environment_properties = {
os_platform: RbConfig::CONFIG['host_os'],
ruby_version: RUBY_VERSION,
}
end
Note that if your launch includes multiple RSpec runs (see How it works), Allure RSpec will only save the environment information from the latest run.