From 7f8ae3944c0ad68c460b3fe9c135de664206f096 Mon Sep 17 00:00:00 2001 From: hellmanj Date: Tue, 23 Feb 2016 17:56:41 -0500 Subject: [PATCH] worker_id fixture as shown in #47 --- README.rst | 10 ++++++++++ testing/acceptance_test.py | 15 +++++++++++++++ xdist/plugin.py | 12 ++++++++++++ 3 files changed, 37 insertions(+) diff --git a/README.rst b/README.rst index 54c72e4..0cbb08b 100644 --- a/README.rst +++ b/README.rst @@ -185,6 +185,16 @@ at once. The specifications strings use the `xspec syntax`_. .. _`execnet`: http://codespeak.net/execnet +Identifying the worker process during a test ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +If you need to determine the identity of a worker process in +a test or fixture, you may use the ``worker_id`` fixture to do so:: + + @pytest.fixture() + def user_account(worker_id): + """ use a different account in each xdist worker """ + return "account_%s" % worker_id + Specifying test exec environments in an ini file +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index c52a6da..036d187 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -620,3 +620,18 @@ class TestNodeFailure: "*Slave*crashed while running*", "*1 failed*2 passed*", ]) + +def test_worker_id_fixture(testdir): + f = testdir.makepyfile(""" + import pytest + @pytest.mark.parametrize("run_num", [1,2]) + def test_worker_id1(worker_id, run_num): + with open("worker_id%s" % run_num, "w") as f: + f.write(worker_id) + """) + result = testdir.runpytest(f, "-n2") + worker_ids = [] + for run_num in [1,2]: + worker_id_file_path = testdir.tmpdir.join("worker_id%s" % run_num).strpath + worker_ids.append(open(worker_id_file_path, "r").read()) + assert "gw0" in worker_ids and "gw1" in worker_ids diff --git a/xdist/plugin.py b/xdist/plugin.py index 69c3a9b..c232a5e 100644 --- a/xdist/plugin.py +++ b/xdist/plugin.py @@ -99,3 +99,15 @@ def pytest_cmdline_main(config): if usepdb: raise pytest.UsageError( "--pdb incompatible with distributing tests.") + +# ------------------------------------------------------------------------- +# fixtures +# ------------------------------------------------------------------------- + + +@pytest.fixture(scope="session") +def worker_id(request): + if hasattr(request.config, 'slaveinput'): + return request.config.slaveinput['slaveid'] + else: + return 'master'