gpt4 book ai didi

python - 在使用 pytest 运行测试之前执行健全性检查

转载 作者:太空宇宙 更新时间:2023-11-04 09:34:09 24 4
gpt4 key购买 nike

我想在使用 pytest 运行测试时执行一些健全性检查。通常,我想检查测试是否可以访问某些可执行文件,以及用户在命令行上提供的选项是否有效。

我发现最接近的是使用固定装置,例如:

@pytest.fixture(scope="session", autouse=True)
def sanity_check(request):
if not good:
sys.exit(0)

但这仍然会运行所有测试。我希望脚本在尝试运行测试之前失败。

最佳答案

您不需要显式地验证命令行选项;这将由 arg 解析器完成,如有必要,它会提前中止执行。至于条件检查,您离解决方案不远了。使用

示例 fixture :

@pytest.fixture(scope='session', autouse=True)
def precondition():
if not shutil.which('spam'):
# immediate shutdown
pytest.exit('Install spam before running this test suite.')
# or skip each test
# pytest.skip('Install spam before running this test suite.')
# or make it an expected failure
# pytest.xfail('Install spam before running this test suite.')

xdist 兼容性

在使用xdist 的测试运行中调用pytest.exit() 只会使当前工作进程崩溃,而不会中止主进程。您必须将检查移动到在 runteSTLoop 开始之前调用的 Hook (所以 pytest_runteSTLoop Hook 之前的任何东西),例如:

# conftest.py
def pytest_sessionstart(session):
if not shutil.which('spam'):
# immediate shutdown
pytest.exit('Install spam before running this test suite.')

关于python - 在使用 pytest 运行测试之前执行健全性检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54463143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com