1

I'm executing python unit tests in parallel with pytest-forked (pytest -r sxX -n auto --cache-clear --max-worker-restart=4 --forked) and there is one test case which takes quite some time and which is running at the end while the other test case runners/CPU cores are idle (because presumably there's only this one test cases left to complete).

I'd like to know which test case that is (to maybe run it at the beginning or disable it). Note, this is not a matter of finding the longest running test case as that may not be the culprit. I'm explicitly looking for some way of knowing which test case is assigned to a pytest runner Python process. Calling ps shows something like python -u -c import sys; exec(eval(sys.stdin.readline())) (for as many CPU cores in the machine) which isn't particularly helpful.

Is there a way to set the name of the test case to the process and retrieve it with system tools such as ps? I'm running those test cases on Linux, in case that's relevant.

orange
  • 7,755
  • 14
  • 75
  • 139
  • 1
    setting the name of the test case won't have it appear in `ps` unfortunately, is there no way to determine beforehand which is the long running test case? – gold_cy Jul 14 '22 at 11:17
  • Thanks. Could I use console printouts with `pytest-forked` and create a "global fixture"(?) that prints "started test cases 'xyz'" and "finished test case 'xyz'" and inspect the terminal? How would you go about that (I only get to show dots for every completed test case)? – orange Jul 14 '22 at 22:57
  • 1
    so you’re using it in conjunction with pytest xdist which does not allow using the -s flag due to its design, it’s listed in the documentation as one of the side effects – gold_cy Jul 15 '22 at 00:23
  • You don't need to use print. You can use `logging` to log the process id and see the log using `--log-cli-level` pytest option. You can use `os.getpid()` to get pid of the current xdist runner process. – SilentGuy Jul 26 '22 at 17:01
  • Also, look at this workaround to print to terminal when using xdist: https://stackoverflow.com/a/37210976/2312300 – SilentGuy Jul 26 '22 at 17:02
  • I need the message (log or print) to appear right away when the worker process gets a new test case assigned (to get "PID 1234 working on test_abc" or something along those lines). – orange Jul 27 '22 at 00:29

2 Answers2

0

Since pytest-dist 2.4, there's a solution to showing which test case is running. It requires an additional package setproctitle.

Identifying workers from the system environment

New in version 2.4

If the setproctitle package is installed, pytest-xdist will use it to update the process title (command line) on its workers to show their current state. The titles used are [pytest-xdist running] file.py/node::id and [pytest-xdist idle], visible in standard tools like ps and top on Linux, Mac OS X and BSD systems. For Windows, please follow setproctitle’s pointer regarding the Process Explorer tool.

This is intended purely as an UX enhancement, e.g. to track down issues with long-running or CPU intensive tests. Errors in changing the title are ignored silently. Please try not to rely on the title format or title changes in external scripts.

https://pypi.org/project/pytest-xdist/#identifying-workers-from-the-system-environmentbas

orange
  • 7,755
  • 14
  • 75
  • 139
0

Here is a way to see which test is running when pytest-xdist is processing. Link to docs: https://docs.pytest.org/en/7.1.x/reference/reference.html#_pytest.hookspec.pytest_report_teststatus

Add the following function to your conftest.py file.


#conftest.py

def pytest_report_teststatus(report):
    print(report.__dict__['nodeid'])

Example command to start pytest:

python -m pytest -n 3 -s --show-capture=no --disable-pytest-warnings
Jortega
  • 3,616
  • 1
  • 18
  • 21