Esempio n. 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'
Esempio n. 2
0
    def test_restores_original_value(self):
        original_value = doubles.testing.User
        patch_class('doubles.testing.User')

        teardown()

        assert original_value == doubles.testing.User
Esempio n. 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'
Esempio n. 4
0
 def verify_and_teardown_doubles():
     try:
         verify()
     except Exception as e:
         pytest.fail(str(e))
     finally:
         teardown()
Esempio n. 5
0
        def test_unsatisfied_expectation(self, test_class):
            TestClass = ClassDouble(test_class)

            expect_constructor(TestClass)
            with raises(MockExpectationError):
                verify()
            teardown()
Esempio n. 6
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()
Esempio n. 7
0
def pytest_runtest_call(item):
    outcome = yield

    try:
        outcome.get_result()
        verify()
    finally:
        teardown()
Esempio n. 8
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
Esempio n. 9
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'
Esempio n. 10
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'
Esempio n. 11
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()
Esempio n. 12
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'
Esempio n. 13
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)
        )
Esempio n. 14
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
Esempio n. 15
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)
        )
Esempio n. 16
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)
        )
Esempio n. 17
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)
        )
Esempio n. 18
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)
        )
Esempio n. 19
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)
        )
Esempio n. 20
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)
        )
Esempio n. 21
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)
        )
Esempio n. 22
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)
        )
Esempio n. 23
0
        def test_with_invalid_args(self, test_class):
            TestClass = ClassDouble(test_class)

            with raises(VerifyingDoubleArgumentError):
                expect_constructor(TestClass).with_args(10)
            teardown()
Esempio n. 24
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'
Esempio n. 25
0
 def afterTest(self, test):
     teardown()
Esempio n. 26
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'
Esempio n. 27
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'
Esempio n. 28
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'
Esempio n. 29
0
def pytest_runtest_call(item, __multicall__):
    try:
        __multicall__.execute()
        verify()
    finally:
        teardown()
Esempio n. 30
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'