def failing_mock_expectation(slf): expect(mock()).foo # emulate a refresh try: core._teardown() finally: core._setup()
def test_reality_formatting(self): core._teardown() try: with MockTransaction: m = mock('meth') expect(m).meth.once() m.meth(1,2,3) m.meth(foo='bar') m.meth() m.meth(1, foo=2) except AssertionError, e: line_agnostic_repr = [ re.sub('\.py:[0-9 ]{3} ', '.py:LINE ', line) for line in str(e).split('\n')] expected_lines = [ 'Mock "meth" did not match expectations:', ' expected exactly 1 calls', ' received 4 calls with arguments:', ' 1: (1, 2, 3) // mocktest_test.py:LINE :: m.meth(1,2,3)', " 2: (foo='bar') // mocktest_test.py:LINE :: m.meth(foo='bar')", ' 3: () // mocktest_test.py:LINE :: m.meth()', ' 4: (1, foo=2) // mocktest_test.py:LINE :: m.meth(1, foo=2)'] for got, expected in zip(line_agnostic_repr, expected_lines): self.assertEqual(got, expected)
def test_reality_formatting(self): core._teardown() try: with MockTransaction: m = mock('meth') expect(m).meth.once() m.meth(1, 2, 3) m.meth(foo='bar') m.meth() m.meth(1, foo=2) except AssertionError as e: line_agnostic_repr = [ re.sub('\.py:[0-9 ]{3} ', '.py:LINE ', line) for line in str(e).split('\n') ] expected_lines = [ 'Mock "meth" did not match expectations:', ' expected exactly 1 calls', ' received 4 calls with arguments:', ' 1: (1, 2, 3) // mocktest_test.py:LINE :: m.meth(1,2,3)', " 2: (foo='bar') // mocktest_test.py:LINE :: m.meth(foo='bar')", ' 3: () // mocktest_test.py:LINE :: m.meth()', ' 4: (1, foo=2) // mocktest_test.py:LINE :: m.meth(1, foo=2)' ] for got, expected in zip(line_agnostic_repr, expected_lines): self.assertEqual(got, expected) finally: core._setup()
def test_should_ignore_with_description(self): callback = mock() expect(callback).__call__.never() @ignore('not done yet') def test_failure(self): callback('a') self.assert_(self.run_method(test_failure).wasSuccessful())
def test_ignore(self): callback = mock() expect(callback).__call__.never() @ignore def test_failure(self): callback('a') self.assert_(self.run_method(test_failure).wasSuccessful())
def test_should_do_expectations(self): try: with MockTransaction: f = mock() expect(f).foo.once() f.foo('a') f.foo() raise RuntimeError("should not reach here!") except AssertionError, e: assert re.compile('Mock "foo" .*expected exactly 1 calls.* received 2 calls.*', re.DOTALL).match(str(e)), str(e)
def test_is_not_expected(self): wrapper = mock() mock_ = wrapper.raw expect(mock_.a).once() wrapper.child('b').is_not_expected mock_.a() core._teardown() core._setup()
def test_runner(self): from pyqcy.properties import Property with MockTransaction: when(sys).exit.then_return(None) assert main(__name__, exit=True) == len([ obj for obj in globals().itervalues() if isinstance(obj, Property) ]) # we have one failing property so expect a failure expect(sys).exit.where(lambda code: code != 0).once()
def test_should_do_expectations(self): try: with MockTransaction: f = mock() expect(f).foo.once() f.foo('a') f.foo() raise RuntimeError("should not reach here!") except AssertionError as e: assert re.compile( 'Mock "foo" .*expected exactly 1 calls.* received 2 calls.*', re.DOTALL).match(str(e)), str(e)
def test_expect_should_work_on_a_mock_or_wrapper(self): wrapper = mock() mock_ = wrapper.raw expect(mock_.a).once() wrapper.expects('b').once() wrapper.child('c').is_expected.once() self.assertTrue(len(core.MockWrapper._all_expectations) == 3) mock_.a() mock_.b() mock_.c()
def test_expect_should_work_on_a_mock_or_wrapper(self): wrapper = mock_wrapper() mock = wrapper.mock expect(mock.a).once() wrapper.expects("b").once() wrapper.child("c").is_expected.once() self.assertTrue(len(mocktest.mockwrapper.MockWrapper._all_expectations) == 3) mock.a() mock.b() mock.c()
def test_invalid_usage_after_teardown(self): core._teardown() try: e = None try: m = mock() expect(m).foo().never() except Exception, e_: print repr(e_) e = e_ self.assertFalse(e is None, "no exception was raised") self.assertEqual(str(e), "Mock transaction has not been started. Make sure you are inheriting from mocktest.TestCase") self.assertEqual(e.__class__, AssertionError)
def test_invalid_usage_after_teardown(self): core._teardown() try: e = None try: m = mock() expect(m).foo().never() except Exception as e_: print(repr(e_)) e = e_ self.assertFalse(e is None, "no exception was raised") self.assertEqual( str(e), "Mock transaction has not been started. Make sure you are inheriting from mocktest.TestCase" ) self.assertEqual(e.__class__, AssertionError) finally: core._setup()
def test_one(self): expect(foo).split('a')
def mock_failure(slf): expect(mock()).foo().at_least.once()
def test_a(self): foo = mock() expect(foo).blah.once()