Exemple #1
0
    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'
Exemple #2
0
    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'
Exemple #3
0
    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'
Exemple #4
0
    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()
Exemple #6
0
 def verify_and_teardown_doubles():
     try:
         verify()
     except Exception as e:
         pytest.fail(str(e))
     finally:
         teardown()
Exemple #7
0
    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'
Exemple #8
0
    def test_restores_original_value(self):
        original_value = doubles.testing.User
        patch_class('doubles.testing.User')

        teardown()

        assert original_value == doubles.testing.User
Exemple #9
0
    def test_restores_original_value(self):
        original_value = doubles.testing.User
        patch_class('doubles.testing.User')

        teardown()

        assert original_value == doubles.testing.User
Exemple #10
0
        def test_unsatisfied_expectation(self, test_class):
            TestClass = ClassDouble(test_class)

            expect_constructor(TestClass)
            with raises(MockExpectationError):
                verify()
            teardown()
Exemple #11
0
        def test_unsatisfied_expectation(self, test_class):
            TestClass = ClassDouble(test_class)

            expect_constructor(TestClass)
            with raises(MockExpectationError):
                verify()
            teardown()
Exemple #12
0
    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()
Exemple #13
0
    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()
Exemple #14
0
def pytest_runtest_call(item):
    outcome = yield

    try:
        outcome.get_result()
        verify()
    finally:
        teardown()
Exemple #15
0
    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
Exemple #16
0
    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'
Exemple #17
0
    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
Exemple #18
0
    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))
Exemple #19
0
    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'
Exemple #20
0
    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()
Exemple #21
0
    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()
Exemple #22
0
    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'
Exemple #23
0
    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'
Exemple #24
0
    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
Exemple #25
0
    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'
Exemple #26
0
    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'
Exemple #27
0
    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)
        )
Exemple #28
0
    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
Exemple #29
0
    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))
Exemple #30
0
    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))
Exemple #31
0
    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))
Exemple #32
0
    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))
Exemple #33
0
    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))
Exemple #34
0
    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)
        )
Exemple #35
0
    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)
        )
Exemple #36
0
    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)
        )
Exemple #37
0
    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))
Exemple #38
0
    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)
        )
Exemple #39
0
    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)
        )
Exemple #40
0
    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)
        )
Exemple #41
0
    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)
        )
Exemple #42
0
    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)
        )
Exemple #43
0
 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'
Exemple #44
0
 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'
Exemple #45
0
 def afterTest(self, test):
     teardown()
Exemple #46
0
def pytest_runtest_call(item, __multicall__):
    try:
        __multicall__.execute()
        verify()
    finally:
        teardown()
Exemple #47
0
 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'
Exemple #48
0
        def test_with_invalid_args(self, test_class):
            TestClass = ClassDouble(test_class)

            with raises(VerifyingDoubleArgumentError):
                expect_constructor(TestClass).with_args(10)
            teardown()
Exemple #49
0
    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'
Exemple #50
0
 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'
Exemple #51
0
 def afterTest(self, test):
     teardown()
Exemple #52
0
 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'
Exemple #53
0
        def test_with_invalid_args(self, test_class):
            TestClass = ClassDouble(test_class)

            with raises(VerifyingDoubleArgumentError):
                expect_constructor(TestClass).with_args(10)
            teardown()
Exemple #54
0
 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'
Exemple #55
0
 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'
Exemple #56
0
 def verify_and_teardown_doubles():
     try:
         verify()
     finally:
         teardown()
Exemple #57
0
 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'