Module: Kintama

Defined in:
lib/kintama.rb,
lib/kintama/test.rb,
lib/kintama/runner.rb,
lib/kintama/context.rb,
lib/kintama/reporter.rb,
lib/kintama/runnable.rb,
lib/kintama/assertions.rb

Defined Under Namespace

Modules: Assertions, Context, Mocha, Test Classes: Reporter, Runnable, Runner, TestFailure

Class Method Summary collapse

Class Method Details

.add_exit_hookObject

Adds the hook to automatically run all known tests using #run when ruby exits; this is most useful when running a test file from the command line or from within an editor



112
113
114
115
116
# File 'lib/kintama.rb', line 112

def add_exit_hook
  return if instance_variable_defined?(:@__added_exit_hook)
  at_exit { exit(options.runner.with(Kintama.default_context).run(options.reporter) ? 0 : 1) }
  @__added_exit_hook = true
end

.context(name, parent = default_context, &block) ⇒ Object Also known as: testcase, describe

Create a new context. Aliases are ‘testcase’ and ‘describe’



29
30
31
# File 'lib/kintama.rb', line 29

def context(name, parent=default_context, &block)
  default_context.context(name, parent, &block)
end

.default_contextObject



23
24
25
26
# File 'lib/kintama.rb', line 23

def default_context
  reset unless instance_variable_defined?(:@default_context) && @default_context
  @default_context
end

.extend(mod) ⇒ Object

Make new testing behaviour available for the definition of tests. Methods included in this way are available during the definition of tests.



75
76
77
# File 'lib/kintama.rb', line 75

def extend(mod)
  default_context.extend(mod)
end

.given(name, parent = default_context, &block) ⇒ Object

Create a new context starting with “given ”



36
37
38
# File 'lib/kintama.rb', line 36

def given(name, parent=default_context, &block)
  default_context.given(name, parent, &block)
end

.include(mod) ⇒ Object

Makes behaviour available within tests:

module SomeModule
  def blah
  end
end
Kintama.include SomeModule

Any methods will then be available within setup, teardown or tests.



69
70
71
# File 'lib/kintama.rb', line 69

def include(mod)
  default_context.send(:include, mod)
end

.no_conflict?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/kintama.rb', line 14

def no_conflict?
  ENV["KINTAMA_NO_CONFLICT"]
end

.on_finish(&block) ⇒ Object Also known as: after_all



55
56
57
# File 'lib/kintama.rb', line 55

def on_finish(&block)
  default_context.on_finish(&block)
end

.on_start(&block) ⇒ Object Also known as: before_all



50
51
52
# File 'lib/kintama.rb', line 50

def on_start(&block)
  default_context.on_start(&block)
end

.optionsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/kintama.rb', line 79

def options
  @options ||= begin
    options = OpenStruct.new(
      :reporter => Kintama::Reporter.default,
      :runner => Kintama::Runner.default
    )
    cmd_options = OptionParser.new do |opts|
      opts.banner = "Usage: ruby <test_file> [options]"

      opts.separator ""
      opts.separator "Specific options:"

      opts.on("-r", "--reporter NAME",
              "Use the given reporter (inline or verbose)") do |reporter|
        options.reporter = Kintama::Reporter.called(reporter)
      end
      opts.on("-l", "--line LINE",
              "Run the test or context on the given line") do |line|
        options.runner = Kintama::Runner::Line.new(line)
      end
      opts.on_tail("-h", "--help", "Show this message") do
        puts opts
        exit
      end
    end
    cmd_options.parse!(ARGV)
    options
  end
end

.resetObject



18
19
20
21
# File 'lib/kintama.rb', line 18

def reset
  @default_context = Class.new(Runnable)
  @default_context.send(:include, Kintama::Context)
end

.setup(&block) ⇒ Object

Add a setup which will run at the start of every test.



41
42
43
# File 'lib/kintama.rb', line 41

def setup(&block)
  default_context.setup(&block)
end

.should_run_on_exit?Boolean

Tries to determine whether or not this is a sensible situation to automatically run all tests when ruby exits. At the moment, this is true when either:

  • the test was run via rake

  • the test file was run as the top-level ruby script

This method will always return false if the environment variable KINTAMA_EXPLICITLY_DONT_RUN is not nil.

Returns:

  • (Boolean)


125
126
127
128
# File 'lib/kintama.rb', line 125

def should_run_on_exit?
  return false if ENV["KINTAMA_EXPLICITLY_DONT_RUN"]
  return test_file_was_run? || run_via_rake?
end

.teardown(&block) ⇒ Object

Add a teardown which will be run at the end of every test.



46
47
48
# File 'lib/kintama.rb', line 46

def teardown(&block)
  default_context.teardown(&block)
end