Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83358

py.test: dump stuck background threads at the end of the tests

I am using pytest to run my projects Python unit tests.

For some reason, sometimes the test runner does not exist after printing the test stats. I suspect this is because some tests open background threads and some dangling threads are not cleaned up properly in the tear down. As this does not occur every time, it makes it harder to pin down what is exactly happening.

I am hoping to find a way to make pytest to display what threads after it prints failed and passed tests. Some ideas I came up with?

Other alternative ways I think would be just print thread dump at the end of each tear down.

Python 3.4.

Upvotes: 5

Views: 1257

Answers (1)

Bruno Oliveira
Bruno Oliveira

Reputation: 15255

Try using the pytest-timeout plugin... after a timeout occurs, it will dump all threads and exit the process.

If you would like to implement custom code yourself though, take a look at pytest hooks. I guess you could use pytest_runtest_teardown hook to write custom tear down code.

Upvotes: 6

Related Questions