ERROR — : Can’t add attachment, no test, step or fixture is running (allure-rspec)

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.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># 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
</code>
<code># 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 </code>
# 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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật