class TestRailRSpecIntegration::TestRailPlanFormatter

Public Class Methods

new(out) click to toggle source
# File lib/files/testrail_rspec_integration.rb, line 27
def initialize(out)
  @out = out
end
post_results(test_cases) click to toggle source

test_cases is an array of TestCase instances

# 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) click to toggle source
# File lib/files/testrail_rspec_integration.rb, line 31
def self.set_product(product)
  @@product = product
end

Public Instance Methods

active() click to toggle source

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.

# File lib/files/testrail_rspec_integration.rb, line 54
def active
  !RSpec.configuration.dry_run
end
example_failed(notification)
Alias for: example_finished
example_finished(notification) click to toggle source

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

# 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.metadata[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
example_passed(notification)
Alias for: example_finished
example_pending(notification)
Alias for: example_finished
output() click to toggle source
# File lib/files/testrail_rspec_integration.rb, line 48
def output
  :testrailtagging
end
set_test_run_id(run_id) click to toggle source
# File lib/files/testrail_rspec_integration.rb, line 35
def set_test_run_id(run_id)
  @@run_id = run_id
end
start(_start_notification) click to toggle source

This gets called before all tests are run

# 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) click to toggle source

This gets called after all tests are run

# 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_key() click to toggle source
# 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

Private Instance Methods

is_for_test_rail_plan() click to toggle source

For pushing results up to a test plan in TestRail.

# File lib/files/testrail_rspec_integration.rb, line 211
def is_for_test_rail_plan
  !ENV["TESTRAIL_RUN"].nil? && !ENV["TESTRAIL_PLAN_ID"].nil? && !ENV["TESTRAIL_ENTRY_ID"].nil? && !ENV["TESTRAIL_ENTRY_RUN_ID"].nil?
end
is_for_test_rail_run() click to toggle source

For pushing results to a single, existing test run in TestRail

# File lib/files/testrail_rspec_integration.rb, line 216
def is_for_test_rail_run
  !ENV["TESTRAIL_RUN_ID"].nil? && ENV["TESTRAIL_RUN"].nil? && ENV["TESTRAIL_PLAN_ID"].nil?
end