Python – pytest session does not end after all tests have been completed

pytest session does not end after all tests have been completed… here is a solution to the problem.

pytest session does not end after all tests have been completed

I’m trying to integrate pytests into my Jenkins project, but the Jenkins job can’t complete because pytest’s test session hasn’t ended yet.

This is the simplest example I can come up with:

Python test:

import pytest
import sys, time
sys.path.append('.. /.. /bindings/python')

class TestControl():
    def test_sanity_check(self):
        """
        Basic Test
        """
        assert (True)

Console commands and output:

 python git:(ed25b33) ✗ sudo nano test_control.py 
➜  python git:(ed25b33) ✗ sudo python3 -m pytest -s test_control.py::TestControl:: test_sanity_check
=============================================================================================================================== test session starts  ===============================================================================================================================
platform linux -- Python 3.4.3, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /var/lib/jenkins/workspace/examples/python, inifile:
collected 1 item                                                                                                                                                                                                                                                                  
test_control.py .                                                                                                                                                                                                                                                           [100%]

============================================================================================================================ 1 passed in 0.67 seconds  =============================================================================================================================

The test showed 100% complete, but the session was not terminated for some reason. I tried adding sys.exit() to the script but it causes an error. I just need to test to release the console so my Jenkins work is done.

Solution

It turns out I have an setup_class but no teardown_class, and my class instance is still in an event state and is preventing the test session from ending.

Related Problems and Solutions