pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
12.14k stars 2.69k forks source link

pytest hangs after tests finish running #7250

Closed akhileshraju closed 4 years ago

akhileshraju commented 4 years ago

The test setup that I have needs to connect to boards to be able to run tests. For this purpose, I wrote a package that makes use of pexpect to ssh into the machine the board is connected to and run some commands to make the board accessible to the test. I defined my own pytest_collection_modifyitems and pytest_unconfigure in the conftests.py as

@pytests.marker.trylast
def pytest_collection_modifyitems(items):
     # Here I parse through all the tests and figure out which of them need a board.
     # Then I try to get those boards
     # If I am not able to obtain a board, the corresponding test is skipped by adding the skip marker to the test item
     print(f"Acquired - {boards}")

@pytest.marker.trylast
def pytest_unconfigure():
     # I close all ssh connection here
     print("Released all boards to the barn")

I start the test on the command line with pytest. The collection hook I defined is called, the test are executed, the pass/fail banner is printed and the last print "Released all boards to the barn" from my hook def is printed. But for some reason, pytest doesn't stop, unless I do ctrl+c. It stays stuck without releasing control. This happens on both Windows 10 and CentOS 7.4.

image

I first ran this in PyCharm 2020.1 using their inbuilt test runner. In "Run" mode I see the same behavior i.e. the pytest session hangs after doing all the things. But in "Debug" mode (i.e. using the PyCharm debugger), the test session runs all hooks and tasks, prints the test results banner and exits with exit code -1, without any external intervention.

To eliminate PyCharm as the source of the issue, I went to terminal (Linux) and cmd (Win), activated my venv and ran pytest in the tests folder and the tests hang.

Even though the --pdb option is for tests, I was curious to see if I could use that to find out where pytest was getting stuck. But unfortunately didnt have any luck.

Can someone please help with some pointers? Any ideas will be helpful. I have run out of ideas to debug this.

My venv has the following

Package                       Version     Location
----------------------------- ----------- ----------------------------------------------------------------------
alabaster                     0.7.12
appdirs                       1.4.4
atomicwrites                  1.4.0
attrs                         19.3.0
Babel                         2.8.0
backcall                      0.1.0
black                         19.10b0
bleach                        3.1.5
certifi                       2020.4.5.1
chardet                       3.0.4
click                         7.1.2
colorama                      0.4.3
decorator                     4.4.2
defusedxml                    0.6.0
docutils                      0.16
entrypoints                   0.3
idna                          2.9
imagesize                     1.2.0
ipykernel                     5.3.0
ipython                       7.14.0
ipython-genutils              0.2.0
jedi                          0.17.0
Jinja2                        2.11.2
json5                         0.9.4
jsonschema                    3.2.0
jupyter-client                6.1.3
jupyter-core                  4.6.3
jupyterlab                    2.1.2
jupyterlab-server             1.1.4
loguru                        0.5.0
MarkupSafe                    1.1.1
mistune                       0.8.4
more-itertools                8.3.0
mypy                          0.770
mypy-extensions               0.4.3
nbconvert                     5.6.1
nbformat                      5.0.6
networkx                      2.4
notebook                      6.0.3
packaging                     20.4
pandocfilters                 1.4.2
parso                         0.7.0
pathspec                      0.8.0
pexpect                       4.8.0
pickleshare                   0.7.5
pip                           19.2.3
pluggy                        0.13.1
prometheus-client             0.7.1
prompt-toolkit                3.0.5
ptyprocess                    0.6.0
py                            1.8.1
Pygments                      2.6.1
pyparsing                     2.4.7
pyrsistent                    0.16.0
pytest                        5.4.2
python-dateutil               2.8.1
pytz                          2020.1
pywin32                       227
pywinpty                      0.5.7
pyzmq                         19.0.1
regex                         2020.5.14
requests                      2.23.0
Send2Trash                    1.5.0
setuptools                    41.2.0
six                           1.14.0
snowballstemmer               2.0.0
Sphinx                        3.0.3
sphinx-autodoc-typehints      1.10.3
sphinx-rtd-theme              0.4.3
sphinxcontrib-applehelp       1.0.2
sphinxcontrib-devhelp         1.0.2
sphinxcontrib-htmlhelp        1.0.3
sphinxcontrib-jsmath          1.0.1
sphinxcontrib-qthelp          1.0.3
sphinxcontrib-serializinghtml 1.1.4
terminado                     0.8.3
testpath                      0.4.4
toml                          0.10.1
tornado                       6.0.4
traitlets                     4.3.3
typed-ast                     1.4.1
typing-extensions             3.7.4.2
urllib3                       1.25.9
wcwidth                       0.1.9
webencodings                  0.5.1
win32-setctime                1.0.1

For now, I hacked it by adding os.kill(os.getpid(), signal.SIGTERM) as the last statement in my pytest_unconfigure hook. That gets pytest to stop, even though I don't like the solution.

Zac-HD commented 4 years ago

Closing this issue because it's been inactive for two weeks - maybe try the pytest tag on StackOverflow?

BQTHake003 commented 4 months ago

I have the same issue. After running pytest and finishes, it hangs indefinitely and not even Ctrl+c or Ctrl+z can kill the process.

I had to kill it on tests teardown as suggested by @akhileshraju by sending the kill signal to terminal.

It is worth mentioning that I am not using venv.

I am using:

  1. Windows 10
  2. python 3.11
  3. pytest 7.4.4
  4. pytest-bdd plugin
  5. pytest.fixture (tried with different scopes with no difference)