【问题标题】:Pytest setup_method executes before xdist worker_id fixturePytest setup_method 在 xdist worker_id 夹具之前执行
【发布时间】:2018-02-28 09:03:31
【问题描述】:

Pytest setup_method 在 xdist worker_id 夹具之前执行 是否可以在setup_method 开始之前获取工作人员 ID?

例如我有:

conftest.py的内容:

@pytest.fixture(autouse=True)
def worker_id(request):
    print("worker")
    if hasattr(request.config, 'slaveinput'):
        request.config.worker = request.config.slaveinput['slaveid']
    else:
        request.config.worker = "master"
    return request.config.worker

test.py的内容:

class TestSomething:

    def setup_method(self):
        print("setup")

    def test_something(self):
        print("test")

输出是:

setup
worker
test

【问题讨论】:

  • 这是 pytest 的已知 bug。你可以使用fixture代替setup方法。

标签: python pytest xdist


【解决方案1】:

你是如何获得 worker_id 的?

假设我们使用 pytest -n 4 启动了 4 个进程 现在启动了四个进程,名为 pw[0]、pw[1]、pw[2] 和 pw[3]。

我会根据 pw[0] 和 pw[1] 的名称将 user1 凭据分配给 process1,将 user2 凭据分配给 process2

是否可以实现

【讨论】:

  • 是 def pytest_configure(config): if hasattr(config, 'slaveinput'): config.worker = config.slaveinput['slaveid'] allocate_user(config.worker) else: config.worker = '大师'take.ms/ARMN2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-15
  • 1970-01-01
相关资源
最近更新 更多