Storing global configuration data in a pytest/xdist framework

1k Views Asked by At

I'm building a test framework using python + pytest + xdist + selenium grid. This framework needs to talk to a pre-existing custom logging system. As part of this logging process, I need to submit API calls to: set up each new test run, set up test cases within those test runs, and log strings and screenshots to those test cases.

The first step is to set up a new test run, and the API call for that returns (among other things) a Test Run ID. I need to keep this ID available for all test cases to read. I'd like to just stick it in a global variable somewhere, but running my tests with xdist causes the framework to lose track of the value.

I've tried:

  • Using a "globals" class; it forgot the value when using xdist.
  • Keeping a global variable inside my conftest.py file; same problem, the value gets dropped when using xdist. Also it seems wrong to import my conftest everywhere.
  • Putting a "globals" class inside the conftest; same thing.

At this point, I'm considering writing it to a temp file, but that seems primitive, and I think I'm overlooking a better solution. What's the most correct, pytest-style way to store and access global data across multiple xdist threads?

2

There are 2 best solutions below

0
On

Might be worth looking into Proboscis, as it allows specific test dependencies and could be a possible solution.

0
On

Can you try config.cache E.g. -

request.config.cache.set('run_id', run_id)

refer documention