Class: Protest::Report

Inherits:
Object
  • Object
show all
Includes:
Utils::ColorfulOutput, Utils::Summaries
Defined in:
lib/protest/report.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::ColorfulOutput

colors, #print, #puts

Methods included from Utils::Summaries

#summarize_errors, #summarize_pending_tests, #summarize_test_totals

Constructor Details

#initialize(stream = STDOUT) ⇒ Report

Set the stream where the report will be written to. STDOUT by default.



9
10
11
# File 'lib/protest/report.rb', line 9

def initialize(stream=STDOUT)
  @stream = stream
end

Instance Attribute Details

#stream ⇒ Object (readonly)

:nodoc:



6
7
8
# File 'lib/protest/report.rb', line 6

def stream
  @stream
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. :test:: Fired by a test before it starts running. It will get the instance of TestCase for the given test 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.



38
39
40
41
42
43
44
45
46
47
# File 'lib/protest/report.rb', line 38

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_assertion ⇒ Object

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



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

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

#assertions ⇒ Object

Number of assertions run during the report.



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

def assertions
  @assertions || 0
end

#errors ⇒ Object

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



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

def errors
  @errors ||= []
end

#failures ⇒ Object

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



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

def failures
  @failures ||= []
end

#failures_and_errors ⇒ Object

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/protest/report.rb', line 102

def failures_and_errors
  @failures_and_errors ||= []
end

#passes ⇒ Object

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



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

def passes
  @passes ||= []
end

#pendings ⇒ Object

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



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

def pendings
  @pendings ||= []
end

#tests ⇒ Object



117
118
119
# File 'lib/protest/report.rb', line 117

def tests
  @tests ||= []
end

#time_elapsed ⇒ Object

Seconds taken since the test suite started running



127
128
129
# File 'lib/protest/report.rb', line 127

def time_elapsed
  Time.now - @started_at
end

#total_tests ⇒ Object

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



122
123
124
# File 'lib/protest/report.rb', line 122

def total_tests
  tests.size
end