在 pytest 运行 后删除缓存文件
Removing cached files after a pytest run
我使用 joblib.Memory
来缓存昂贵的计算,当 运行ning 测试 py.test
时。我使用的代码简化为以下内容,
from joblib import Memory
memory = Memory(cachedir='/tmp/')
@memory.cache
def expensive_function(x):
return x**2 # some computationally expensive operation here
def test_other_function():
input_ds = expensive_function(x=10)
## run some tests with input_ds
效果很好。我知道使用 tmpdir_factory
fixture 可能会更优雅地完成此操作,但这不是重点。
我遇到的问题是如何在所有测试完成后清理缓存文件运行,
- 是否可以在所有测试中共享一个全局变量(其中包含例如缓存对象的路径列表)?
- py.test 中是否有一种机制可以在所有测试 运行(无论成功与否)后调用某些命令?
is it possible to share a global variable among all tests (which would contains e.g. a list of path to the cached objects) ?
我不会走那条路。最好避免全局可变状态,尤其是在测试中。
is there a mechanism in py.test to call some command once all the tests are run (whether they succeed or not)?
是的,将一个自动使用的会话范围的固定装置添加到您的项目级 conftest.py
文件中:
# conftest.py
import pytest
@pytest.yield_fixture(autouse=True, scope='session')
def test_suite_cleanup_thing():
# setup
yield
# teardown - put your command here
yield 之后的代码将 运行 - 一次 - 在测试套件结束时,无论通过还是失败。
is it possible to share a global variable among all tests (which would
contains e.g. a list of path to the cached objects) ?
实际上有几种方法可以做到这一点,每种方法各有利弊。我认为这个 SO 答案很好地总结了它们 - - 但例如:
def pytest_namespace():
return {'my_global_variable': 0}
def test_namespace(self):
assert pytest.my_global_variable == 0
is there a mechanism in py.test to call some command once all the tests are run (whether they succeed or not)?
是,py.test 有 teardown 个可用功能:
def setup_module(module):
""" setup any state specific to the execution of the given module."""
def teardown_module(module):
""" teardown any state that was previously setup with a setup_module
method.
"""
我使用 joblib.Memory
来缓存昂贵的计算,当 运行ning 测试 py.test
时。我使用的代码简化为以下内容,
from joblib import Memory
memory = Memory(cachedir='/tmp/')
@memory.cache
def expensive_function(x):
return x**2 # some computationally expensive operation here
def test_other_function():
input_ds = expensive_function(x=10)
## run some tests with input_ds
效果很好。我知道使用 tmpdir_factory
fixture 可能会更优雅地完成此操作,但这不是重点。
我遇到的问题是如何在所有测试完成后清理缓存文件运行,
- 是否可以在所有测试中共享一个全局变量(其中包含例如缓存对象的路径列表)?
- py.test 中是否有一种机制可以在所有测试 运行(无论成功与否)后调用某些命令?
is it possible to share a global variable among all tests (which would contains e.g. a list of path to the cached objects) ?
我不会走那条路。最好避免全局可变状态,尤其是在测试中。
is there a mechanism in py.test to call some command once all the tests are run (whether they succeed or not)?
是的,将一个自动使用的会话范围的固定装置添加到您的项目级 conftest.py
文件中:
# conftest.py
import pytest
@pytest.yield_fixture(autouse=True, scope='session')
def test_suite_cleanup_thing():
# setup
yield
# teardown - put your command here
yield 之后的代码将 运行 - 一次 - 在测试套件结束时,无论通过还是失败。
is it possible to share a global variable among all tests (which would contains e.g. a list of path to the cached objects) ?
实际上有几种方法可以做到这一点,每种方法各有利弊。我认为这个 SO 答案很好地总结了它们 - - 但例如:
def pytest_namespace():
return {'my_global_variable': 0}
def test_namespace(self):
assert pytest.my_global_variable == 0
is there a mechanism in py.test to call some command once all the tests are run (whether they succeed or not)?
是,py.test 有 teardown 个可用功能:
def setup_module(module):
""" setup any state specific to the execution of the given module."""
def teardown_module(module):
""" teardown any state that was previously setup with a setup_module
method.
"""