Skip to content

Commit 797867f

Browse files
committed
Simplify example and use syntax for env vars
1 parent e3c1a3d commit 797867f

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

docs/how-to.rst

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ When ``xdist`` is disabled (running with ``-n0`` for example), then
2424
Worker processes also have the following environment variables
2525
defined:
2626

27-
* ``PYTEST_XDIST_WORKER``: the name of the worker, e.g., ``"gw2"``.
28-
* ``PYTEST_XDIST_WORKER_COUNT``: the total number of workers in this session,
29-
e.g., ``"4"`` when ``-n 4`` is given in the command-line.
27+
.. envvar:: PYTEST_XDIST_WORKER
28+
29+
The name of the worker, e.g., ``"gw2"``.
30+
31+
.. envvar:: PYTEST_XDIST_WORKER_COUNT
32+
33+
The total number of workers in this session, e.g., ``"4"`` when ``-n 4`` is given in the command-line.
3034

3135
The information about the worker_id in a test is stored in the ``TestReport`` as
3236
well, under the ``worker_id`` attribute.
@@ -116,7 +120,9 @@ wanted to create a separate database for each test run:
116120
117121
Additionally, during a test run, the following environment variable is defined:
118122

119-
* ``PYTEST_XDIST_TESTRUNUID``: the unique id of the test run.
123+
.. envvar:: PYTEST_XDIST_TESTRUNUID
124+
125+
The unique id of the test run.
120126

121127
Accessing ``sys.argv`` from the controller node in workers
122128
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -227,47 +233,38 @@ where executing a high-scope fixture exactly once is important.
227233
Creating one log file for each worker
228234
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
229235

230-
To create one log file for each worker with ``pytest-xdist``, add
236+
To create one log file for each worker with ``pytest-xdist``, you can leverage :envvar:`PYTEST_XDIST_WORKER`
231237
an option to ``pytest.ini`` for the file base name. Then, in ``conftest.py``,
232238
register it with ``pytest_addoption(parser)`` and use ``pytest_configure(config)``
233239
to rename it with the worker id.
234240

235241
Example:
236242

237-
.. code-block:: python
243+
.. code-block:: ini
238244
239-
# content of pytest.ini
240245
[pytest]
241246
log_file_format = %(asctime)s %(name)s %(levelname)s %(message)s
242247
log_file_level = INFO
243-
worker_log_file = tests_%w.log
248+
worker_log_file = tests_{worker_id}.log
244249
245250
246251
.. code-block:: python
247252
248253
# content of conftest.py
249254
def pytest_addoption(parser):
250-
log_help_text = 'Similar to log_file, but %w will be replaced with a worker identifier.'
251-
parser.addini('worker_log_file', help=log_help_text)
255+
parser.addini('worker_log_file', help='Similar to log_file, but %w will be replaced with a worker identifier.')
252256
253257
254258
def pytest_configure(config):
255-
configure_logger(config)
256-
257-
258-
def configure_logger(config):
259-
if xdist_is_enabled():
259+
worker_id = os.environ.get('PYTEST_XDIST_WORKER')
260+
if worker_id is not None:
260261
log_file = config.getini('worker_log_file')
261262
logging.basicConfig(
262263
format=config.getini('log_file_format'),
263-
filename=log_file.replace('%w', os.environ.get('PYTEST_XDIST_WORKER')),
264+
filename=log_file.format(worker_id=worker_id),
264265
level=config.getini('log_file_level')
265266
)
266267
267268
268-
def xdist_is_enabled():
269-
return os.environ.get('PYTEST_XDIST_WORKER') is not None
270-
271-
272-
If running tests with ``-n3``, for example, three files would be created and named
273-
as ``tests_gw0.log``, ``tests_gw1.log`` and ``tests_gw2.log``.
269+
When running the tests with ``-n3``, for example, three files will be created in the current directory:
270+
``tests_gw0.log``, ``tests_gw1.log`` and ``tests_gw2.log``.

0 commit comments

Comments
 (0)