diff --git a/CHANGELOG b/CHANGELOG index aa7a5a4..1e212b6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,9 @@ -1.14.1.dev +1.15.0.dev ---------- +- new ``worker_id`` fixture, returns the id of the worker in a test or fixture. + Thanks Jared Hellman for the PR. + 1.14 ---- diff --git a/README.rst b/README.rst index 0cbb08b..04452cb 100644 --- a/README.rst +++ b/README.rst @@ -195,6 +195,9 @@ a test or fixture, you may use the ``worker_id`` fixture to do so:: """ use a different account in each xdist worker """ return "account_%s" % worker_id +When ``xdist`` is disabled (running with ``-n0`` for example), then +``worker_id`` will return ``"master"``. + Specifying test exec environments in an ini file +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index f5ad367..9ba158b 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -622,18 +622,23 @@ class TestNodeFailure: ]) -def test_worker_id_fixture(testdir): +@pytest.mark.parametrize('n', [0, 2]) +def test_worker_id_fixture(testdir, n): + import glob f = testdir.makepyfile(""" import pytest - @pytest.mark.parametrize("run_num", [1,2]) + @pytest.mark.parametrize("run_num", range(2)) def test_worker_id1(worker_id, run_num): - with open("worker_id%s" % run_num, "w") as f: + with open("worker_id%s.txt" % run_num, "w") as f: f.write(worker_id) """) - result = testdir.runpytest(f, "-n2") + result = testdir.runpytest(f, "-n%d" % n) result.stdout.fnmatch_lines('* 2 passed in *') - worker_ids = [] - for run_num in [1, 2]: - worker_id_file_path = testdir.tmpdir.join("worker_id%s" % run_num) - worker_ids.append(open(str(worker_id_file_path), "r").read()) - assert "gw0" in worker_ids and "gw1" in worker_ids + worker_ids = set() + for fname in glob.glob(str(testdir.tmpdir.join("*.txt"))): + with open(fname) as f: + worker_ids.add(f.read().strip()) + if n == 0: + assert worker_ids == set(['master']) + else: + assert worker_ids == set(['gw0', 'gw1'])