我有以下模块,它充当执行 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", dest="exclude_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 时附加输出
如何让 pytest-xdist 在并行执行测试时运行自定义插件?