Cavoke  1.1.0
A Platform for creating and hosting multiplayer turn-based board games
Loading...
Searching...
No Matches
conftest.py
1import subprocess
2
3import cavoke_openapi_client
4import pytest
5
6
7def pytest_addoption(parser: pytest.Parser):
8 parser.addoption("--endpoint", action="store", default="http://localhost:8080", help="API endpoint "
9 "to test, "
10 "usually your localhost")
11 parser.addoption("--servercmd", action="store", default=None, help="command to start the server process")
12
13
14def pytest_configure(config: pytest.Config):
15 """
16 Allows plugins and conftest files to perform initial configuration.
17 This hook is called for every plugin and initial conftest
18 file after command line options have been parsed.
19 """
20 host = config.getoption("endpoint")
21 print(f"Using this endpoint for testing: '{host}'", flush=True)
22 print(f"Please make sure that the server is NOT using JWT Auth!!!", flush=True)
23 pytest.server_config = cavoke_openapi_client.Configuration(host=host)
24
25
26def pytest_sessionstart(session: pytest.Session):
27 """
28 Called after the Session object has been created and
29 before performing collection and entering the run test loop.
30 """
31 server_cmd = session.config.getoption("servercmd")
32 if server_cmd is None:
33 print("No process started!!!", flush=True)
34 else:
35 print(f"Starting server process with '{server_cmd}'...", flush=True)
36 pytest.server_process = subprocess.Popen(args=server_cmd, shell=True)
37
38
39def pytest_sessionfinish(session, exitstatus):
40 """
41 Called after whole test run finished, right before
42 returning the exit status to the system.
43 """
44 if session.config.getoption("servercmd") is None:
45 return
46 try:
47 print('Waiting for server to finish...', flush=True)
48 pytest.server_process.wait(timeout=0.5)
49 except subprocess.TimeoutExpired:
50 print('Server terminating by force...', flush=True)
51 pytest.server_process.kill()
52 print('Server process stopped', flush=True)
53
54
55def pytest_unconfigure(config):
56 """
57 called before test process is exited.
58 """