AttributeError: 'Config' object has no attribute 'test_system'

AttributeError: 'Config' object has no attribute 'test_system'

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,但我不知道为什么它会失败。

使用 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 实例,只能共享原始数据类型(字符串、数字、列表、字典等)。这就是为什么只有 build_configuration dict 存储在 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]