def test_restores_instance_methods_on_teardown(self, test_class): user = test_class('Alice', 25) allow(user).get_name.and_return('Bob') teardown() assert user.get_name() == 'Alice'
def test_callable_instance_attribute(self, test_class): user = test_class('Alice', 25) allow(user).callable_instance_attribute.and_return('Bob Barker') assert user.callable_instance_attribute() == 'Bob Barker' teardown() assert user.callable_instance_attribute() == 'dummy result'
def test_restores_class_methods_on_teardown(self, test_class): allow(test_class).class_method.and_return('overridden value') teardown() assert test_class.class_method( 'foo') == 'class_method return value: foo'
def verify_and_teardown_doubles(): try: verify() except Exception as e: pytest.fail(str(e)) finally: teardown()
def test_restores_original_value(self): original_value = doubles.testing.User patch_class('doubles.testing.User') teardown() assert original_value == doubles.testing.User
def test_unsatisfied_expectation(self, test_class): TestClass = ClassDouble(test_class) expect_constructor(TestClass) with raises(MockExpectationError): verify() teardown()
def test_raises_if_no_arguments_supplied(self, stubber): subject = InstanceDouble('doubles.testing.User') with raises(TypeError) as e: stubber(subject).instance_method.and_return() assert str(e.value) == 'and_return() expected at least 1 return value' teardown()
def pytest_runtest_call(item): outcome = yield try: outcome.get_result() verify() finally: teardown()
def test_teardown_restores_previous_functionality(self, test_class): user = test_class('Alice', 25) allow(user).__exit__.and_return('bob barker') assert user.__exit__(None, None, None) == 'bob barker' teardown() assert user.__exit__(None, None, None) is None
def test_teardown_restores_previous_functionality(self, test_class): user = test_class('Alice', 25) allow(user).__call__.and_return('bob barker') assert user() == 'bob barker' teardown() assert user() == 'user was called'
def test_raises_if_called_with_negative_value(self): subject = InstanceDouble('doubles.testing.User') with raises(TypeError) as e: allow(subject).instance_method.at_most(-1).times teardown() assert re.match(r"at_most requires one positive integer argument", str(e.value))
def test_takes_precedence_over_subsequent_allowances(self): subject = InstanceDouble('doubles.testing.User') expect(subject).instance_method allow(subject).instance_method.and_return('foo') with raises(MockExpectationError): verify() teardown()
def test_restores_constructor_on_teardown(self): user = object() allow(User).__new__.and_return(user) teardown() result = User('Alice', 25) assert result is not user assert result.name == 'Alice'
def test_teardown_restores_previous_functionality(self, test_class): user = test_class('Alice', 25) allow(user).__enter__.and_return('bob barker') with user as u: assert u == 'bob barker' teardown() with user as u: assert u == user
def test_teardown_restores_properties(self, test_class): user_1 = test_class('Bob', 25) user_2 = test_class('Drew', 25) allow(user_1).some_property.and_return('Barker') allow(user_2).some_property.and_return('Carey') teardown() assert user_1.some_property == 'some_property return value' assert user_2.some_property == 'some_property return value'
def test_raises_if_called_with_negative_value(self): subject = InstanceDouble('doubles.testing.User') with raises(TypeError) as e: allow(subject).instance_method.at_most(-1).times teardown() assert re.match( r"at_most requires one positive integer argument", str(e.value) )
def test_called_with_zero(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.exactly(0).times with raises(MockExpectationError) as e: subject.instance_method() teardown() assert re.match( r"Allowed 'instance_method' to be called 0 times instead of 1 " r"time on <InstanceDouble of <class 'doubles.testing.User'> " r"object at .+> with any args, but was not." r" \(.*doubles/test/allow_test.py:\d+\)", str(e.value))
def test_unsatisfied_expectation(self): subject = InstanceDouble('doubles.testing.User') expect(subject).__exit__.once() with raises(MockExpectationError) as e: verify() teardown() assert re.match( r"Expected '__exit__' to be called 1 time instead of 0 times on " r"<InstanceDouble of <class 'doubles.testing.User'> object at .+> " r"with any args, but was not." r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value))
def test_fails_when_called_more_than_at_most_times(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.at_most(1).times subject.instance_method() with raises(MockExpectationError) as e: subject.instance_method() teardown() assert re.match( r"Allowed 'instance_method' to be called at most 1 time instead of 2 times on " r"<InstanceDouble of <class 'doubles.testing.User'> object at .+> " r"with any args, but was not." r" \(.*doubles/test/allow_test.py:\d+\)", str(e.value))
def test_fails_when_called_less_than_at_least_times(self): subject = InstanceDouble('doubles.testing.User') expect(subject).instance_method.at_least(2).times subject.instance_method() with raises(MockExpectationError) as e: verify() teardown() assert re.match( r"Expected 'instance_method' to be called at least 2 times instead of 1 time on " r"<InstanceDouble of <class 'doubles.testing.User'> object at .+> " r"with any args, but was not." r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value))
def test_raises_if_an_expected_method_call_with_args_is_not_made(self): subject = InstanceDouble('doubles.testing.User') expect(subject).method_with_varargs.with_args('bar') with raises(MockExpectationError) as e: verify() teardown() assert re.match( r"Expected 'method_with_varargs' to be called on " r"<InstanceDouble of <class '?doubles.testing.User'?" r"(?: at 0x[0-9a-f]{9})?> object at .+> " r"with \('bar'\), but was not." r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value))
def test_called_with_zero(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.exactly(0).times with raises(MockExpectationError) as e: subject.instance_method() teardown() assert re.match( r"Allowed 'instance_method' to be called 0 times but was called 1 " r"time on <InstanceDouble of <class 'doubles.testing.User'> " r"object at .+> with any args, but was not." r" \(.*doubles/test/allow_test.py:\d+\)", str(e.value) )
def test_unsatisfied_expectation(self): subject = InstanceDouble('doubles.testing.User') expect(subject).__exit__.once() with raises(MockExpectationError) as e: verify() teardown() assert re.match( r"Expected '__exit__' to be called 1 time instead of 0 times on " r"<InstanceDouble of <class 'doubles.testing.User'> object at .+> " r"with any args, but was not." r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value) )
def test_fails_when_called_once_times(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.never() with raises(MockExpectationError) as e: subject.instance_method() teardown() assert re.match( r"Allowed 'instance_method' to be called 0 times instead of 1 " r"time on <InstanceDouble of <class 'doubles.testing.User'> " r"object at .+> with any args, but was not." r" \(.*doubles/test/allow_test.py:\d+\)", str(e.value) )
def test_with_args_validator_not_called(self): subject = InstanceDouble('doubles.testing.User') def arg_matcher(*args): return True expect(subject).method_with_varargs.with_args_validator(arg_matcher) with raises(MockExpectationError) as e: verify() teardown() assert re.match( r"Expected 'method_with_varargs' to be called on " r"<InstanceDouble of <class '?doubles.testing.User'?" r"(?: at 0x[0-9a-f]{9})?> object at .+> " r"with custom matcher: 'arg_matcher', but was not." r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value))
def test_fails_when_called_more_than_expected_times(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.exactly(1).times subject.instance_method() with raises(MockExpectationError) as e: subject.instance_method() teardown() assert re.match( r"Allowed 'instance_method' to be called 1 time but was called 2 times on " r"<InstanceDouble of <class 'doubles.testing.User'> object at .+> " r"with any args, but was not." r" \(.*doubles/test/allow_test.py:\d+\)", str(e.value) )
def test_fails_when_called_once(self): subject = InstanceDouble('doubles.testing.User') expect(subject).instance_method.twice() subject.instance_method() with raises(MockExpectationError) as e: verify() teardown() assert re.match( r"Expected 'instance_method' to be called 2 times instead of 1 time on " r"<InstanceDouble of <class 'doubles.testing.User'> object at .+> " r"with any args, but was not." r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value) )
def test_fails_when_called_more_than_at_most_times(self): subject = InstanceDouble('doubles.testing.User') expect(subject).instance_method.at_most(1).times subject.instance_method() with raises(MockExpectationError) as e: subject.instance_method() teardown() assert re.match( r"Expected 'instance_method' to be called at most 1 time instead of 2 times on " r"<InstanceDouble of <class 'doubles.testing.User'> object at .+> " r"with any args, but was not." r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value) )
def test_raises_if_an_expected_method_call_with_default_args_is_not_made(self): subject = InstanceDouble('doubles.testing.User') expect(subject).method_with_default_args.with_args('bar', bar='barker') with raises(MockExpectationError) as e: verify() teardown() assert re.match( r"Expected 'method_with_default_args' to be called on " r"<InstanceDouble of <class '?doubles.testing.User'?" r"(?: at 0x[0-9a-f]{9})?> object at .+> " r"with \('bar', bar='barker'\), but was not." r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value) )
def test_with_args_validator_not_called(self): subject = InstanceDouble('doubles.testing.User') def arg_matcher(*args): return True expect(subject).method_with_varargs.with_args_validator(arg_matcher) with raises(MockExpectationError) as e: verify() teardown() assert re.match( r"Expected 'method_with_varargs' to be called on " r"<InstanceDouble of <class '?doubles.testing.User'?" r"(?: at 0x[0-9a-f]{9})?> object at .+> " r"with custom matcher: 'arg_matcher', but was not." r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value) )
def test_arbitrary_callable_on_instance(self, test_class): instance = test_class('Bob', 10) allow(instance).arbitrary_callable.and_return('Bob Barker') assert instance.arbitrary_callable() == 'Bob Barker' teardown() assert instance.arbitrary_callable() == 'ArbitraryCallable Value'
def test_arbitrary_callable_on_class(self, test_class): allow(test_class).arbitrary_callable.and_return('Bob Barker') assert test_class.arbitrary_callable() == 'Bob Barker' teardown() assert test_class.arbitrary_callable() == 'ArbitraryCallable Value'
def afterTest(self, test): teardown()
def pytest_runtest_call(item, __multicall__): try: __multicall__.execute() verify() finally: teardown()
def test_restores_the_orignal_method(self): allow(doubles.testing).top_level_function.and_return('foo') teardown() assert doubles.testing.top_level_function('foo', 'bar') == 'foo -- bar'
def test_with_invalid_args(self, test_class): TestClass = ClassDouble(test_class) with raises(VerifyingDoubleArgumentError): expect_constructor(TestClass).with_args(10) teardown()
def test_restores_class_methods_on_teardown(self, test_class): allow(test_class).class_method.and_return('overridden value') teardown() assert test_class.class_method('foo') == 'class_method return value: foo'
def test_callable_class_attribute(self, test_class): allow(test_class).callable_class_attribute.and_return('Bob Barker') assert test_class.callable_class_attribute() == 'Bob Barker' teardown() assert test_class.callable_class_attribute() == 'dummy result'
def verify_and_teardown_doubles(): try: verify() finally: teardown()