Class: Testicles::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/testicles/report.rb

Direct Known Subclasses

Testicles::Reports::Progress

Defined Under Namespace

Classes: ErroredTest, FailedTest, PassedTest, PendingTest

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#time_elapsedObject

Seconds taken to run the test suite.

TODO: this doesn’t belong here, it’s being set from within the test runner. But we need the value here.



7
8
9
# File 'lib/testicles/report.rb', line 7

def time_elapsed
  @time_elapsed
end

Class Method Details

.on(event, &block) ⇒ Object

Define an event handler for your report. The different events fired in a report’s life cycle are:

:start

Fired by the runner when starting the whole test suite.

:enter

Fired by the runner when starting a particular test case. It will get the test case as an argument.

:assertion

Fired by a test each time an assertion is run.

:pass

Fired by a test after it runs successfully without errors. It will get an instance of PassedTest as an argument.

:pending

Fired by a test which doesn’t provide a test block or which calls TestCase#pending. It will get an instance of PendingTest as an argument.

:failure

Fired by a test in which an assertion failed. It will get an instance of FailedTest as an argument.

:error

Fired by a test where an uncaught exception was found. It will get an instance of ErroredTest as an argument.

:exit

Fired by the runner each time a test case finishes. It will take the test case as an argument.

:end

Fired by the runner at the end of the whole test suite.

The event handler will receive the report as a first argument, plus any arguments documented above (depending on the event). It will also ensure that any handler for the same event declared on an ancestor class is run.



32
33
34
35
36
37
38
39
40
41
# File 'lib/testicles/report.rb', line 32

def self.on(event, &block)
  define_method(:"on_#{event}") do |*args|
    begin
      super(*args)
    rescue NoMethodError
    end

    block.call(self, *args)
  end
end

Instance Method Details

#add_assertionObject

Log an assertion was run (whether it succeeded or failed.)



107
108
109
110
# File 'lib/testicles/report.rb', line 107

def add_assertion
  @assertions ||= 0
  @assertions += 1
end

#assertionsObject

Number of assertions run during the report.



113
114
115
# File 'lib/testicles/report.rb', line 113

def assertions
  @assertions || 0
end

#errorsObject

List all the tests (as ErroredTest instances) that raised an unrescued exception.



96
97
98
# File 'lib/testicles/report.rb', line 96

def errors
  @errors ||= []
end

#failuresObject

List all the tests (as FailedTest instances) that failed an assertion.



90
91
92
# File 'lib/testicles/report.rb', line 90

def failures
  @failures ||= []
end

#failures_and_errorsObject

Aggregated and ordered list of tests that either failed an assertion or raised an unrescued exception. Useful for displaying back to the user.



102
103
104
# File 'lib/testicles/report.rb', line 102

def failures_and_errors
  @failures_and_errors ||= []
end

#passesObject

List all the tests (as PassedTest instances) that passed.



85
86
87
# File 'lib/testicles/report.rb', line 85

def passes
  @passes ||= []
end

#pendingsObject

List all the tests (as PendingTest instances) that were pending.



80
81
82
# File 'lib/testicles/report.rb', line 80

def pendings
  @pendings ||= []
end

#report(name, report_success = true) ⇒ Object

Run a test and report if it passes, fails, or is pending. Takes the name of the test as an argument. You can avoid reporting a passed test by passing false as a second argument.



68
69
70
71
72
73
74
75
76
77
# File 'lib/testicles/report.rb', line 68

def report(name, report_success=true)
  yield
  on_pass(PassedTest.new(name)) if report_success
rescue Pending => e
  on_pending(PendingTest.new(name, e))
rescue AssertionFailed => e
  on_failure(FailedTest.new(name, e))
rescue Exception => e
  on_error(ErroredTest.new(name, e))
end

#total_testsObject

Amount ot tests run (whether passed, pending, failed, or errored.)



118
119
120
# File 'lib/testicles/report.rb', line 118

def total_tests
  passes.size + failures.size + errors.size + pendings.size
end