首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >配置:“AttributeError”对象没有属性“”test_system“”

配置:“AttributeError”对象没有属性“”test_system“”
EN

Stack Overflow用户
提问于 2019-08-28 04:33:56
回答 1查看 3K关注 0票数 1
代码语言:javascript
复制
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,我不知道它为什么会失败。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-01 03:17:57

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

代码语言:javascript
复制
if hasattr(request.config, 'slaveinput'):
    ...  # we are in worker node
else:
    ...  # we are in master

下面是我尝试将其与您发布的异常回溯绑定在一起的示例:

代码语言:javascript
复制
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字典存储在slaveinput中,每个worker从共享数据重新创建自己的TestSystem对象的原因。

示例测试:

代码语言:javascript
复制
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'

按顺序运行测试会产生以下结果:

代码语言:javascript
复制
$ 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

在分布式模式下运行测试:

代码语言:javascript
复制
$ 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] 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57681891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档