-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Description
Part of #13768. See also discussion in #13837.
The PYTEST_CURRENT_TEST envvar stores the node id and phase of the current test. When multiple threads are running, we'll need to generalize this, so that each thread shows the current test being run.
Based on @RonnyPfannschmidt's comments in #13837, one option is that pytest makes it a requirement that whoever spawns the threads* sets each thread name to a scheme recognized by pytest, like pytest-thread-{n}. Pytest would check threading.current_thread().name in runtestprotocol, and then if it matches pytest-thread-{n}, use n to set the PYTEST_CURRENT_TEST_THREAD_{n} envvar.
* this responsibility might fall to pytest, or to the user, depending on TBD details about how a user invokes pytest multithreading. Eg, it might be invoked with a high-level + pytest-managed --threads arg, or a user-managed override of pytest_runtestloop and spawning a thread pool, or an alternative mechanism.
Main thread considerations
@RonnyPfannschmidt mentions that PYTEST_CURRENT_TEST should be reserved for the main thread. If we check this with threading.main_thread(), then we'll mistake cases where pytest is run inside of a thread:
import threading
import pytest
thread = threading.Thread(target=pytest.main)
thread.start()We could:
- Accept this as a possibility and don't worry about it
- Treat the
threading.current_thread()whenpytest.mainis called as "this pytest session's main thread", even if it's not truly the main thread - Forgo
PYTEST_CURRENT_TESTentirely under threading, and just usePYTEST_CURRENT_TEST_THREAD_{n}