微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。