python - 如何使用 pytest-xdist 访问共享资源?

标签 python testing pytest pytest-xdist

我想访问一个包含所有帐户凭据的列表,以将它们提供给 pytest-xdist 中的每个单独线程。我该如何实现?据我所知,pytest-dist,对于每个启动的线程,它是一个单独的测试 session ,它们在内存中分别初始化每个 python 模块。我有一个代码片段的例子如下

import sys
import pytest
import threading
import time

lock = threading.Lock()

i = 0
lis = []
lis.append(1)
lis.append(2)


class TestAAA(object):

    def test_function1(self, fixture_function_feature1):
        global i, lock
        with lock:
            print "Lock Acquired"
            time.sleep(2)
            print >> sys.stderr, 'test_: '+str(lis[i])

执行的命令:

pytest -sv -n 2 --count=5 

输出:

test_: 1
test_: 1

预期输出:

test_: 1
test_: 2

I also tried using a shared resource mentioned at https://github.com/pytest-dev/pytest/issues/1402#issuecomment-186299177

 import pytest

    def pytest_configure(config):        
        if is_master(config):
            config.shared_directory = tempdir.mktemp()

    def pytest_unconfigure(config):        
        if is_master(config):
            shutil.rmtree(config.shared_directory)


    def pytest_configure_node(self, node):
        """xdist hook"""
        node.slaveinput['shared_dir'] = node.config.shared_directory


    @pytest.fixture
    def shared_directory(request):
        if is_master(request.config):
            return request.config.shared_directory
        else:
            return request.config.slaveinput['shared_dir']


    def is_master(config):
        """True if the code running the given pytest.config object is running in a xdist master
        node or not running xdist at all.
        """
        return not hasattr(config, 'slaveinput')

    def test_shared_dir(shared_directory):
        print >> sys.stderr, 'master logs dir: '+str(shared_directory) 

使用和不使用 pytest-xdist 运行它们,都给出错误

命令:pytest -sv test_parallel_share.py

输出:

―――――――――――――――――――――――――――――――――――――――――――――― ERROR at setup of test_shared_dir ―――――――――――――――――――――――――――――――――――――――――――――――

request = <SubRequest 'shared_directory' for <Function 'test_shared_dir'>>

    @pytest.fixture
    def shared_directory(request):
        if is_master(request.config):
>           return request.config.shared_directory
E           AttributeError: 'Config' object has no attribute 'shared_directory'

test_parallel_share.py:21: AttributeError
                                                                                                                 100% ██████████

Results (0.05s):
       1 error

命令:pytest -n 2 -sv test_parallel_share.py

输出:

scheduling tests via LoadScheduling


―――――――――――――――――――――――――――――――――――――――――――――― ERROR at setup of test_shared_dir ―――――――――――――――――――――――――――――――――――――――――――――――

request = <SubRequest 'shared_directory' for <Function 'test_shared_dir'>>

    @pytest.fixture
    def shared_directory(request):
        if is_master(request.config):
            return request.config.shared_directory
        else:
>           return request.config.slaveinput['shared_dir']
E           KeyError: 'shared_dir'

test_parallel_share.py:23: KeyError
                                                                                                                 100% ██████████

Results (0.55s):
       1 error

最佳答案

我认为有多种方法可以解决您的问题。 config.option 内容在 master 和 workers 之间共享。一个特殊的钩子(Hook)可以自定义运行时信息...

在 fixtures 中测试 is_master 是没有意义的,接受它的值(value)是为了 xdist 不存在的情况(没有 -n)。当 xdist 运行时,fixtures 不在 master 中运行,只有 hooks。固定装置仅在包裹在 pytest_runteSTLoop 中的正常协议(protocol)内的子进程内执行。

您取消引用的 slave_input 中的自定义键需要在准备从机的钩子(Hook)中从主机设置。查看 pytest-xdist newhooks.py你会在 node.workerinput 中找到类似 home 的东西来设置那个键。看着 pytest_configure_node(self, node: WorkerController)。您应该编写其中的一个并在那里设置 node.workerinput['shared_dir'],从站将找到您要分发的公共(public)数据。

def pytest_configure_node(self, node: WorkerController):
    node.workerinput['shared_dir'] = node.config.option.shared_dir

关于python - 如何使用 pytest-xdist 访问共享资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51485371/

相关文章:

javascript - 如何在 qUnit 中为单个测试添加临时 css?

java - 如何在运行时定义 JUnit 测试超时(即没有注释)?

python - 在 python 模块的 py.test 测试目录中,导入该模块的文件

python - 是否可以构建一个适用于 Python 2 和 3 的shebang?

python - 将 svg 文件导入 matplotlib 图

python - 读取相当大的 JSON 文件

python - pytest -> 如何在类下的测试方法中使用 fixture 返回值

python - Flask 导入错误 "cannot import name ' Flask'”

testing - 你如何为 Jmeter 的代理服务器设置代理?

python - 是否可以使用 'owner' 标记测试并在测试失败时记录该所有者?