pytest-xdist is a plugin for distributed testing.
A somewhat common need is how one can make a plugin share data between
Here's a simple recipe that provides a
shared_directory fixture which
points to a temporary directory accessible by both
import shutil import tempdir def pytest_configure(config): if is_master(config): config.shared_directory = tempdir.mktemp() def pytest_unconfigure(config): if is_master(config): shutil.rmtree(config.shared_directory) def pytest_configure_node(node): """xdist hook""" node.slaveinput['shared_dir'] = node.config.shared_directory def is_master(config): """True if the code running the given pytest.config object is running in a xdist master node or not running xdist at all. """ return not hasattr(config, 'slaveinput') @pytest.fixture def shared_directory(request): """Returns a unique and temporary directory which can be shared by master or worker nodes in xdist runs. """ if is_master(request.config): return request.config.shared_directory else: return request.config.slaveinput['shared_dir']
Anything put in
node.slaveinput dictionary during the
xdist hook can be accessed by the worker nodes later. You can put any
simple builtin type, like lists, strings, tuples, etc.
shared_directory fixture can then be used by a plugin or even tests
to have a common directory where information can be exchanged. This
recipe also shows how one can find out if the code is running in the master
or a worker node.
(this recipe appeared originally in this pytest issue)