Class: TestRailRSpecIntegration::TestRailPlanFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/files/testrail_rspec_integration.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out) ⇒ TestRailPlanFormatter

Returns a new instance of TestRailPlanFormatter.



25
26
27
# File 'lib/files/testrail_rspec_integration.rb', line 25

def initialize(out)
  @out = out
end

Class Method Details

.set_product(product) ⇒ Object



29
30
31
# File 'lib/files/testrail_rspec_integration.rb', line 29

def self.set_product(product)
  @@product = product
end

Instance Method Details

#activeObject

Gets whether the formatter is active or not. We don’t want to push results up to test rail for instance if –dry-run is specified on the command line.



44
45
46
# File 'lib/files/testrail_rspec_integration.rb', line 44

def active
  !RSpec.configuration.dry_run
end

#example_finished(notification) ⇒ Object Also known as: example_passed, example_pending, example_failed

This gets called after all after hooks are run after each example is completed



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/files/testrail_rspec_integration.rb', line 107

def example_finished(notification)
  return unless active
  example = notification.example
  result = example.execution_result

  testrail_ids = example.[test_id_key]
  return unless testrail_ids.present?
  completion_message = ""

  if (result.status == :failed)
    # This is the best format, unfortunately it has bash console color codes embedded in it.
    completion_message = notification.fully_formatted(1)
    # need to remove those color codes from the string
    completion_message.gsub!(/\[(\d)+m/, '')
  end

  cases = [] # the test cases
  Array(testrail_ids).each do |id|
    tc = @test_case_hash[id.to_i]
    next unless tc # A test case ID exists in the rspec file, but not on testrail
    tc.set_status(result.status, completion_message)
    cases << tc
    @executed_test_ids << id.to_i
  end

  post_results cases
end

#post_results(test_cases) ⇒ Object

test_cases is an array of TestCase instances



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/files/testrail_rspec_integration.rb', line 136

def post_results(test_cases)
  data = []
  test_cases.each do |tc|

    status_value = TestRailOperations.status_rspec_to_testrail(tc.status)
    if status_value == TestRailOperations::UNTESTED
      # ! SUPER IMPORTANT !
      # test rail does NOT allow you to set the status of a test to untested.
      # so skip them
      next
    end

    # id was not found in the list of test run id's. Due to incorrect include pattern in rspec.
    next unless tc.temp_id

    data << {
        "test_id" => tc.temp_id, # results require the new test case temporary ID's, not the static ID's
        "status_id" => status_value,
        "comment" => tc.result_message
    }
  end

  if data.size > 0
    TestRailOperations.post_run_results(@testrail_run_id, data)
    test_case_ids = test_cases.collect { |tc| tc.id }
    @out.puts "Successfully posted results for testcases: #{test_case_ids} to test run: #{@testrail_run_id}"
  else
    @out.puts "No results sent to test rail"
  end
end

#start(_start_notification) ⇒ Object

This gets called before all tests are run



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/files/testrail_rspec_integration.rb', line 49

def start(_start_notification)
  # It's been verified that these 4 environment variables already exist
  # These three are not actively used in this class, but their presence governs whether
  # this class is instantiated and used in the first place.

  if is_for_test_rail_run
    @testrail_run_id = ENV["TESTRAIL_RUN_ID"]
  elsif !ENV["TESTRAIL_PLAN_ID"].nil?
    @testrail_plan_id  = ENV["TESTRAIL_PLAN_ID"]
    @testrail_run_name = ENV["TESTRAIL_RUN"]
    if is_for_test_rail_plan # run on jenkins
      @testrail_run_id   = ENV["TESTRAIL_ENTRY_RUN_ID"]
      @testrail_entry_id = ENV["TESTRAIL_ENTRY_ID"]
    else # run locally, and only one thread
      ids = TestRailOperations.create_test_plan_entry(@testrail_plan_id, @testrail_run_name, include_all_cases: true)
      @testrail_run_id   = ids[:run_id]
      @testrail_entry_id = ids[:entry_id]
    end
  end

  # Pull down ALL the test cases from testrail. Granted this is more than what rspec will actually
  # execute. But there is no safe way to append a test case to a test run in a parallel environment.
  # The Testrail API is just too limited.
  puts "Using test run ID: #{@testrail_run_id}"
  puts "Using test entry ID: #{@testrail_entry_id}"

  puts "Count of skipped tests: #{TestRailRSpecIntegration.get_skip_count}"
  puts "Count of tests to be run: #{TestRailRSpecIntegration.get_run_count}"
  puts "Count of tests that entered filter: #{TestRailRSpecIntegration.get_total_count}"

  @test_case_hash = TestRailOperations.get_test_run_cases(@testrail_run_id)
  # save the test case ID's that were actually executed
  @executed_test_ids = []
end

#stop(_examples_notification) ⇒ Object

This gets called after all tests are run



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/files/testrail_rspec_integration.rb', line 85

def stop(_examples_notification)
  if @testrail_plan_id
    # Need to prune un-executed tests from the test run on testrail
    if is_for_test_rail_plan # run on jenkins, multiple threads doing this
      # Need to dump a list of executed tests so unexecuted tests can be pruned later (on testrail)
      # after all the rspec tests are done.
      File.open("executed_tests_#{Process.pid}.json", 'w') do |f|
        f.puts @executed_test_ids.to_json
      end
      # Another process will take the json file and use it to prune the test run.
    else # run locally, and only one thread
      # prune the test cases to only what was run
      response = TestRailOperations.keep_only(@testrail_plan_id, @testrail_entry_id, @executed_test_ids)
    end
  elsif !ENV["TESTRAIL_RUN_ID"].nil?
    # Results were already pushed to an existing testrail run. Nothing more to do here, we are done! :)
  else
    puts "Unknown condition"
  end
end

#test_id_keyObject



33
34
35
36
37
38
39
40
# File 'lib/files/testrail_rspec_integration.rb', line 33

def test_id_key
  case @@product
    when :bridge
      :testrail_id
    when :canvas
      :test_id
  end
end