How to get data from pytest-xdist nodes

#Simple pytest plugin

Denys Korytkin
3 min readDec 10, 2020

We implemented a simple pytest plugin that can measure memory usage statistics per test through a run and return TOP5 tests which took lots of RAM.

Looks like our plugin works fine 🔥

test results without pytest-xdist

But after some time when our project became huge, need to think about parallelization, and the first thing that comes to mind pytest-xdist, and it will be a good tool to resolve our problem.

But unexpectedly our plugin doesn’t work 😵

pytest -lvv tests -n 3
test results with pytest-xdist

Need to make some hack to force it works. But first, need to understand how pytest-xdist works

#How does pytest-xdist work

pytest-xdist needs for running tests in parallel, when you run pytest -n 4 tests/backend/unit where -n 4 number of nodes which will run for testing

That means, it will run 5 isolated python process:

  • master
  • gw0
  • gw1
  • gw2
  • gw3

The master doesn’t run any tests just speaks with nodes by a small subset of messages like:

- workerready when a node was successfully started

- collectionstart

- collectionfinish

- runtest_protocol_complete completed single test

- etc…

I tried to show a simple diagram of how it works in general where

  • workerinput data send from master to node
  • workeroutput data send from node to master
giagram of pytest-xdist

#Hack pytest-xdist

Then when we already know how it works, we can do something with this and fix our plugin 😉

The main idea here is using workeroutput. The node sends workeroutput to the master, we can add our info to this Dict in pytest_sessionfinish hook which calls per node and master node as well (the last), for understanding where we are, we can check workeroutput attribute in config if it doesn’t exist, we are in the master node.

After that, we can merge all received data from node to single Dict to the master node, this ability is allowed in pytest_testnodedown hook which is called once in the master node when all tests in the node completed.

It will be enough for us, and our plugin works fine again and already supports pytest-xdist🔥

The whole codebase you can see on GitHub

test results with pytest-xdist

--

--