【问题标题】:How to disable pytest xdist only when pytest is called with filters?如何仅在使用过滤器调用 pytest 时禁用 pytest xdist?
【发布时间】:2021-02-28 09:25:24
【问题描述】:

当我运行所有 pytests 时,我确实希望充分受益于将负载分散到所有 m 个核心 (xdist) 上,但是当我运行其中的一个子集时,几乎总是出于开发/调试目的,我确实想避免这种情况xdist 的缺点如下:

  • 开始时间变慢
  • 无法使用 pdb 调试

例如:调用pytest -k foo 应该有效地运行pytest -n0 -k foo。请记住,不带参数调用 pytest 不应影响 -n 值。

【问题讨论】:

    标签: pytest pytest-xdist


    【解决方案1】:

    您可以dynamically add the option,例如:

    conftest.py

    import sys
    
    def pytest_cmdline_preparse(args):
        if "xdist" in sys.modules and "-k" in args:
            for i, arg in enumerate(args):
                # remove -n # option
                if arg == "-n":
                    del args[i]
                    del args[i]
                    break
                # remove -n# option
                if re.match(r"-n\d+", arg):
                    del args[i]
                    break
    
            args[:] = ["-n0"] + args
    

    这将删除现有的“-n”选项(-n #-n# 的形式 - 您可能不需要两者,具体取决于命令行的外观),并添加“-n0”选项。

    更新
    pytest_cmdline_preparse 在当前 (6.x) pytest 版本中已弃用,pytest_load_initial_conftests 可以以相同的方式使用。

    【讨论】:

    • 非常感谢!!!我确实工作了。它可能看起来很奇怪,但我认为这是 pytest 的可靠方法,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2022-10-13
    • 2016-11-21
    • 2022-11-05
    • 1970-01-01
    • 2023-02-18
    • 2018-07-04
    相关资源
    最近更新 更多