debian-mirror-gitlab/lib/gitlab/ci/reports/test_suite.rb

108 lines
2.8 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-11-18 11:00:15 +05:30
module Gitlab
module Ci
module Reports
class TestSuite
2020-07-28 23:09:34 +05:30
attr_accessor :name
attr_accessor :test_cases
attr_accessor :total_time
2020-05-24 23:13:21 +05:30
attr_reader :suite_error
2018-11-18 11:00:15 +05:30
def initialize(name = nil)
@name = name
@test_cases = {}
2021-01-03 14:25:43 +05:30
@all_test_cases = []
2018-11-18 11:00:15 +05:30
@total_time = 0.0
end
def add_test_case(test_case)
@test_cases[test_case.status] ||= {}
@test_cases[test_case.status][test_case.key] = test_case
@total_time += test_case.execution_time
end
2021-01-03 14:25:43 +05:30
def each_test_case
@test_cases.each do |status, test_cases|
test_cases.values.each do |test_case|
yield test_case
end
end
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
def total_count
2020-05-24 23:13:21 +05:30
return 0 if suite_error
2020-10-24 23:57:45 +05:30
[success_count, failed_count, skipped_count, error_count].sum
2018-11-18 11:00:15 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
def total_status
2020-05-24 23:13:21 +05:30
if suite_error
TestCase::STATUS_ERROR
elsif failed_count > 0 || error_count > 0
2018-11-18 11:00:15 +05:30
TestCase::STATUS_FAILED
else
TestCase::STATUS_SUCCESS
end
end
2020-04-22 19:07:51 +05:30
def with_attachment!
@test_cases = @test_cases.extract!("failed")
@test_cases.keep_if do |status, hash|
hash.any? do |key, test_case|
test_case.has_attachment?
end
end
end
2018-11-18 11:00:15 +05:30
TestCase::STATUS_TYPES.each do |status_type|
define_method("#{status_type}") do
2020-05-24 23:13:21 +05:30
return {} if suite_error || test_cases[status_type].nil?
test_cases[status_type]
2018-11-18 11:00:15 +05:30
end
define_method("#{status_type}_count") do
2020-05-24 23:13:21 +05:30
return 0 if suite_error || test_cases[status_type].nil?
test_cases[status_type].length
2018-11-18 11:00:15 +05:30
end
end
2020-05-24 23:13:21 +05:30
def set_suite_error(msg)
@suite_error = msg
end
2020-07-28 23:09:34 +05:30
def +(other)
self.class.new.tap do |test_suite|
test_suite.name = self.name
test_suite.test_cases = self.test_cases.deep_merge(other.test_cases)
test_suite.total_time = self.total_time + other.total_time
end
end
2020-11-24 15:15:51 +05:30
def sorted
sort_by_status
sort_by_execution_time_desc
self
end
2018-11-18 11:00:15 +05:30
private
2020-11-24 15:15:51 +05:30
def sort_by_status
@test_cases = @test_cases.sort_by { |status, _| Gitlab::Ci::Reports::TestCase::STATUS_TYPES.index(status) }.to_h
end
def sort_by_execution_time_desc
@test_cases = @test_cases.keys.each_with_object({}) do |key, hash|
hash[key] = @test_cases[key].sort_by { |_key, test_case| -test_case.execution_time }.to_h
end
end
2018-11-18 11:00:15 +05:30
end
end
end
end