pytest-dev / pytest-dev/pytest

Generalize `PYTEST_CURRENT_TEST` for multiple threads

Open
#13,844 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: performance type: proposal
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:

  1. Accept this as a possibility and don't worry about it
  2. Treat the threading.current_thread() when pytest.main is called as "this pytest session's main thread", even if it's not truly the main thread
  3. Forgo PYTEST_CURRENT_TEST entirely under threading, and just use PYTEST_CURRENT_TEST_THREAD_{n}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.