Debian Codegraph @

test/unit/minitest/mean_time_reporter_unit_test.rb

raw
1 require_relative '../../test_helper'
2
3 module MinitestReportersTest
4 class MeanTimeReporterUnitTest < Minitest::Test
5 def setup
6 @test_data = []
7 @test_data << { name: 'MIDDLE', prev_time: 5.0, cur_time: 5.0 }
8 @test_data << { name: 'MIN_FAST', prev_time: 0.5, cur_time: 3.5 }
9 @test_data << { name: 'MIN_SLOW', prev_time: 10.5, cur_time: 10.5 }
10 @test_data << { name: 'MAX_FAST', prev_time: 1.2, cur_time: 0.9 }
11 @test_data << { name: 'MAX_SLOW', prev_time: 16.3, cur_time: 6.3 }
12 @test_data << { name: 'AVG_FAST', prev_time: 1.3, cur_time: 0.65 }
13 @test_data << { name: 'AVG_SLOW', prev_time: 10.2, cur_time: 14.2 }
14 configure_report_paths
15 end
16
17 def teardown
18 File.delete(@previous_run_path) if File.exist?(@previous_run_path)
19 File.delete(@report_file_path) if File.exist?(@report_file_path)
20 end
21
22 def test_defaults
23 subject = Minitest::Reporters::MeanTimeReporter.new.send(:defaults)
24
25 expected_prefix = "#{Dir.tmpdir}#{File::Separator}"
26 assert_match expected_prefix, subject[:previous_runs_filename]
27 assert_match expected_prefix, subject[:report_filename]
28 end
29
30 def test_sorts_avg_numerically
31 prev_output = generate_report(:avg, :prev_time)
32 report_output = generate_report(:avg, :cur_time)
33
34 expected_order = [
35 'AVG_SLOW',
36 'MAX_SLOW',
37 'MIN_SLOW',
38 'MIDDLE',
39 'MIN_FAST',
40 'MAX_FAST',
41 'AVG_FAST'
42 ]
43 verify_result_order(report_output, expected_order, prev_output)
44 end
45
46 def test_sorts_min_numerically
47 prev_output = generate_report(:min, :prev_time)
48 report_output = generate_report(:min, :cur_time)
49
50 expected_order = [
51 'MIN_SLOW',
52 'AVG_SLOW',
53 'MAX_SLOW',
54 'MIDDLE',
55 'MAX_FAST',
56 'AVG_FAST',
57 'MIN_FAST'
58 ]
59 verify_result_order(report_output, expected_order, prev_output)
60 end
61
62 def test_sorts_max_numerically
63 prev_output = generate_report(:max, :prev_time)
64 report_output = generate_report(:max, :cur_time)
65
66 expected_order = [
67 'MAX_SLOW',
68 'AVG_SLOW',
69 'MIN_SLOW',
70 'MIDDLE',
71 'MIN_FAST',
72 'AVG_FAST',
73 'MAX_FAST'
74 ]
75 verify_result_order(report_output, expected_order, prev_output)
76 end
77
78 def test_sorts_last_numerically
79 prev_output = generate_report(:last, :prev_time)
80 report_output = generate_report(:last, :cur_time)
81
82 expected_order = [
83 'AVG_SLOW',
84 'MIN_SLOW',
85 'MAX_SLOW',
86 'MIDDLE',
87 'MIN_FAST',
88 'MAX_FAST',
89 'AVG_FAST'
90 ]
91 verify_result_order(report_output, expected_order, prev_output)
92 end
93
94 private
95
96 def simulate_suite_runtime(suite_name, run_time)
97 test_suite = Minitest::Test.new(suite_name)
98 base_clock_time = Minitest::Reporters.clock_time
99 Minitest::Reporters.stub(:clock_time, base_clock_time - run_time) do
100 @reporter.before_suite(test_suite)
101 end
102 Minitest::Reporters.stub(:clock_time, base_clock_time) do
103 @reporter.after_suite(test_suite)
104 end
105 end
106
107 def configure_report_paths
108 @previous_run_path = File.expand_path("../minitest-mean-time-previous-runs", File.realpath(__FILE__))
109 File.delete(@previous_run_path) if File.exist?(@previous_run_path)
110 @report_file_path = File.expand_path("../minitest-mean-time-report", File.realpath(__FILE__))
111 File.delete(@report_file_path) if File.exist?(@report_file_path)
112 end
113
114 def generate_report(sort_column, time_name)
115 # Reset the reporter for the test run
116 @reporter = Minitest::Reporters::MeanTimeReporter.new(
117 previous_runs_filename: @previous_run_path,
118 report_filename: @report_file_path,
119 sort_column: sort_column
120 )
121 @test_data.each { |hash| simulate_suite_runtime(hash[:name], hash[time_name])}
122 @reporter.tests << Minitest::Test.new('Final')
123
124 report_output = StringIO.new
125 @reporter.io = report_output
126 @reporter.start
127 @reporter.report
128 report_output
129 end
130
131 def verify_result_order(report_output, expected_order, prev_output)
132 prev_lines = extract_test_lines(prev_output)
133 test_lines = extract_test_lines(report_output)
134
135 actual_order = test_lines.map { |line| line.gsub(/.*Description: /, '') }
136
137 assert_equal(expected_order, actual_order, "\nCurrent report:\n#{test_lines.join("\n")}\n\nPrevious report:\n#{prev_lines.join("\n")}")
138 end
139
140 def extract_test_lines(report_output)
141 report_output.rewind
142 test_lines = report_output.read.split("\n")
143 test_lines.select! {|line| line.start_with?('Avg:')}
144 # Exclude the final placeholder 0 second test from assertions
145 test_lines.reject! {|line| line.end_with?('Minitest::Test')}
146 test_lines
147 end
148 end
149 end