pytest-dev / pytest-dev/pytest-xdist

Allow fixtures to execute only once in the test run (not for each worker)

Open
#779 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.9k
Forks
287
Avg merge
9h 30m
Merged PRs (30d)
2

Description

Use case

There is a resource that can be used in many testcases in parallel. The idea is to create a fixture that will acquire this resource before all the test cases and release it after all the test cases. In between all the test cases can use this resource, even if they run in parallel.

Alternatives
  1. Using locks - Complicated and adds overhead to the tests
  2. Checking in a fixture if the code runs on the worker thread - I found two problems with this approach. The first one is that method needs to check something very "magic"-like that can maybe change, like the "master" worker_id. The second one is that it can lead to races.
Suggested solution

Mark a fixture as "run only once"

Thank you in advance :)

Contributor guide

No contributing guide indexed for this repository

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 by tracing how pytest-xdist creates workers and executes fixtures, then inspect the fixture lifecycle and worker communication paths. Define how a fixture marked to run once would be acquired and released across the full test run, including parallel workers, and identify tests needed to verify both single execution and resource cleanup.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
distributed-systems, testing-qa
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.