Python 如何使用pytest xdist访问共享资源?
我想访问一个包含所有帐户凭据的列表,以便将它们提供给pytest xdist中的每个单独线程。我如何做到这一点?据我所知,pytest dist,对于每个启动的线程,它是一个单独的测试会话,它们在内存中分别初始化每个python模块。我有一个代码片段示例,如下所示Python 如何使用pytest xdist访问共享资源?,python,testing,pytest,pytest-xdist,Python,Testing,Pytest,Pytest Xdist,我想访问一个包含所有帐户凭据的列表,以便将它们提供给pytest xdist中的每个单独线程。我如何做到这一点?据我所知,pytest dist,对于每个启动的线程,它是一个单独的测试会话,它们在内存中分别初始化每个python模块。我有一个代码片段示例,如下所示 import sys import pytest import threading import time lock = threading.Lock() i = 0 lis = [] lis.append(1) lis.appe
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
―――――――――――――――――――――――――――――――――――――――――――――― 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
预期产出:
test_: 1
test_: 2
使用pytest xdist和不使用pytest xdist运行它们都会产生错误
命令:pytest-sv test\u parallel\u share.py
输出:
test_: 1
test_: 1
―――――――――――――――――――――――――――――――――――――――――――――― 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
现在,在我看来,这似乎是最重要的,但也许我错了-凭证列表的目的是什么?您只是想用不同的凭据并行运行相同的测试吗?是的,这是正确的。同一个测试具有不同的凭据,因此它可以登录到不同的帐户。为什么不直接参数化这些测试呢?这些测试是参数化的,日志需要由所有这些测试都写入一个公共目录,其中包含一个带有时间戳的名称。