-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytest_plugin.py
52 lines (40 loc) · 1.75 KB
/
pytest_plugin.py
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
from subprocess import Popen
import pytest
from pytest_simplehttpserver.simplehttpserver import http_server_process
def determine_scope(fixture_name, config):
scope = config.getoption("--simplehttpserver-scope", None)
if scope is None:
return "function"
else:
if scope not in ("function", "class", "module", "package", "session"):
raise ValueError("--simplehttpserver must be set to one of: function, class, module, package or session.")
return scope
@pytest.fixture(scope=determine_scope)
def simplehttpserver(request):
directory: str = request.config.getoption('simplehttpserver_directory')
if directory is None:
raise TypeError('The following pytest arguments are required: --simplehttpserver-directory')
server_process: Popen = http_server_process(directory)
yield server_process
server_process.terminate()
server_process.wait()
def pytest_addoption(parser):
group = parser.getgroup('simplehttpserver')
group.addoption(
'--simplehttpserver-directory',
action='store',
dest='simplehttpserver_directory',
required=False,
help='Path to the directory containing the root (index.html) file to serve.'
)
group.addoption(
'--simplehttpserver-scope',
action='store',
dest='simplehttpserver_scope',
required=False,
default="function",
help='If using the server for multiple tests, set the scope for "reusage": '
'function, class, module, package, session'
)
parser.addini('SIMPLEHTTPSERVER_DIRECTORY', 'Directory containing the root (index.html) file to serve.')
parser.addini('SIMPLEHTTPSERVER_SCOPE', 'Scope for server reusage: function, class, module, package, session.')