-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
63 lines (54 loc) · 2.2 KB
/
conftest.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
53
54
55
56
57
58
59
60
61
62
63
import pytest
from websocket import create_connection
from websocket import _exceptions as ws_exception
from lib.setup_logger import logger
def pytest_addoption(parser):
""" TODO"""
parser.addoption("--url", action="store")
parser.addoption("--port", action="store")
@pytest.fixture(name="url", scope="class")
def url_fixture(request):
""" TODO """
logger.info("url() fixture is being called")
logger.info("-----------------------------\n")
# if option does not exist, None value will be returned.
_url = request.config.getoption("--url")
logger.info("url() fixture : url={}".format(_url))
request.cls.url = _url
logger.info("url() fixture is done")
logger.info("---------------------")
return request.cls.url
@pytest.fixture(name="port", scope="class")
def port_fixture(request):
""" TODO """
logger.info("port() fixture is being called")
logger.info("------------------------------\n")
# if option does not exist, None value will be returned.
_port = request.config.getoption("--port")
logger.info("url() fixture : url={}".format(_port))
request.cls.port = _port
logger.info("port() fixture is done")
logger.info("----------------------")
return request.cls.port
@pytest.fixture(scope="function")
def connect(request, url, port):
""" TODO"""
expected_greeting = f"Greetings, friend! Type <tt>help</tt> to get started."
ws_url = f"{url}:{port}"
try:
# create websocket connection
logger.info(f"Creating websocket connection to {ws_url}")
_connect = create_connection(ws_url)
actual_greeting = _connect.recv()
assert expected_greeting == actual_greeting, f"ERROR: Actual greeting: \"{actual_greeting}\" differs from " \
f"Expected greeting : {expected_greeting}"
except (ws_exception.WebSocketAddressException, ValueError):
_connect = None
assert _connect is not None, f"Failed to establish websocket connection"
request.cls.connect = _connect
def fin():
logger.info(f"Closing websocket connection.")
if _connect:
_connect.close()
request.addfinalizer(fin)
return request.cls.connect