Commit b24f2693 authored by Maxime Orefice's avatar Maxime Orefice Committed by Vitali Tatarintev

Sort TestCase data

This MR introduces the ability for a TestSuite object to sort
its test_cases data by status and execution_time dsc.
parent bff3d230
...@@ -13,7 +13,7 @@ class TestSuiteEntity < Grape::Entity ...@@ -13,7 +13,7 @@ class TestSuiteEntity < Grape::Entity
with_options if: -> (_, opts) { opts[:details] } do |test_suite| with_options if: -> (_, opts) { opts[:details] } do |test_suite|
expose :suite_error expose :suite_error
expose :test_cases, using: TestCaseEntity do |test_suite| expose :test_cases, using: TestCaseEntity do |test_suite|
test_suite.suite_error ? [] : test_suite.test_cases.values.flat_map(&:values) test_suite.suite_error ? [] : test_suite.sorted.test_cases.values.flat_map(&:values)
end end
end end
end end
---
title: Sort TestCase data by status and execution_time
merge_request: 40722
author:
type: changed
...@@ -8,7 +8,7 @@ module Gitlab ...@@ -8,7 +8,7 @@ module Gitlab
STATUS_FAILED = 'failed' STATUS_FAILED = 'failed'
STATUS_SKIPPED = 'skipped' STATUS_SKIPPED = 'skipped'
STATUS_ERROR = 'error' STATUS_ERROR = 'error'
STATUS_TYPES = [STATUS_SUCCESS, STATUS_FAILED, STATUS_SKIPPED, STATUS_ERROR].freeze STATUS_TYPES = [STATUS_ERROR, STATUS_FAILED, STATUS_SUCCESS, STATUS_SKIPPED].freeze
attr_reader :name, :classname, :execution_time, :status, :file, :system_output, :stack_trace, :key, :attachment, :job attr_reader :name, :classname, :execution_time, :status, :file, :system_output, :stack_trace, :key, :attachment, :job
......
...@@ -78,11 +78,27 @@ module Gitlab ...@@ -78,11 +78,27 @@ module Gitlab
end end
end end
def sorted
sort_by_status
sort_by_execution_time_desc
self
end
private private
def existing_key?(test_case) def existing_key?(test_case)
@test_cases[test_case.status]&.key?(test_case.key) @test_cases[test_case.status]&.key?(test_case.key)
end end
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
end end
end end
end end
......
...@@ -176,6 +176,37 @@ RSpec.describe Gitlab::Ci::Reports::TestSuite do ...@@ -176,6 +176,37 @@ RSpec.describe Gitlab::Ci::Reports::TestSuite do
end end
end end
describe '#sorted' do
subject { test_suite.sorted }
context 'when there are multiple failed test cases' do
before do
test_suite.add_test_case(create_test_case_rspec_failed('test_spec_1', 1.11))
test_suite.add_test_case(create_test_case_rspec_failed('test_spec_2', 4.44))
end
it 'returns test cases sorted by execution time desc' do
expect(subject.test_cases['failed'].each_value.first.execution_time).to eq(4.44)
expect(subject.test_cases['failed'].values.second.execution_time).to eq(1.11)
end
end
context 'when there are multiple test cases' do
let(:status_ordered) { %w(error failed success skipped) }
before do
test_suite.add_test_case(test_case_success)
test_suite.add_test_case(test_case_failed)
test_suite.add_test_case(test_case_error)
test_suite.add_test_case(test_case_skipped)
end
it 'returns test cases sorted by status' do
expect(subject.test_cases.keys).to eq(status_ordered)
end
end
end
Gitlab::Ci::Reports::TestCase::STATUS_TYPES.each do |status_type| Gitlab::Ci::Reports::TestCase::STATUS_TYPES.each do |status_type|
describe "##{status_type}" do describe "##{status_type}" do
subject { test_suite.public_send("#{status_type}") } subject { test_suite.public_send("#{status_type}") }
......
...@@ -10,12 +10,12 @@ module TestReportsHelper ...@@ -10,12 +10,12 @@ module TestReportsHelper
status: Gitlab::Ci::Reports::TestCase::STATUS_SUCCESS) status: Gitlab::Ci::Reports::TestCase::STATUS_SUCCESS)
end end
def create_test_case_rspec_failed(name = 'test_spec') def create_test_case_rspec_failed(name = 'test_spec', execution_time = 2.22)
Gitlab::Ci::Reports::TestCase.new( Gitlab::Ci::Reports::TestCase.new(
name: 'Test#sum when a is 1 and b is 3 returns summary', name: 'Test#sum when a is 1 and b is 3 returns summary',
classname: "spec.#{name}", classname: "spec.#{name}",
file: './spec/test_spec.rb', file: './spec/test_spec.rb',
execution_time: 2.22, execution_time: execution_time,
system_output: sample_rspec_failed_message, system_output: sample_rspec_failed_message,
status: Gitlab::Ci::Reports::TestCase::STATUS_FAILED) status: Gitlab::Ci::Reports::TestCase::STATUS_FAILED)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment