Pytest-xdist 无法识别作为脚本执行的自定义 pytest 插件

问题描述

我有以下模块作为脚本来执行 pytest。

class CustomPlugin:

    def pytest_configure(self,config):

        config.addinivalue_line(
            "python_files","*.py"
        )
        config.addinivalue_line(
            "markers","ignore_stdout: mark test to ignore stdout as Failed task"
        )
        config.addinivalue_line(
            "markers","ignore_stderr: mark test to ignore stderr as Failed task"
        )
        config.addinivalue_line(
            "markers","big: mark test as big and only running it certain times"
        )

    def is_setup_py(self,path):
        if path.basename != "setup.py":
            return False
        contents = path.read_binary()
        return b"setuptools" in contents or b"distutils" in contents
        
    def pytest_addoption(self,parser):
        parser.addoption("--big",dest="exclude_big",default=True,action='store_false',help='run big test')
        parser.addoption("--no-big",action='store_true',help='miss big test [ default ]' )

    def pytest_collection_modifyitems(self,config,items):
        exclude_big = config.getoption("exclude_big")
        deselect_tests,selected = [],[]
        for item in items:
            if exclude_big and "big" in item.keywords or item.name == "test_ignore_this":
                deselect_tests.append(item)
            else:
                selected.append(item)
        items[:] = selected
        config.hook.pytest_deselected(items=deselect_tests)

    @pytest.hookimpl(hookwrapper=True)
    def pytest_runtest_makereport(self,item,call):
        report = (yield).get_result()

        markers = [mark.name for mark in item.own_markers if mark.name in ['xfail','ignore_stdout','ignore_stderr']]
        
        if report.when == 'call' \
            and len(item._report_sections)>0 \
                and len(item._report_sections[0])==3 \
                    and 'ignore_stdout' not in markers \
                        and 'ignore_stderr' not in markers:
            if len(markers) > 0:
                report.outcome = "skipped"
            else:    
                report.outcome = "Failed"


if __name__=='__main__':
    pytest.main(sys.argv[1:],plugins=[CustomPlugin()])

当我在没有 pytest-xdist 的情况下正常运行脚本时,该脚本按预期工作。

但是当我使用 pytest-xdist 时,它无法识别基于自定义插件标记和 collection_modifications。

在正常运行和使用 xdist 时附加输出

Output while executing normally

Output while executing using xdist

如何让 pytest-xdist 在并行执行测试时运行自定义插件

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)