首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >强迫pytest始终运行特定的测试,而不考虑"-k“

强迫pytest始终运行特定的测试,而不考虑"-k“
EN

Stack Overflow用户
提问于 2019-11-28 18:19:24
回答 1查看 169关注 0票数 1

我正在尝试向我的pytest套件添加一个测试,它将验证用于运行测试的python版本,并在版本低于3.8时停止测试。我想让pytest始终运行这个测试,而不管在命令行上进行的任何"-k“过滤。

作为参考,这是实际的测试(对改进的任何评论也将不胜感激):

代码语言:javascript
复制
@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")

我怎样才能做到这一点?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2022-08-27 03:45:17

可能很晚了,我能够使用pytest_configure钩子和pytest_collection_modifyitems钩子实现同样的目标。希望它能帮到别人。

代码语言:javascript
复制
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_tests
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59094496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档