Module: Report

Included in:
CucumberTestrail
Defined in:
lib/cucumber_testrail/external_reporting.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#testrailObject

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



8
9
10
# File 'lib/cucumber_testrail/external_reporting.rb', line 8

def testrail
  @testrail
end

Instance Method Details

#check_if_a_test_case_id_exists(scenario) ⇒ Object



82
83
84
85
86
87
88
# 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) ⇒ Object



66
67
68
# 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) ⇒ Object

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



62
63
64
# 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) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# 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}&section_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) ⇒ Object

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object



90
91
92
93
# 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) ⇒ Object

This method takes an external reference, e.g. testcase_119 and adds it as a tag before the scenario



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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) ⇒ Object



95
96
97
98
# 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