Pytest 单元测试失败,因为目标函数有 cachetools.ttl_cache 装饰器

Amr*_*eja 6 python unit-testing mocking pytest

我有一个函数,我正在为使用 pytest 编写单元测试。唯一的问题是,由于我正在为同一个函数编写多个测试,由于 cachetools.ttl_cache 装饰器,一些测试失败了。这个装饰器使函数每次运行时都返回相同的值,这会扰乱测试。这个装饰器不存在于我正在测试的函数中,而是存在于我正在测试的函数调用的函数中。我无法从我正在测试的函数中删除这个装饰器。这是测试:

@patch('load_balancer.model_helpers.DBSession')
def test_returns_true_if_split_test_is_external(self, dbsession, group_ctx):
    group_ctx.group_id = '{}-{}'.format('2222222222', '123456789')
    split_test = Mock()
    split_test.state = 'external'
    config = {
        'query.return_value.filter.return_value.first.return_value': split_test
    }
    dbsession.configure_mock(**config)
    assert group_ctx.is_in_variation_group('foo') == True
Run Code Online (Sandbox Code Playgroud)

这是要测试的功能:

def is_in_variation_group(self, split_test=None):
    try:
        split_test = get_split_test(split_test) # This function has the 
        #decorator
        log.info('Split test {} is set to {}'.format(split_test.name,
                                                     split_test.state))
        if not split_test or split_test.state == 'off':
            return False

        phone_number = int(self.group_id.split('-')[0])
        if split_test.state == 'internal':
            return True if str(phone_number) in INTERNAL_GUINEA_PIGS else False
        if split_test.state == 'external':
            return True if phone_number % 2 == 0 else False
    except Exception as e:
        log.warning("A {} occurred while evaluating membership into {}'s variation "
                    "group".format(e.__class__.__name__, split_test))
Run Code Online (Sandbox Code Playgroud)

获取拆分测试功能:

    @cachetools.ttl_cache(maxsize=1024, ttl=60)
    def get_split_test(name):
         return (DBSession.query(SplitTest)
                 .filter(SplitTest.name == name)
                 .first())
Run Code Online (Sandbox Code Playgroud)

我怎样才能让它忽略这个缓存装饰器?非常感谢任何帮助

Fra*_*k T 7

我建议在每次测试运行后清除函数的缓存。

cachetools文件没有提到这一点,但是从源代码出现缓存装饰暴露cache_clear功能。

对于您正在测试的示例代码:

import cachetools.func

@cachetools.func.ttl_cache(maxsize=1024, ttl=60)
def get_split_test(name):
     return (DBSession.query(SplitTest)
             .filter(SplitTest.name == name)
             .first())
Run Code Online (Sandbox Code Playgroud)

这将是我的方法(假设 pytest >= 3,否则使用yield_fixture装饰器):

@pytest.fixture(autouse=True)
def clear_cache():
    yield
    get_split_test.cache_clear()

def test_foo():
    pass # Test your function like normal.
Run Code Online (Sandbox Code Playgroud)

clear_cache夹具使用在每次测试后自动使用的 yield 夹具 ( autouse=True) 在每次测试后执行清理。您还可以使用request夹具和request.addfinalizer运行清理功能。

  • 注意 cachetools 版本`3.0.0` 中的方法是`.clear()` 而不是`cache_clear()` (3认同)
  • 感谢您的建议,但当我运行上面的固定装置时,我收到此错误:`AttributeError:'function'对象没有属性'clear_cache'`还有其他方法可以清除缓存吗? (2认同)