@@ -24,9 +24,13 @@ When ``xdist`` is disabled (running with ``-n0`` for example), then
24
24
Worker processes also have the following environment variables
25
25
defined:
26
26
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.
30
34
31
35
The information about the worker_id in a test is stored in the ``TestReport `` as
32
36
well, under the ``worker_id `` attribute.
@@ -116,7 +120,9 @@ wanted to create a separate database for each test run:
116
120
117
121
Additionally, during a test run, the following environment variable is defined:
118
122
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.
120
126
121
127
Accessing ``sys.argv `` from the controller node in workers
122
128
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -227,47 +233,38 @@ where executing a high-scope fixture exactly once is important.
227
233
Creating one log file for each worker
228
234
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
229
235
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 `
231
237
an option to ``pytest.ini `` for the file base name. Then, in ``conftest.py ``,
232
238
register it with ``pytest_addoption(parser) `` and use ``pytest_configure(config) ``
233
239
to rename it with the worker id.
234
240
235
241
Example:
236
242
237
- .. code-block :: python
243
+ .. code-block :: ini
238
244
239
- # content of pytest.ini
240
245
[pytest]
241
246
log_file_format = %(asctime)s %(name)s %(levelname)s %(message)s
242
247
log_file_level = INFO
243
- worker_log_file = tests_% w .log
248
+ worker_log_file = tests_{worker_id} .log
244
249
245
250
246
251
.. code-block :: python
247
252
248
253
# content of conftest.py
249
254
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.' )
252
256
253
257
254
258
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 :
260
261
log_file = config.getini(' worker_log_file' )
261
262
logging.basicConfig(
262
263
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 ),
264
265
level = config.getini(' log_file_level' )
265
266
)
266
267
267
268
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