1
rootdir: python, inifile: pytest.ini, testpaths: test_cases
plugins: metadata-1.8.0, html-1.22.0, sugar-0.9.2, timeout-1.3.3, forked-1.0.2, xdist-1.29.0, repeat-0.8.0
timeout: 3600.0s
timeout method: signal
timeout func_only: False
gw0 ok / gw1 ok / gw2 ok
../python-venv/lib/python2.7/site-packages/pluggy/hooks.py:289: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
../python-venv/lib/python2.7/site-packages/pluggy/manager.py:87: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
../python-venv/lib/python2.7/site-packages/pluggy/manager.py:81: in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
../python-venv/lib/python2.7/site-packages/_pytest/python.py:234: in pytest_pycollect_makeitem
    res = list(collector._genfunctions(name, obj))
../python-venv/lib/python2.7/site-packages/_pytest/python.py:410: in _genfunctions
    self.ihook.pytest_generate_tests(metafunc=metafunc)
../python-venv/lib/python2.7/site-packages/pluggy/hooks.py:289: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
../python-venv/lib/python2.7/site-packages/pluggy/manager.py:87: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
../python-venv/lib/python2.7/site-packages/pluggy/manager.py:81: in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
../../modules/test/python/test_cases/uts_plugin.py:20: in pytest_generate_tests
    for build_config in metafunc.config.test_system.build_configuration.get(
E   AttributeError: 'Config' object has no attribute 'test_system'
gw0 [1] / gw1 ok / gw2 ok

在不使用“-n”选项的情况下,所有测试都运行良好。我打印了 config 的属性,它确实有 test_system ,我不知道它为什么会失败。

4

1 回答 1

1

在分发测试时,在主节点和工作节点之间共享数据的常用方法xdist是实现pytest_configure_node挂钩,然后以下列方式访问数据:

if hasattr(request.config, 'slaveinput'):
    ...  # we are in worker node
else:
    ...  # we are in master

这是我尝试与您发布的异常回溯相关的示例:

class TestSystem:
    def __init__(self):
        self.build_configuration = dict()


def pytest_configure(config):
    if hasattr(config, 'slaveinput'):  # slave
        return
    # we are on master node
    # create test system object, attach to config
    s = TestSystem()
    s.build_configuration['foo'] = 'bar'
    config.test_system = s


def pytest_configure_node(node):
    # this is the pendant of pytest_configure hook, but for worker nodes only
    # store serializable stuff from test system object in slaveinput
    node.slaveinput['test_system_serialized'] = node.config.test_system.build_configuration


def pytest_generate_tests(metafunc):
    if hasattr(metafunc.config, 'slaveinput'):  # we are in worker node
        # restore test system object using serialized data
        s = TestSystem()
        s.build_configuration = metafunc.config.slaveinput['test_system_serialized']
    else:  # master
        # simply get test system instance from config
        s = metafunc.config.test_system

    # generate tests    
    if 'test_system' in metafunc.fixturenames:
        metafunc.parametrize('test_system', [s])

请注意,您不能TestSystem在 master 和 worker 之间共享实例,只能共享原始数据类型(字符串、数字、列表、dicts 等)。这就是为什么只build_configuration存储字典并且每个工作人员从共享数据中slaveinput重新创建自己的对象的原因。TestSystem

示例测试:

import time
import pytest


@pytest.mark.parametrize('n', range(1, 5))
def test_eggs(n, test_system):
    time.sleep(1)
    assert test_system.build_configuration['foo'] == 'bar'

连续运行测试会产生:

$ pytest -v
...
test_spam.py::test_eggs[test_system0-1] PASSED
test_spam.py::test_eggs[test_system0-2] PASSED
test_spam.py::test_eggs[test_system0-3] PASSED
test_spam.py::test_eggs[test_system0-4] PASSED

以分布式模式运行测试:

$ pytest -v -n4
...
scheduling tests via LoadScheduling

test_spam.py::test_eggs[test_system0-1] 
test_spam.py::test_eggs[test_system0-2] 
test_spam.py::test_eggs[test_system0-4] 
test_spam.py::test_eggs[test_system0-3] 
[gw1] [ 25%] PASSED test_spam.py::test_eggs[test_system0-2] 
[gw2] [ 50%] PASSED test_spam.py::test_eggs[test_system0-3] 
[gw0] [ 75%] PASSED test_spam.py::test_eggs[test_system0-1] 
[gw3] [100%] PASSED test_spam.py::test_eggs[test_system0-4] 
于 2019-09-30T19:17:56.947 回答