Module: Minitest
- Includes:
- ErrorOnWarning
- Defined in:
- lib/minitest.rb,
lib/minitest/mock.rb,
lib/minitest/spec.rb,
lib/minitest/test.rb,
lib/minitest/unit.rb,
lib/minitest/compress.rb,
lib/minitest/parallel.rb,
lib/minitest/benchmark.rb,
lib/minitest/benchmark.rb,
lib/minitest/test_task.rb,
lib/minitest/assertions.rb,
lib/minitest/pride_plugin.rb,
lib/minitest/manual_plugins.rb,
lib/minitest/error_on_warning.rb
Overview
:nodoc:
Defined Under Namespace
Modules: Assertions, Compress, ErrorOnWarning, Expectations, Guard, Parallel, Reportable Classes: AbstractReporter, Assertion, BacktraceFilter, BenchSpec, Benchmark, CompositeReporter, Expectation, Mock, PrideIO, PrideLOL, ProgressReporter, Reporter, Result, Runnable, Skip, Spec, StatisticsReporter, SummaryReporter, Test, TestTask, UnexpectedError, UnexpectedWarning, Unit
Constant Summary collapse
- VERSION =
:nodoc:
"5.26.2"- @@after_run =
[]
Class Method Summary collapse
-
.__run(reporter, options) ⇒ Object
Internal run method.
-
.after_run(&block) ⇒ Object
A simple hook allowing you to run a block of code after everything is done running.
-
.autorun ⇒ Object
Registers Minitest to run at process exit.
-
.cattr_accessor(name) ⇒ Object
:nodoc:.
-
.clock_time ⇒ Object
:nodoc:.
-
.empty_run!(options) ⇒ Object
:nodoc:.
-
.filter_backtrace(bt) ⇒ Object
:nodoc:.
-
.init_plugins(options) ⇒ Object
:nodoc:.
-
.load(*names) ⇒ Object
Manually load plugins by name.
-
.load_plugins ⇒ Object
:nodoc:.
-
.plugin_pride_init(options) ⇒ Object
:nodoc:.
-
.plugin_pride_options(opts, _options) ⇒ Object
:nodoc:.
-
.process_args(args = []) ⇒ Object
:nodoc:.
-
.register_plugin(name_or_mod) ⇒ Object
Register a plugin to be used.
-
.run(args = []) ⇒ Object
This is the top-level run method.
-
.run_one_method(klass, method_name) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#backtrace_filter ⇒ Object
Filter object for backtraces.
-
#extensions ⇒ Object
Names of known extension plugins.
-
#info_signal ⇒ Object
The signal to use for dumping information to STDERR.
-
#parallel_executor ⇒ Object
Parallel test executor.
-
#reporter ⇒ Object
Reporter object to be used for all runs.
-
#seed ⇒ Object
The random seed used for this run.
Methods included from ErrorOnWarning
Class Method Details
.__run(reporter, options) ⇒ Object
Internal run method. Responsible for telling all Runnable sub-classes to run.
337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/minitest.rb', line 337 def self.__run reporter, suites = Runnable.runnables.shuffle parallel, serial = suites.partition { |s| s.test_order == :parallel } # If we run the parallel tests before the serial tests, the parallel tests # could run in parallel with the serial tests. This would be bad because # the serial tests won't lock around Reporter#record. Run the serial tests # first, so that after they complete, the parallel tests will lock when # recording results. serial.map { |suite| suite.run reporter, } + parallel.map { |suite| suite.run reporter, } end |
.after_run(&block) ⇒ Object
A simple hook allowing you to run a block of code after everything is done running. Eg:
Minitest.after_run { p $debugging_info }
96 97 98 |
# File 'lib/minitest.rb', line 96 def self.after_run &block @@after_run << block end |
.autorun ⇒ Object
Registers Minitest to run at process exit
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/minitest.rb', line 70 def self.autorun Warning[:deprecated] = true at_exit { next if $! and not ($!.kind_of? SystemExit and $!.success?) exit_code = nil pid = Process.pid at_exit { next if !Minitest.allow_fork && Process.pid != pid @@after_run.reverse_each(&:call) exit exit_code || false } exit_code = Minitest.run ARGV } unless @@installed_at_exit @@installed_at_exit = true end |
.cattr_accessor(name) ⇒ Object
:nodoc:
19 20 21 |
# File 'lib/minitest.rb', line 19 def self.cattr_accessor name # :nodoc: (class << self; self; end).attr_accessor name end |
.clock_time ⇒ Object
:nodoc:
1231 1232 1233 |
# File 'lib/minitest.rb', line 1231 def self.clock_time Process.clock_gettime Process::CLOCK_MONOTONIC end |
.empty_run!(options) ⇒ Object
:nodoc:
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/minitest.rb', line 317 def self.empty_run! # :nodoc: filter = [:filter] return true unless filter # no filter, but nothing ran == success warn "Nothing ran for filter: %s" % [filter] require "did_you_mean" # soft dependency, punt if it doesn't load ms = Runnable.runnables.flat_map(&:runnable_methods) cs = DidYouMean::SpellChecker.new(dictionary: ms).correct filter warn DidYouMean::Formatter. cs unless cs.empty? rescue LoadError # do nothing end |
.filter_backtrace(bt) ⇒ Object
:nodoc:
350 351 352 353 354 |
# File 'lib/minitest.rb', line 350 def self.filter_backtrace bt # :nodoc: result = backtrace_filter.filter bt result = bt.dup if result.empty? result end |
.init_plugins(options) ⇒ Object
:nodoc:
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/minitest.rb', line 124 def self.init_plugins # :nodoc: self.extensions.each do |mod_or_meth| case mod_or_meth when Symbol, String then name = mod_or_meth msg = "plugin_#{name}_init" next unless self.respond_to? msg send msg, when Module then recv = mod_or_meth next unless recv.respond_to? :minitest_plugin_init recv.minitest_plugin_init else raise ArgumentError, "plugin is %p, but it must be a symbol, string or module" % [mod_or_meth] end end end |
.load(*names) ⇒ Object
Manually load plugins by name.
9 10 11 12 13 14 15 |
# File 'lib/minitest/manual_plugins.rb', line 9 def self.load *names names.each do |name| require "minitest/#{name}_plugin" self.extensions << name.to_s end end |
.load_plugins ⇒ Object
:nodoc:
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/minitest.rb', line 108 def self.load_plugins # :nodoc: return unless defined? Gem seen = {} Gem.find_files("minitest/*_plugin.rb").each do |plugin_path| name = File.basename plugin_path, "_plugin.rb" next if seen[name] seen[name] = true require plugin_path self.extensions << name end end |
.plugin_pride_init(options) ⇒ Object
:nodoc:
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/minitest/pride_plugin.rb', line 10 def self.plugin_pride_init # :nodoc: return unless PrideIO.pride? klass = ENV["TERM"] =~ /^xterm|-(?:256color|direct)$/ ? PrideLOL : PrideIO io = klass.new [:io] self.reporter.reporters.grep(Minitest::Reporter).each do |rep| rep.io = io if rep.io.tty? end end |
.plugin_pride_options(opts, _options) ⇒ Object
:nodoc:
4 5 6 7 8 |
# File 'lib/minitest/pride_plugin.rb', line 4 def self. opts, # :nodoc: opts.on "-p", "--pride", "Pride. Show your testing pride!" do PrideIO.pride! end end |
.process_args(args = []) ⇒ Object
:nodoc:
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/minitest.rb', line 142 def self.process_args args = [] # :nodoc: = { :io => $stdout, } orig_args = args.dup OptionParser.new do |opts| opts.program_name = "minitest" opts.version = Minitest::VERSION opts. = [ "Usage: rake test [A=options] (see Minitest::TestTask for more options)", "minitest [paths] [options] (with minitest-sprint gem)", "ruby path/to/test.rb [options]\n\n", ].join "\n or: " opts.on "-h", "--help", "Display this help." do puts opts exit end opts.on "--no-plugins", "Bypass minitest plugin auto-loading (or env: MT_NO_PLUGINS=1)." desc = "Sets random seed. Also via env, eg: SEED=42" opts.on "-s", "--seed SEED", Integer, desc do |m| [:seed] = m end opts.on "-v", "--verbose", "Verbose. Print each name as they run." do [:verbose] = true end opts.on "-q", "--quiet", "Quiet. Show no dots while processing files." do [:quiet] = true end opts.on "--show-skips", "Show skipped at the end of run." do [:show_skips] = true end opts.on "-n", "--name PATTERN", "Include /regexp/ or string for run." do |a| [:filter] = a end opts.on "-e", "--exclude PATTERN", "Exclude /regexp/ or string from run." do |a| [:exclude] = a end # part of my unofficial embedded gem "makeoptparseworkwell" def opts.topdict(name) = (name.length > 1 ? top.long : top.short) def opts.alias(from, to) = (dict = topdict(from) ; dict[to] = dict[from]) # these will work but won't show up in --help output: opts.alias "name", "include" opts.alias "n", "i" opts.alias "e", "x" opts.on "-S", "--skip CODES", String, "Skip reporting of certain types of results (eg E)." do |s| [:skip] = s.chars.to_a end opts.on "-W[error]", String, "Turn Ruby warnings into errors" do |s| [:Werror] = true case s when "error", "all", nil then require_relative "minitest/error_on_warning" $VERBOSE = true ::Warning[:deprecated] = true else ::Warning[s.to_sym] = true # check validity of category end end unless extensions.empty? opts.separator "" opts.separator "Known extensions: #{extensions.join ", "}" extensions.each do |mod_or_meth| case mod_or_meth when Symbol, String then meth = mod_or_meth msg = "plugin_#{meth}_options" send msg, opts, if respond_to? msg when Module recv = mod_or_meth next unless recv.respond_to? :minitest_plugin_options recv. opts, else raise ArgumentError, "plugin is %p, but it must be a symbol, string or module" % [mod_or_meth] end end end begin opts.parse! args rescue OptionParser::InvalidOption => e puts puts e puts puts opts exit 1 end orig_args -= args end unless [:seed] then srand [:seed] = (ENV["SEED"] || srand).to_i % 0xFFFF orig_args << "--seed" << [:seed].to_s end [:args] = orig_args.map { |s| s.match?(/[\s|&<>$()]/) ? s.inspect : s }.join " " end |
.register_plugin(name_or_mod) ⇒ Object
Register a plugin to be used. Does NOT require / load it.
103 104 105 106 |
# File 'lib/minitest.rb', line 103 def self.register_plugin name_or_mod self.extensions << name_or_mod nil end |
.run(args = []) ⇒ Object
This is the top-level run method. Everything starts from here. It tells each Runnable sub-class to run, and each of those are responsible for doing whatever they do.
The overall structure of a run looks like this:
Minitest.autorun
Minitest.run(args)
Minitest.load_plugins
Minitest.process_args
Minitest.init_plugins
Minitest.__run(reporter, )
Runnable.runnables.each |runnable_klass|
runnable_klass.run(reporter, )
filtered_methods = runnable_methods.select {...}.reject {...}
filtered_methods.each |runnable_method|
runnable_klass.run_one_method(self, runnable_method, reporter)
Minitest.run_one_method(runnable_klass, runnable_method)
runnable_klass.new(runnable_method).run
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/minitest.rb', line 282 def self.run args = [] self.load_plugins unless args.delete("--no-plugins") || ENV["MT_NO_PLUGINS"] = process_args args Minitest.seed = [:seed] srand Minitest.seed reporter = CompositeReporter.new reporter << SummaryReporter.new([:io], ) reporter << ProgressReporter.new([:io], ) unless [:quiet] self.reporter = reporter # this makes it available to plugins self.init_plugins self.reporter = nil # runnables shouldn't depend on the reporter, ever self.parallel_executor.start if parallel_executor.respond_to? :start reporter.start begin __run reporter, finished = true rescue Interrupt warn "Interrupted. Exiting..." end self.parallel_executor.shutdown if parallel_executor.respond_to? :shutdown # might have been removed/replaced during init_plugins: summary = reporter.reporters.grep(SummaryReporter).first reporter.report return empty_run! if finished && summary && summary.count == 0 finished and reporter.passed? end |
.run_one_method(klass, method_name) ⇒ Object
:nodoc:
1222 1223 1224 1225 1226 |
# File 'lib/minitest.rb', line 1222 def self.run_one_method klass, method_name # :nodoc: result = klass.new(method_name).run raise "#{klass}#run _must_ return a Result" unless Result === result result end |
Instance Method Details
#backtrace_filter ⇒ Object
Filter object for backtraces.
44 |
# File 'lib/minitest.rb', line 44 cattr_accessor :backtrace_filter |
#extensions ⇒ Object
Names of known extension plugins.
56 |
# File 'lib/minitest.rb', line 56 cattr_accessor :extensions |
#info_signal ⇒ Object
The signal to use for dumping information to STDERR. Defaults to “INFO”.
61 |
# File 'lib/minitest.rb', line 61 cattr_accessor :info_signal |
#parallel_executor ⇒ Object
Parallel test executor
34 |
# File 'lib/minitest.rb', line 34 cattr_accessor :parallel_executor |
#reporter ⇒ Object
Reporter object to be used for all runs.
NOTE: This accessor is only available during setup, not during runs.
51 |
# File 'lib/minitest.rb', line 51 cattr_accessor :reporter |
#seed ⇒ Object
The random seed used for this run. This is used to srand at the start of the run and between each Runnable.run.
Set via Minitest.run after processing args.
29 |
# File 'lib/minitest.rb', line 29 cattr_accessor :seed |