【发布时间】:2014-12-15 06:27:08
【问题描述】:
我有一个包裹:
- package/
- __init__.py
- cache.py
- module1.py
- module2.py
- tests/
- test_module1.py
- test_module2.py
- conftest.py
module1 和 module2 都从 cache.py 导入:
from package.cache import cache
@cache()
def foo():
...
默认情况下,cache 使用dogpile.cache 提供的基于文件的缓存,但是在运行测试时,我想使用dogpile.cache 也支持的基于内存的缓存来模拟cache。
这就是我想要的:
# conftest.py
import pytest
@pytest.fixture(autouse=True)
def patch_cache(monkeypatch):
from dogpile.cache import make_region
m_cache = make_region().configure('dogpile.cache.memory')
monkeypatch.setattr('package.cache.cache', m_cache)
如您所见,我创建了一个夹具,其中使用 monkeypatch 将 cache 替换为 m_cache
但是,这不起作用,当我使用py.test 运行测试时,他们仍在使用基于旧文件的缓存。有什么问题吗?
【问题讨论】:
标签: python unit-testing testing pytest