def __init__(self, style=None, medium=None, **kw): Fixture.__init__(self, loader=self, **kw) if style: self.style = style if medium: self.Medium = medium self.loaded = None
def setUp(self): reset_mock_call_log() self.fxt = Fixture(loader=MockLoader(), dataclass=StubSuperSet)
class TestFixture: def setUp(self): reset_mock_call_log() self.fxt = Fixture(loader=MockLoader(), dataclass=StubSuperSet) def tearDown(self): reset_mock_call_log() @attr(unit=True) def test_data_sets_up_and_tears_down_data(self): data = self.fxt.data(StubDataset1, StubDataset2) data.setup() eq_(mock_call_log[-1], (MockLoader, 'load', StubSuperSet)) data.teardown() eq_(mock_call_log[-1], (MockLoader, 'unload')) @attr(unit=True) def test_data_implements_with_statement(self): data = self.fxt.data(StubDataset1, StubDataset2) data = data.__enter__() eq_(mock_call_log[-1], (MockLoader, 'load', StubSuperSet)) data.__exit__(None, None, None) eq_(mock_call_log[-1], (MockLoader, 'unload')) @attr(unit=True) def test_with_data_decorates_a_callable(self): @self.fxt.with_data(StubDataset1, StubDataset2) def some_callable(data): mock_call_log.append(('some_callable', data.__class__)) some_callable() eq_(mock_call_log[0], (MockLoader, 'load', StubSuperSet)) eq_(mock_call_log[1], ('some_callable', Fixture.Data)) eq_(mock_call_log[2], (MockLoader, 'unload')) @attr(unit=True) def test_with_data_calls_teardown_on_error(self): @self.fxt.with_data(StubDataset1, StubDataset2) def some_callable(data): raise RuntimeError("a very bad thing") raises(RuntimeError)(some_callable)() eq_(mock_call_log[0], (MockLoader, 'load', StubSuperSet)) eq_(mock_call_log[1], (MockLoader, 'unload')) @attr(unit=True) def test_with_data_aborts_teardown_on_interrupt(self): @self.fxt.with_data(StubDataset1, StubDataset2) def some_callable(data): raise KeyboardInterrupt raises(KeyboardInterrupt)(some_callable)() eq_(mock_call_log[0], (MockLoader, 'load', StubSuperSet)) eq_(len(mock_call_log), 1, "unexpected additional calls were made: %s" % mock_call_log) @attr(unit=True) def test_with_data_raises_exception_in_teardown(self): self.fxt.loader = AbusiveMockLoader() @self.fxt.with_data(StubDataset1, StubDataset2) def some_callable(data): pass raises(ValueError)(some_callable)() eq_(mock_call_log[0], (AbusiveMockLoader, 'load', StubSuperSet)) @attr(unit=True) def test_with_data_does_soft_teardown_on_exception(self): self.fxt.loader = AbusiveMockLoader() @self.fxt.with_data(StubDataset1, StubDataset2) def some_callable(data): raise RuntimeError("a very bad thing") err = StringIO() sys.stderr = err try: raises(RuntimeError)(some_callable)() finally: sys.stderr = sys.__stderr__ saved_err = err.getvalue() assert "ValueError: An exception during teardown" in \ saved_err, ( "unexpected stderr capture: \n<<<<<<\n%s>>>>>>\n" % saved_err) eq_(mock_call_log[0], (AbusiveMockLoader, 'load', StubSuperSet)) @attr(unit=True) def test_with_data_decorates_a_generator(self): @self.fxt.with_data(StubDataset1, StubDataset2) def some_generator(): def generated_test(data, step): mock_call_log.append(('some_generator', data.__class__, step)) for step in range(4): yield generated_test, step loader = nose.loader.TestLoader() # 0.10 only .... suite = loader.loadTestsFromGenerator(some_generator, None) SilentTestRunner().run(suite) eq_(mock_call_log[0], (MockLoader, 'load', StubSuperSet)) eq_(mock_call_log[1], ('some_generator', Fixture.Data, 0)) eq_(mock_call_log[2], (MockLoader, 'unload')) eq_(mock_call_log[3], (MockLoader, 'load', StubSuperSet)) eq_(mock_call_log[4], ('some_generator', Fixture.Data, 1)) eq_(mock_call_log[5], (MockLoader, 'unload')) eq_(mock_call_log[6], (MockLoader, 'load', StubSuperSet)) eq_(mock_call_log[7], ('some_generator', Fixture.Data, 2)) eq_(mock_call_log[8], (MockLoader, 'unload')) eq_(mock_call_log[9], (MockLoader, 'load', StubSuperSet)) eq_(mock_call_log[10], ('some_generator', Fixture.Data, 3)) @attr(unit=True) def test_generated_tests_call_teardown_on_error(self): @self.fxt.with_data(StubDataset1, StubDataset2) def some_generator(): @raises(RuntimeError) def generated_test(data, step): mock_call_log.append(('some_generator', data.__class__, step)) raise RuntimeError for step in range(2): yield generated_test, step loader = nose.loader.TestLoader() # 0.10 only .... suite = loader.loadTestsFromGenerator(some_generator, None) SilentTestRunner().run(suite) eq_(mock_call_log[0], (MockLoader, 'load', StubSuperSet)) eq_(mock_call_log[1], ('some_generator', Fixture.Data, 0)) eq_(mock_call_log[2], (MockLoader, 'unload')) eq_(mock_call_log[3], (MockLoader, 'load', StubSuperSet)) eq_(mock_call_log[4], ('some_generator', Fixture.Data, 1)) eq_(mock_call_log[5], (MockLoader, 'unload')) @attr(unit=True) def test_generated_raises_exception_in_teardown(self): self.fxt.loader = AbusiveMockLoader() @self.fxt.with_data(StubDataset1, StubDataset2) def some_generator(): def generated_test(data, step): mock_call_log.append(('some_generator', data.__class__, step)) for step in range(2): yield generated_test, step loader = nose.loader.TestLoader() # 0.10 only .... suite = loader.loadTestsFromGenerator(some_generator, None) @raises(Exception) def run_tests(): SilentTestRunner().run(suite) run_tests() eq_(mock_call_log[0], (AbusiveMockLoader, 'load', StubSuperSet)) eq_(mock_call_log[1], ('some_generator', Fixture.Data, 0)) @attr(unit=True) def test_generated_error_raises_soft_exception_in_teardown(self): self.fxt.loader = AbusiveMockLoader() @self.fxt.with_data(StubDataset1, StubDataset2) def some_generator(): def generated_test(data, step): mock_call_log.append(('some_generator', data.__class__, step)) raise RuntimeError("error raised from some_generator") for step in range(2): yield generated_test, step loader = nose.loader.TestLoader() loader = nose.loader.TestLoader() # 0.10 only .... suite = loader.loadTestsFromGenerator(some_generator, None) @raises(RuntimeError) def run_tests(): SilentTestRunner().run(suite) err = StringIO() sys.stderr = err try: try: run_tests() except Exception, e: etype, val, tb = sys.exc_info() assert 'error raised from some_generator' in str(val), ( "Unexpected: %s" % val) else: