def spec(world): example_suite = ExampleSuite() ExampleSuite.set_suite(example_suite) with describe('Example Group with examples added by it()') as eg: decorator = it('Example description created by it()') example = decorator(first_test_function) assert eg.examples == [example]
from io import StringIO from rgf.core.runner import ProgressFormatter from rgf.core.examples import Example class MockExampleGroup(object): def run_before_each(self, example): pass class MockExample(object): pass def mock_world_factory(): pass with describe('ProgressFormatter'): @it('can log a success to an IO-like object') def spec(world): io = StringIO() pf = ProgressFormatter(io) pf.success(MockExample(), (1, None)) assert io.getvalue() == '.' @it('can log a failure to an IO-like object') def spec(world): io = StringIO() pf = ProgressFormatter(io) pf.failure(MockExample(), (2, None)) assert io.getvalue() == 'F' @it('can log an error to an IO-like object')
from rgf.dsl import describe, it, before import os, re try: from sets import Set as set except ImportError: # `sets` module deprecated since 2.6 pass from rgf.core.runner import Collector from rgf.core.examples import ExampleSuite with describe('Collector'): @before def b(w): w.spec_file_path = os.path.abspath('fixture_specs/success/b/b_spec.py') w.spec_root_path = os.path.abspath('fixture_specs/success') @it('can find spec files in a directory hierarchy') def f(w): actual = set(Collector(w.spec_root_path).found_spec_files()) expected = ['a_spec.py', 'b/b_spec.py', 'c/d/d_spec.py'] expected = set(['%s/%s' % (w.spec_root_path, x) for x in expected]) assert actual == expected @it('can import a spec file and collect its ExampleGroups') def f(w): collector = Collector('/path/to/spec') root_module = 'rgf_anon_collector' mod = collector.import_spec_file(w.spec_file_path, root_module) assert re.compile(r'^%s\.spec_[0-9a-f]+$' % root_module).match(mod.__name__)
from rgf.dsl import describe, it with describe("This isn't going to end well"): @it('goes boom') def s(w): assert False
class MockExampleContext(object): pass def passing_spec_function(world): world.has_been_run = True def return_world(world): def returner(): return world return returner with describe("Example"): @before def b(w): w.mock_world = MockExampleContext() @it("can be run with an isolated context") def spec(w): example = Example("can be run", passing_spec_function) example.run(MockExampleGroup(), return_world(w.mock_world)) assert w.mock_world.has_been_run @it("is run with before func from context") def spec(w): example = Example("runs before method from context", passing_spec_function) example.run(MockExampleGroup(), return_world(w.mock_world))
def returner(): world = MockExampleContext() container.append(world) return world return returner def first_test_function(world): world.has_been_run = True def failing_test_function(world): assert False def before_func(world): world.before_was_run = True with describe('ExampleGroup'): @before def b(w): w.example_suite = ExampleSuite() w.eg = ExampleGroup(w.example_suite, "A group of Examples") w.eg.add_example(Example('All good', first_test_function)) w.eg.add_example(Example('Still good', first_test_function)) @it('can be created and described') def spec(w): assert w.eg.description == "A group of Examples" @it("can create a new Example") def s(w): example = Example('An example', first_test_function) w.eg.add_example(example)
from rgf.dsl import describe, it, before from rgf.core.examples import ExampleResult class MockException(object): pass class MockTraceback(object): pass with describe('ExampleResult'): @it('reports that it was a success') def s(w): result = ExampleResult.as_success() assert result.is_success() with describe('ExampleResult failed'): @before def b(w): w.mock_exception = MockException() w.mock_traceback = MockTraceback() w.result = ExampleResult.as_failure(w.mock_exception, w.mock_traceback) @it('reports that it was a failure') def s(w): assert w.result.is_failure() @it('reports that it was not a success') def s(w): assert w.result.is_not_success()
def spec(world): with describe('Example Group with before function') as eg: before(before_func) assert eg.before_function is before_func
def spec(world): eg = describe('This Example Group') assert type(eg) is ExampleGroup
from rgf.dsl import describe, it with describe('D1'): @it('D spec') def f(w): pass with describe('D2'): @it('D spec') def f(w): pass
from rgf.dsl import describe, it, before from rgf.core.examples import ExampleGroup, ExampleSuite def first_test_function(world): world.has_been_run = True def before_func(world): world.before_was_run = True with describe('DSL'): @it('provides describe helper context to create and set current ExampleGroup') def spec(world): eg = describe('This Example Group') assert type(eg) is ExampleGroup @it('provides it() decorator creator. The decorator creates Examples on the current ExampleGroup') def spec(world): example_suite = ExampleSuite() ExampleSuite.set_suite(example_suite) with describe('Example Group with examples added by it()') as eg: decorator = it('Example description created by it()') example = decorator(first_test_function) assert eg.examples == [example] @it("provides before() decorator creator. The decorator adds a function to the current ExampleGroup's before runner") def spec(world): with describe('Example Group with before function') as eg: before(before_func) assert eg.before_function is before_func
def error(self, example, result): self.errors.append(example) def summarise_results(self, *args): self.summarise_results_called_with = args def summarise_failures(self, failures): self.summarise_failures_called_with = failures def summarise_errors(self, errors): self.summarise_errors_called_with = errors def first_test_function(world): world.has_been_run = True with describe('Reporter'): @before def b(world): def failing_test_function(self): assert False def error_test_function(self): raise KeyError('grrr') example_suite = ExampleSuite() example_group = ExampleGroup(example_suite, 'reports') world.ex1 = Example('All good', first_test_function) world.ex2 = Example('Fail', failing_test_function) world.ex3 = Example('Error', error_test_function) example_group.add_example(world.ex1) example_group.add_example(world.ex2) example_group.add_example(world.ex3) io = StringIO()
from rgf.dsl import describe, it with describe('B'): @it('B spec') def f(w): pass
from rgf.dsl import describe, it with describe('A'): @it('A spec') def f(w): pass
import os.path, subprocess, re from rgf.core.examples import ExampleSuite from rgf.core.runner import Runner class MockReporter(object): def __init__(self): self.examples_ran= [] def example_ran(self, *args): self.examples_ran.append(args) def run_finished(self): self.run_finished_was_called = True with describe('Runner'): @it('can collect and run spec files through a Reporter') def f(w): reporter = MockReporter() runner = Runner(reporter) suite = ExampleSuite() runner.run(suite, os.path.abspath(os.path.join(__file__, '../../../../../fixture_specs/success'))) assert len(reporter.examples_ran) > 0 with describe('rgf script'): def run_spec_script(spec_path): p = subprocess.Popen('./cold_runner %s' % spec_path, shell = True, stdout = subprocess.PIPE) output, p_null = p.communicate() return_val = p.wait() return (return_val, output)
def __init__(self): self.examples_ran= [] def example_ran(self, *args): self.examples_ran.append(args) def run_finished(self): self.run_finished_was_called = True def first_test_function(world): world.has_been_run = True def before_func(world): world.before_was_run = True with describe('ExampleSuite'): @before def b(w): w.suite = ExampleSuite() @it('can collect many ExampleGroups') def spec(w): example_group = w.suite.add_example_group('ExampleGroup description') assert type(example_group) is ExampleGroup assert example_group in w.suite.example_groups @it('returns itself as the current ExampleGroup if there is none') def spec(w): assert w.suite.get_current_example_group() is w.suite @it('allows the current ExampleGroup to be set')