From 34f63600ee7a75aff0343eda1379b86950a66007 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 10 Mar 2022 11:51:43 -0300 Subject: [PATCH] Simplify the example about creating one log per worker No need to use a custom option as this complicates the example a bit and might distract from the main details. --- docs/how-to.rst | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/docs/how-to.rst b/docs/how-to.rst index 268a969..c357ae5 100644 --- a/docs/how-to.rst +++ b/docs/how-to.rst @@ -211,37 +211,20 @@ Creating one log file for each worker ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ To create one log file for each worker with ``pytest-xdist``, you can leverage :envvar:`PYTEST_XDIST_WORKER` -an option to ``pytest.ini`` for the file base name. Then, in ``conftest.py``, -register it with ``pytest_addoption(parser)`` and use ``pytest_configure(config)`` -to rename it with the worker id. +to generate a unique filename for each worker. Example: -.. code-block:: ini - - [pytest] - log_file_format = %(asctime)s %(name)s %(levelname)s %(message)s - log_file_level = INFO - worker_log_file = tests_{worker_id}.log - - .. code-block:: python # content of conftest.py - def pytest_addoption(parser): - parser.addini( - "worker_log_file", - help="Similar to log_file, but %w will be replaced with a worker identifier.", - ) - - def pytest_configure(config): worker_id = os.environ.get("PYTEST_XDIST_WORKER") if worker_id is not None: log_file = config.getini("worker_log_file") logging.basicConfig( format=config.getini("log_file_format"), - filename=log_file.format(worker_id=worker_id), + filename=f"test_{worker_id}.log", level=config.getini("log_file_level"), )