Class: TestRailRSpecIntegration::TestRailPlanFormatter

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

Constant Summary collapse

@@cases =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out) ⇒ TestRailPlanFormatter

Returns a new instance of TestRailPlanFormatter.



27
28
29
# File 'lib/files/testrail_rspec_integration.rb', line 27

def initialize(out)
  @out = out
end

Class Method Details

.post_results(test_cases) ⇒ Object

test_cases is an array of TestCase instances



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/files/testrail_rspec_integration.rb', line 172

def self.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(@@run_id, data)
    test_case_ids = test_cases.collect { |tc| tc.id }
    puts "Successfully posted results for testcases: #{test_case_ids} to test run: #{@@run_id}"
  else
    puts "No results sent to test rail"
  end
end

.set_product(product) ⇒ Object



31
32
33
# File 'lib/files/testrail_rspec_integration.rb', line 31

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.



54
55
56
# File 'lib/files/testrail_rspec_integration.rb', line 54

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



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
166
167
168
169
# File 'lib/files/testrail_rspec_integration.rb', line 136

def example_finished(notification)
  return unless active
  example = notification.example
  result = example.execution_result
  testrail_ids = example.[test_id_key]

  return if testrail_ids.empty?

  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

  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

  # Batches together test cases before posting. Relies on environment variable TESTRAIL_BATCH_SIZE to determine
  # batch size.
  # Relies on an 'after suite' hook to capture and post results for any number of remaining test cases less
  # than the batch size
  if @@cases.size >= @batch_size.to_i
    TestRailPlanFormatter.post_results @@cases
    @@cases.clear
  end
end

#outputObject



48
49
50
# File 'lib/files/testrail_rspec_integration.rb', line 48

def output
  :testrailtagging
end

#set_test_run_id(run_id) ⇒ Object



35
36
37
# File 'lib/files/testrail_rspec_integration.rb', line 35

def set_test_run_id(run_id)
  @@run_id = run_id
end

#start(_start_notification) ⇒ Object

This gets called before all tests are run



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/files/testrail_rspec_integration.rb', line 59

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

  # Initialize the batch size for test rail batching based on environment variable.
  # One test is the default, in case people don't want to batch or haven't provided the variable.
  if !ENV["TESTRAIL_BATCH_SIZE"].nil?
    @batch_size = ENV["TESTRAIL_BATCH_SIZE"]
  else
    @batch_size = 1
  end

  # Initialize the number of cleanup days for test rail runs based on environment variable.
  # Unless force_delete = true is passed the minimum is 7 days.
  if !ENV["TESTRAIL_RUN_DELETE_DAYS"].nil? && !@testrail_plan_id.nil?
    TestRailOperations.delete_plan_entry(@testrail_plan_id, ENV["TESTRAIL_RUN_DELETE_DAYS"].to_f, ENV["TESTRAIL_RUN_DELETE_FORCE"])
  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}"

  puts "Batching test results in groups of #{@batch_size}"
  @test_case_hash = TestRailOperations.get_test_run_cases(@testrail_run_id)
  # save the test case ID's that were actually executed
  @executed_test_ids = []

  # Need a class variable for after suite hooks to post results,
  # since the after suite hooks are defined outside the class
  set_test_run_id(@testrail_run_id)
end

#stop(_examples_notification) ⇒ Object

This gets called after all tests are run



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 114

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



39
40
41
42
43
44
45
46
# File 'lib/files/testrail_rspec_integration.rb', line 39

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