Class BareTest::Run
In: lib/baretest/run/minimal.rb
lib/baretest/run/cli.rb
lib/baretest/run/xml.rb
lib/baretest/run/tap.rb
lib/baretest/run/spec.rb
lib/baretest/run/none.rb
lib/baretest/run.rb
Parent: Object

Run is the environment in which the suites and assertions are executed. Prior to the execution, the Run instance extends itself with the formatter that should be used and other runner related toolsets. Your formatter can override:

:run_all:Invoked once, before the first run_suite is ran. No arguments.
:run_suite:Invoked per suite. Takes the suite to run as argument.
:run_test_variants:Invoked per assertion. Takes the assertion to execute as argument.
:run_test:Invoked per setup variation of each assertion. Takes the assertion to execute and the setup blocks to use as arguments.

Don‘t forget to call super within your overrides, or the tests won‘t be executed.

Methods

Attributes

count  [R]  Some statistics, standard count keys are:
:test:the number of tests executed until now
:suite:the number of suites executed until now
<status>:the number of tests with that status (see BareTest::StatusOrder for a list of states)
inits  [R]  The initialisation blocks of extenders
suite  [R]  The toplevel suite.

Public Class methods

Run the passed suite. Calls run_all with the toplevel suite as argument and a block that calls run_suite with the yielded argument (which should be the toplevel suite). Options accepted:

  • :extenders: An Array of Modules, will be used as argument to self.extend, useful e.g. for mock integration
  • :format: A string with the basename (without suffix) of the formatter to use - or a Module
  • :interactive: true/false, will switch this Test::Run instance into IRB mode, where an error will cause an irb session to be started in the context of a clean copy of the assertion with all setup callbacks invoked

The order of extensions is:

  • :extender
  • :format (extends with the formatter module)
  • :interactive (extends with IRBMode)

[Source]

     # File lib/baretest/run.rb, line 61
 61:     def initialize(suite, opts=nil)
 62:       @suite           = suite
 63:       @inits           = []
 64:       @options         = opts || {}
 65:       @count           = @options[:count] || Hash.new(0)
 66:       @provided        = [] # Array's set operations are the fastest
 67:       @include_tags    = @options[:include_tags]   # nil is ok here
 68:       @exclude_tags    = @options[:exclude_tags]   # nil is ok here
 69:       include_states   = @options[:include_states] # nil is ok here
 70:       exclude_states   = @options[:exclude_states] # nil is ok here
 71:       @states          = [nil, *BareTest::StatusOrder]
 72:       @skipped         = {}
 73:       @last_run_states = {}
 74:       @persistence     = @options[:persistence]
 75: 
 76:       if (include_states || exclude_states) && !((include_states && include_states.empty?) && (exclude_states && exclude_states.empty?)) then
 77:         [include_states, exclude_states].compact.each do |states|
 78:           states << nil if states.include?(:new)
 79:           states.push(:error, :skipped, :pending) if states.include?(:failure)
 80:           states.delete(:new)
 81:           if states.include?(:skipped) then
 82:             states.delete(:skipped)
 83:             states.push(:pending, :manually_skipped, :dependency_missing, :library_missing, :component_missing)
 84:           end
 85:           states.uniq!
 86:         end
 87:         @states = (include_states || @states) - (exclude_states || [])
 88:       end
 89: 
 90:       (BareTest.extender+Array(@options[:extender])).each do |extender|
 91:         extend(extender)
 92:       end
 93: 
 94:       # Extend with the output formatter
 95:       format = @options[:format]
 96:       if format.is_a?(String) then
 97:         require "baretest/run/#{format}"
 98:         extend(BareTest.format["baretest/run/#{format}"])
 99:       elsif format.is_a?(Module) then
100:         extend(format)
101:       end
102: 
103:       # Extend with irb dropout code
104:       extend(BareTest::IRBMode) if @options[:interactive]
105: 
106:       # Initialize extenders
107:       @inits.each { |init| instance_eval(&init) }
108:     end

Public Instance methods

Status over all tests ran up to now Can be :error, :failure, :incomplete or :success The algorithm is a simple fall through: if any test errored, then global_status is :error, if not, then if any test failed, global_status is :failure, if not, then if any test was pending or skipped, global_status is :incomplete, if not, then global_status is success

[Source]

     # File lib/baretest/run.rb, line 237
237:     def global_status
238:       status_counts         = @count.values_at(*BareTest::StatusOrder)
239:       most_important_status = BareTest::StatusOrder.zip(status_counts) { |status, count|
240:         break status if count > 0
241:       } || :success
242:     end

Hook initializers for extenders. Blocks passed to init will be instance_eval‘d at the end of initialize. Example usage:

  module ExtenderForRun
    def self.extended(run_obj)
       run_obj.init do
         # do some initialization stuff for this module
       end
    end
  end

[Source]

     # File lib/baretest/run.rb, line 120
120:     def init(&block)
121:       @inits << block
122:     end

Get an assertions’ interpolated description for a given Array of Setup instances. See Assertion#interpolated_description

[Source]

     # File lib/baretest/run.rb, line 247
247:     def interpolated_description(assertion, setups)
248:       setups = setups ? setups.select { |s| s.component } : []
249:       substitutes = {}
250:       setups.each do |setup| substitutes[setup.component] = setup.substitute end
251:       assertion.interpolated_description(substitutes)
252:     end

Formatter callback. Invoked once at the beginning. Gets the toplevel suite as single argument.

[Source]

     # File lib/baretest/run.rb, line 127
127:     def run_all
128:       @last_run_states = @persistence ? @persistence.read('final_states', {}) : {}
129:       @skipped         = {}
130:       run_suite(@suite)
131:       @persistence.store('final_states', @last_run_states) if @persistence
132:     end

Formatter callback. Invoked once for every suite. Gets the suite to run as single argument. Runs all assertions and nested suites.

[Source]

     # File lib/baretest/run.rb, line 138
138:     def run_suite(suite)
139:       missing_tags     = @include_tags && @include_tags - suite.tags
140:       superfluous_tags = @exclude_tags && suite.tags & @exclude_tags
141:       ignored          = (missing_tags && !missing_tags.empty?) || (superfluous_tags && !superfluous_tags.empty?)
142: 
143:       unless ignored then
144:         unmet_dependencies  = (suite.depends_on-@provided)
145:         manually_skipped    = suite.skipped?
146:         recursively_skipped = !unmet_dependencies.empty? || manually_skipped
147:         skipped             = @skipped[suite] || recursively_skipped
148: 
149:         if recursively_skipped then
150:           skip_recursively(suite, "Ancestor was skipped")
151:         elsif skipped then
152:           skip_suite(suite, "Container was skipped")
153:         end
154:       end
155: 
156:       if ignored then
157:         states = []
158:       else
159:         states = suite.assertions.map do |test|
160:           run_test_variants(test)
161:         end
162:       end
163:       states.concat(suite.suites.map { |(description, subsuite)|
164:         run_suite(subsuite)
165:       })
166:       @count[:suite] += 1
167: 
168:       # || in case the suite contains no tests or suites
169:       final_status = BareTest.most_important_status(states) || :pending
170: 
171:       @provided |= suite.provides if final_status == :success
172: 
173:       Status.new(suite, final_status)
174:     end

Formatter callback. Invoked once for every variation of an assertion. Gets the assertion to run as single argument.

[Source]

     # File lib/baretest/run.rb, line 205
205:     def run_test(assertion, setup)
206:       rv = assertion.execute(setup, assertion.suite.ancestry_teardown)
207:       @count[:test]     += 1
208:       @count[rv.status] += 1
209: 
210:       rv
211:     end

Invoked once for every assertion. Iterates over all variants of an assertion and invokes run_test for each.

[Source]

     # File lib/baretest/run.rb, line 179
179:     def run_test_variants(assertion)
180:       ignored = !@states.include?(@last_run_states[assertion.id])
181:       skipped = @skipped[assertion] || assertion.skipped?
182: 
183:       if ignored then
184:         overall_status = nil
185:       elsif skipped then
186:         Array.new(assertion.suite.component_variant_count) { run_test(assertion, []) }
187:         @last_run_states[assertion.id] = :manually_skipped
188:         overall_status                 = :manually_skipped
189:       else
190:         states = []
191:         assertion.suite.each_component_variant do |setups|
192:           rv = run_test(assertion, setups)
193:           states << rv.status
194:         end
195:         overall_status                 = BareTest.most_important_status(states)
196:       end
197:       @last_run_states[assertion.id] = overall_status if overall_status
198: 
199:       overall_status
200:     end

[Validate]