pytest-dev / pytest-dev/pytest
Generalize `PYTEST_CURRENT_TEST` for multiple threads
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
Part of https://github.com/pytest-dev/pytest/issues/13768. See also discussion in https://github.com/pytest-dev/pytest/pull/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 https://github.com/pytest-dev/pytest/pull/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}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with runtestprotocol and the existing PYTEST_CURRENT_TEST behavior described in the issue. Read issue 13768 and the discussion in pull request 13837 before deciding how thread names, environment variables, and pytest sessions started inside threads should interact; the work is done when those semantics are resolved and consistently supported.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100