2017-08-02 62 views
1

我有一个函数,我正在使用pytest编写单元测试。唯一的问题是,因为我正在为同一个函数编写多个测试,所以有几个测试因cachetools.ttl_cache修饰器而失败。这个装饰器使得函数在每次运行时都会返回相同的值,从而混淆了测试。这个装饰器不存在于我测试的函数中,而是存在于我正在测试的函数中调用的函数中。我无法从我测试的函数中删除这个装饰器。下面是测试: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 

,这里是要测试的功能:

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)) 

获取分割测试功能:

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

我怎样才能使它所以这个缓存装饰器忽略?任何帮助都很赞赏

+0

我想你需要在运行测试之前修补'cachetools'并用一个不缓存任何东西的函数替换'ttl_cache'。 – phd

回答

1

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

cachetools documentation没有提到这个,但是从the source code看来缓存修饰器揭示了一个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()) 

这将是我的方法(假定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. 

clear_cache灯具使用了产率每次测试后自动使用夹具(autouse=True)在每次测试后执行清理。您也可以使用the request fixture and request.addfinalizer来运行清理功能。

+0

感谢您的建议,但是当我运行上述设备时出现此错误:'AttributeError:'function'object has no attribute'clear_cache''有没有其他方法可以清除缓存? –

+0

哎呀,我倒过来了,它是'cache_clear()'。我会更新我的答案。 顺便说一句,除非你正在运行不同版本的cachetools,你的例子应该显示'cachetools.func.ttl_cache'作为装饰器。我测试了cachetools版本2.0.0 –

+0

你击中了头部。非常感谢 –