我正在尝试向我的pytest套件添加一个测试,它将验证用于运行测试的python版本,并在版本低于3.8时停止测试。我想让pytest始终运行这个测试,而不管在命令行上进行的任何"-k“过滤。
作为参考,这是实际的测试(对改进的任何评论也将不胜感激):
@pytest.mark.tryfirst
def test_python_version():
version = sys.version_info
if version.major < 3:
logging.error("python2 is not supported")
pytest.exit("Stopped testing")
elif version.minor < 8:
logging.warning("Use python 3.8 or higher for best results")我怎样才能做到这一点?
谢谢!
发布于 2022-08-27 03:45:17
可能很晚了,我能够使用pytest_configure钩子和pytest_collection_modifyitems钩子实现同样的目标。希望它能帮到别人。
def pytest_configure(config):
keyword = config.option.keyword
file_or_dir = config.option.file_or_dir
markers = config.option.markexpr
if len(file_or_dir) > 0:
config.option.file_or_dir.append('unittesting/test_authentication.py::test_authenticate')
if keyword != '':
config.option.keyword = 'test_authenticate or '+ keyword
if markers != '':
config.option.markexpr = 'authenticate_must or '+ markers
def pytest_collection_modifyitems(config, items):
auth_tests = [list(filter(lambda x:x.name == 'test_authenticate', items))[0]]
logout_tests = list(filter(lambda x:x.name == 'test_logout', items))
other_tests = list(filter(lambda x:x.name not in ('test_authenticate','test_logout'), items))
items[:] = auth_tests + other_tests + logout_testshttps://stackoverflow.com/questions/59094496
复制相似问题