forked from csernazs/pytest-httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpytest_plugin.py
More file actions
54 lines (37 loc) · 1.13 KB
/
pytest_plugin.py
File metadata and controls
54 lines (37 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import pytest
from .httpserver import HTTPServer
class Plugin:
SERVER = None
class PluginHTTPServer(HTTPServer):
def start(self):
super().start()
Plugin.SERVER = self
def stop(self):
super().stop()
Plugin.SERVER = None
def get_httpserver_listen_address():
listen_host = os.environ.get("PYTEST_HTTPSERVER_HOST")
listen_port = os.environ.get("PYTEST_HTTPSERVER_PORT")
if listen_port:
listen_port = int(listen_port)
return (listen_host, listen_port)
@pytest.fixture
def httpserver():
if Plugin.SERVER:
Plugin.SERVER.clear()
yield Plugin.SERVER
return
host, port = get_httpserver_listen_address()
if not host:
host = HTTPServer.DEFAULT_LISTEN_HOST
if not port:
port = HTTPServer.DEFAULT_LISTEN_PORT
server = PluginHTTPServer(host=host, port=port)
server.start()
yield server
def pytest_sessionfinish(session, exitstatus): # pylint: disable=unused-argument
if Plugin.SERVER is not None:
Plugin.SERVER.clear()
if Plugin.SERVER.is_running():
Plugin.SERVER.stop()