module Report
Attributes
testrail[RW]
allows access to an instance of the testrail object. If it’s not set then it will be created on the first read most useful when testing because you can put a spy in place of the testrail api to check the expected calls are being made
Public Instance Methods
check_if_a_test_case_id_exists(scenario)
click to toggle source
# File lib/cucumber_testrail/external_reporting.rb, line 82 def check_if_a_test_case_id_exists(scenario) if scenario.testcase matches = testrail.send_get("get_cases/#{scenario.project}&suite_id=#{scenario.suite}").select{|m| m['id'].to_i == scenario.testcase.to_i} ref = matches.first['id'] end ref end
create_a_test_case_from_scenario(scenario)
click to toggle source
# File lib/cucumber_testrail/external_reporting.rb, line 66 def create_a_test_case_from_scenario(scenario) testrail.send_post("add_case/#{scenario.sub_section || scenario.suite}",scenario.testrail_testcase) end
get_an_external_reference(scenario)
click to toggle source
If we don’t have a tag testcase id then we need to find one. Either by matching a testcase by title, or by creating a new testcase The input is a scenario and the result is a testcase id
# File lib/cucumber_testrail/external_reporting.rb, line 62 def get_an_external_reference(scenario) (check_if_a_test_case_id_exists(scenario) || locate_a_test_case_by_name(scenario) || create_a_test_case_from_scenario(scenario)['id']).to_i end
locate_a_test_case_by_name(scenario)
click to toggle source
# File lib/cucumber_testrail/external_reporting.rb, line 70 def locate_a_test_case_by_name(scenario) if scenario.sub_section restrict_by ="#{scenario.project}&suite_id=#{scenario.suite}§ion_id=#{scenario.sub_section}" else restrict_by ="#{scenario.project}&suite_id=#{scenario.suite}" end matches = testrail.send_get("get_cases/#{restrict_by}").select{|m| m['title'] == scenario.title} unless matches.empty? matches.first['id'] end end
send_result(scenario)
click to toggle source
The only entry point from your Cucumber After scenario hooks.
You need to have this code ;-
After do |scenario|
send_result(scenario)
end
It returns true if it wrote to Testrail
and false if it didn’t. I.e. if you set ignore_testrail then it won’t send results to Testrail
and it will return false
# File lib/cucumber_testrail/external_reporting.rb, line 19 def send_result(scenario) if scenario.ignore_testrail? result = false else external_reference = get_an_external_reference(scenario) raise 'duff external reference' unless external_reference.to_i > 0 #p "external reference #{external_reference}" send_test_result_to_testrail(scenario,external_reference) unless scenario.skip_result? update_test_case(scenario,external_reference) # only write the testcase tag if it's a scenario, not a scenario outline update_source_file(scenario,"testcase_#{external_reference}") if scenario.is_a?(Cucumber::Ast::Scenario) result = true end result end
send_test_result_to_testrail(scenario,external_reference=nil)
click to toggle source
# File lib/cucumber_testrail/external_reporting.rb, line 90 def send_test_result_to_testrail(scenario,external_reference=nil) testcase = external_reference || scenario.testcase testrail.send_post("add_result_for_case/#{scenario.testrun}/#{testcase}",scenario.testrail_test_report) end
update_source_file(scenario, external_reference)
click to toggle source
This method takes an external reference, e.g. testcase_119 and adds it as a tag before the scenario
# File lib/cucumber_testrail/external_reporting.rb, line 39 def update_source_file(scenario, external_reference) #this could be done on one line with sed. But the format is different on Mac OS and GNU Linux version and it definitely won't work on a Windows machine path = scenario.file lines = IO.readlines(scenario.file) lines[scenario.tag_line].gsub!(/@testcase_(\d*)|\n/," @#{external_reference}") unless lines[scenario.tag_line] =~/#{external_reference}/ temp_file = Tempfile.new('foo') begin File.open(path, 'r') do |file| lines.each do |line| temp_file.puts line end end temp_file.close FileUtils.mv(temp_file.path, path) ensure temp_file.close temp_file.unlink end end
update_test_case(scenario,external_reference)
click to toggle source
# File lib/cucumber_testrail/external_reporting.rb, line 95 def update_test_case(scenario,external_reference) testcase = external_reference || scenario.testcase testrail.send_post("update_case/#{testcase}",scenario.testrail_testcase) end