機能テストを実行するために、バックグラウンド スレッドでウェイトレス Web サーバーを起動しています。テスト実行の最後にクリーンアップしてウェイトレスを終了するにはどうすればよいですか? KeyboardInterrupt
パブリック ウェイトレス API は、終了シグナルとして期待される一方向のエントリ ポイントのみを提供します。
現在、サーバーをデーモンスレッドで実行しているだけで、テストランナーが終了するまで、すべての新しい Web サーバーのクリーンアップが保留されています。
私のテスト Web サーバー コード:
"""py.test fixtures for spinning up a WSGI server for functional test run."""
import threading
import time
from pyramid.router import Router
from waitress import serve
from urllib.parse import urlparse
import pytest
from backports import typing
#: The URL where WSGI server is run from where Selenium browser loads the pages
HOST_BASE = "http://localhost:8521"
class ServerThread(threading.Thread):
"""Run WSGI server on a background thread.
This thread starts a web server for a given WSGI application. Then the Selenium WebDriver can connect to this web server, like to any web server, for running functional tests.
"""
def __init__(self, app:Router, hostbase:str=HOST_BASE):
threading.Thread.__init__(self)
self.app = app
self.srv = None
self.daemon = True
self.hostbase = hostbase
def run(self):
"""Start WSGI server on a background to listen to incoming."""
parts = urlparse(self.hostbase)
domain, port = parts.netloc.split(":")
try:
# TODO: replace this with create_server call, so we can quit this later
serve(self.app, host='127.0.0.1', port=int(port))
except Exception as e:
# We are a background thread so we have problems to interrupt tests in the case of error. Try spit out something to the console.
import traceback
traceback.print_exc()
def quit(self):
"""Stop test webserver."""
# waitress has no quit
# if self.srv:
# self.srv.shutdown()