2

I want to write a pytest session finish hook that is aware if the xdist plugin has been activated (for example if the -n option has been passed). Is there an official / stable way to do this ? I found a way to identify if this is a xdist worker, but not to disambiguate between the xdist master and the 'main' pytest process when xdist is deactivated.

def pytest_sessionfinish(session):
    try:
        pytest_worker_id = session.config.slaveinput['slaveid']
    except AttributeError:
        # TODO disambiguate: are we the xdist master node or the "main" pytest node with xdist deactivated?
        ...

Note: this is to get rid of the try/except in this conftest.py example

Thanks in advance ! Kind regards

smarie
  • 4,568
  • 24
  • 39
  • 1
    IIRC the master node is the main process, so you can't distinguish between them. As for the `xdist` "activation" - there's no such state for a plugin. If the plugin is installed, it will be registered and all its hooks will be executed. You can check whether `xdist` is installed though - e.g. via `config.pluginmanager.getplugin("xdist") is not None`. – hoefling Feb 13 '20 at 17:22
  • Thanks ! I was afraid of this answer :) but if that's the only way, fair enough. – smarie Feb 13 '20 at 17:25
  • 1
    pytest-xdist sets PYTEST_XDIST_WORKER_COUNT and PYTEST_XDIST_WORKER env var for all the worker processes. You can use those to find whether xdist is activated or not. More info in their repo: https://github.com/pytest-dev/pytest-xdist – SilentGuy Feb 14 '20 at 18:27
  • Thanks SilentGuy, this is a valuable addition. – smarie Feb 17 '20 at 09:43

0 Answers0