-
Notifications
You must be signed in to change notification settings - Fork 2
/
conftest.py
97 lines (71 loc) · 2.75 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import pathlib
import logging
import shutil
import pytest
from utilities.logger import setup_logging
LOGGER = logging.getLogger(__name__)
BASIC_LOGGER = logging.getLogger("basic")
def separator(symbol_, val=None):
terminal_width = shutil.get_terminal_size(fallback=(120, 40))[0]
if not val:
return f"{symbol_ * terminal_width}"
sepa = int((terminal_width - len(val) - 2) // 2)
return f"{symbol_ * sepa} {val} {symbol_ * sepa}"
def pytest_addoption(parser):
log_collector_group = parser.getgroup(name="LogCollector")
log_collector_group.addoption(
"--pytest-log-file",
help="Path to pytest log file",
default="pytest-tests.log",
)
def pytest_sessionstart(session):
tests_log_file = session.config.getoption("pytest_log_file")
if os.path.exists(tests_log_file):
pathlib.Path(tests_log_file).unlink()
setup_logging(
log_file=tests_log_file,
log_level=session.config.getoption("log_cli_level") or logging.INFO,
)
def pytest_report_teststatus(report, config):
test_name = report.head_line
when = report.when
call_str = "call"
if report.passed:
if when == call_str:
BASIC_LOGGER.info(f"\nTEST: {test_name} STATUS: \033[0;32mPASSED\033[0m")
elif report.skipped:
BASIC_LOGGER.info(f"\nTEST: {test_name} STATUS: \033[1;33mSKIPPED\033[0m")
elif report.failed:
if when != call_str:
BASIC_LOGGER.info(
f"\nTEST: {test_name} [{when}] STATUS: \033[0;31mERROR\033[0m"
)
else:
BASIC_LOGGER.info(f"\nTEST: {test_name} STATUS: \033[0;31mFAILED\033[0m")
def pytest_sessionfinish(session, exitstatus):
reporter = session.config.pluginmanager.get_plugin("terminalreporter")
reporter.summary_stats()
def pytest_runtest_makereport(item, call):
"""
incremental tests implementation
"""
if call.excinfo is not None and "incremental" in item.keywords:
parent = item.parent
parent._previousfailed = item
def pytest_fixture_setup(fixturedef, request):
LOGGER.info(f"Executing {fixturedef.scope} fixture: {fixturedef.argname}")
def pytest_runtest_setup(item):
"""
Use incremental
"""
BASIC_LOGGER.info(f"\n{separator(symbol_='-', val=item.name)}")
BASIC_LOGGER.info(f"{separator(symbol_='-', val='SETUP')}")
if "incremental" in item.keywords:
previousfailed = getattr(item.parent, "_previousfailed", None)
if previousfailed is not None:
pytest.xfail("previous test failed (%s)" % previousfailed.name)
def pytest_runtest_call(item):
BASIC_LOGGER.info(f"{separator(symbol_='-', val='CALL')}")
def pytest_runtest_teardown(item):
BASIC_LOGGER.info(f"{separator(symbol_='-', val='TEARDOWN')}")