diff --git a/jupyterhub/tests/conftest.py b/jupyterhub/tests/conftest.py index 47d97650..d4e8f04c 100644 --- a/jupyterhub/tests/conftest.py +++ b/jupyterhub/tests/conftest.py @@ -3,11 +3,12 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. +import asyncio import os import logging from getpass import getuser from subprocess import TimeoutExpired -import time + from unittest import mock from pytest import fixture, raises from tornado import ioloop, gen @@ -25,6 +26,7 @@ import jupyterhub.services.service # global db session object _db = None + @fixture def db(): """Get a db session""" @@ -53,6 +55,26 @@ def io_loop(request): return io_loop +@fixture(autouse=True) +def cleanup_after(request): + """function-scoped fixture to shutdown user servers + + allows cleanup of servers between tests + without having to launch a whole new app + """ + try: + yield + finally: + if not MockHub.initialized(): + return + app = MockHub.instance() + loop = asyncio.new_event_loop() + for uid, user in app.users.items(): + for name, spawner in list(user.spawners.items()): + if spawner.active: + loop.run_until_complete(user.stop(name)) + + @fixture(scope='module') def app(request, io_loop): """Mock a jupyterhub app for testing"""