Class: Minitest::Reporters::MeanTimeReporter

Inherits:
DefaultReporter show all
Defined in:
lib/minitest/reporters/mean_time_reporter.rb

Overview

This reporter creates a report providing the average (mean), minimum and maximum times for a test to run. Running this for all your tests will allow you to:

1) Identify the slowest running tests over time as potential candidates for improvements or refactoring. 2) Identify (and fix) regressions in test run speed caused by changes to your tests or algorithms in your code. 3) Provide an abundance of statistics to enjoy.

This is achieved by creating a (configurable) 'previous runs' statistics file which is parsed at the end of each run to provide a new (configurable) report. These statistics can be reset at any time by using a simple rake task:

rake reset_statistics

Defined Under Namespace

Classes: InvalidOrder, InvalidSortColumn

Constant Summary

Constants included from Minitest::RelativePosition

Minitest::RelativePosition::INFO_PADDING, Minitest::RelativePosition::MARK_SIZE, Minitest::RelativePosition::TEST_PADDING, Minitest::RelativePosition::TEST_SIZE

Instance Attribute Summary

Attributes inherited from BaseReporter

#tests

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DefaultReporter

#before_suite, #before_test, #print_failure, #record, #record_failure, #record_pass, #record_skip, #start

Methods included from ANSI::Code

#black, color?

Methods inherited from BaseReporter

#add_defaults, #after_test, #before_test, #record

Constructor Details

#initialize(options = {}) ⇒ Minitest::Reporters::MeanTimeReporter

Parameters:

  • options (Hash) (defaults to: {})
  • previous_runs_filename (Hash)

    a customizable set of options

  • report_filename (Hash)

    a customizable set of options

  • show_count (Hash)

    a customizable set of options

  • sort_column (Hash)

    a customizable set of options

  • order (Hash)

    a customizable set of options



51
52
53
54
55
# File 'lib/minitest/reporters/mean_time_reporter.rb', line 51

def initialize(options = {})
  super

  @all_suite_times = []
end

Class Method Details

.reset_statistics!Boolean

Reset the statistics file for this reporter. Called via a rake task:

rake reset_statistics

Returns:

  • (Boolean)


34
35
36
# File 'lib/minitest/reporters/mean_time_reporter.rb', line 34

def self.reset_statistics!
  new.reset_statistics!
end

Instance Method Details

#after_suite(suite) ⇒ Hash<String => Float>

Copies the suite times from the DefaultReporter#after_suite method, making them available to this class.

Returns:

  • (Hash<String => Float>)


62
63
64
65
66
# File 'lib/minitest/reporters/mean_time_reporter.rb', line 62

def after_suite(suite)
  super

  @all_suite_times = @suite_times
end

#report

Runs the DefaultReporter#report method and then enhances it by storing the results to the 'previous_runs_filename' and outputs the parsed results to both the 'report_filename' and the terminal.



73
74
75
76
77
78
79
80
81
# File 'lib/minitest/reporters/mean_time_reporter.rb', line 73

def report
  super

  create_or_update_previous_runs!

  create_new_report!

  write_to_screen!
end

#reset_statistics!

This method returns an undefined value.

Resets the 'previous runs' file, essentially removing all previous statistics gathered.



87
88
89
# File 'lib/minitest/reporters/mean_time_reporter.rb', line 87

def reset_statistics!
  File.open(previous_runs_filename, 'w+') { |f| f.write('') }
end