I’m integrating allure reports with my API test project using Rspec.
I wanted to attach request and response details to the report after each testcase.
- The following is my
response_parser
to get needed request and response details.
module ResponseParser
extend self
def get_request_body(response)
request_body = response[:request].body
puts "#{request_body.class}"
request_body
end
def get_request_headers(response)
headers = []
response[:request].each_header do |header, value|
headers << "#{header}: #{value}"
end
puts "#{headers.class}"
headers
end
def get_request_method(response)
request_method = response[:request].method
puts "#{request_method.class}"
request_method
end
def get_request_uri(response)
request_uri = response[:request].uri
puts "#{request_uri.class}"
request_uri
end
def get_response_body(response)
response_body = JSON.parse(response[:response].body)
puts "#{response_body.class}"
response_body
end
def get_response_headers(response)
headers = []
response[:response].each_header do |header, value|
headers << "#{header}: #{value}"
end
puts "#{headers.class}"
headers
end
def get_response_status(response)
{
code: response[:response].code.to_i,
message: response[:response].message
}
end
end
and I created an allure_helper
to any customizations to the report
# spec/allure_helper.rb
require "allure-rspec"
require_relative '../lib/response_parser'
module AllureHelper
extend self
def attach_request_details_to_report(response)
Allure.add_attachment(
name: 'request method',
source: ResponseParser.get_request_method(response),
type: Allure::ContentType::TXT,
test_case: true
)
Allure.add_attachment(
name: 'request uri',
source: ResponseParser.get_request_uri(response),
type: Allure::ContentType::TXT,
test_case: true
)
Allure.add_attachment(
name: 'request headers',
source: ResponseParser.get_request_headers(response).join("n"),
type: Allure::ContentType::TXT,
test_case: true
)
Allure.add_attachment(
name: 'request body',
source: ResponseParser.get_request_body(response).to_json,
type: Allure::ContentType::JSON,
test_case: true
)
Allure.add_attachment(
name: 'response status',
source: ResponseParser.get_response_status(response).to_json,
type: Allure::ContentType::JSON,
test_case: true
)
Allure.add_attachment(
name: 'response headers',
source: ResponseParser.get_response_headers(response).join("n"),
type: Allure::ContentType::TXT,
test_case: true
)
Allure.add_attachment(
name: 'response body',
source: ResponseParser.get_response_body(response).to_json,
type: Allure::ContentType::JSON,
test_case: true
)
rescue StandardError => e
puts "Failed to add allure attachment: #{e.message}"
puts e.backtrace
example.execution_result.exception = e
end
end
And the below in my spec_helper
where I call the function to attach details to report in the after(:each) hook.
require "rspec"
require "allure-rspec"
require "pathname"
require_relative '../spec/allure_helper'
RSpec.configure do |config|
config.add_formatter AllureRspecFormatter
AllureRspec.configure do |c|
c.results_directory = 'reports/allure-results'
c.clean_results_directory = true
c.logger = Logger.new($stdout, Logger::DEBUG)
c.logging_level = Logger::INFO
end
config.after(:each) do |example|
if @response
example.metadata[:response] = @response
AllureHelper.attach_request_details_to_report(@response)
end
end
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.shared_context_metadata_behavior = :apply_to_host_groups
end
when I run tests, I keep getting this error
ERROR -- : Can't add attachment, no test, step or fixture is running