0

我有这样的包装纸

def external_services_mock(f):
    @patch('service.remote_call1')
    @patch('service.remote_call2')
    def wrapper(self, remote_call2_mock, remote_call1_mock, *args, **kwargs):
        remote_call1_mock.return_value = None

        def test_mocks():
            return remote_call1_mock, remote_call2_mock

        f(self, test_mocks, *args, **kwargs)

    return wrapper

并测试:

@external_services_mock
def test_add_callback(self, test_mocks):
    remote_call1_mock, remote_call2_mock = test_mocks()

    // do smth

    // assert smth

django_nose runner 忽略包装的测试,并正常运行

Django 版本 2.0.2 django_nose 1.4.5

有任何想法吗?

4

1 回答 1

0

使用functools.wraps来保留包装测试的名称似乎可以解决。

from functools import wraps

def external_services_mock(f):
    @patch('service.remote_call1')
    @patch('service.remote_call2')
    @wraps(f)
    def wrapper(self, remote_call2_mock, remote_call1_mock, *args, **kwargs):
        remote_call1_mock.return_value = None

        def test_mocks():
            return remote_call1_mock, remote_call2_mock

        f(self, test_mocks, *args, **kwargs)

    return wrapper
于 2018-03-29T13:01:00.900 回答