示例#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'
示例#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'
示例#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'
示例#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'
示例#5
0
 def verify_and_teardown_doubles():
     try:
         verify()
     except Exception as e:
         pytest.fail(str(e))
     finally:
         teardown()
示例#6
0
 def verify_and_teardown_doubles():
     try:
         verify()
     except Exception as e:
         pytest.fail(str(e))
     finally:
         teardown()
示例#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'
示例#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
示例#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
示例#10
0
        def test_unsatisfied_expectation(self, test_class):
            TestClass = ClassDouble(test_class)

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

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

    try:
        outcome.get_result()
        verify()
    finally:
        teardown()
示例#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
示例#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'
示例#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
示例#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))
示例#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'
示例#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()
示例#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()
示例#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'
示例#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'
示例#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
示例#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'
示例#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'
示例#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)
        )
示例#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
示例#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))
示例#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))
示例#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))
示例#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))
示例#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))
示例#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)
        )
示例#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)
        )
示例#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)
        )
示例#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))
示例#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)
        )
示例#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)
        )
示例#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)
        )
示例#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)
        )
示例#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)
        )
示例#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'
示例#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'
示例#45
0
文件: nose.py 项目: EasyPost/doubles
 def afterTest(self, test):
     teardown()
示例#46
0
def pytest_runtest_call(item, __multicall__):
    try:
        __multicall__.execute()
        verify()
    finally:
        teardown()
示例#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'
示例#48
0
        def test_with_invalid_args(self, test_class):
            TestClass = ClassDouble(test_class)

            with raises(VerifyingDoubleArgumentError):
                expect_constructor(TestClass).with_args(10)
            teardown()
示例#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'
示例#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'
示例#51
0
文件: nose.py 项目: uber/doubles
 def afterTest(self, test):
     teardown()
示例#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'
示例#53
0
        def test_with_invalid_args(self, test_class):
            TestClass = ClassDouble(test_class)

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