Skip to content

Commit 54efe52

Browse files
logging
1 parent 07b4d8b commit 54efe52

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ venv/
2222

2323
# Local
2424
logs/pytest.log
25+
logs/test_level.log
2526
main.py
2627
reports/*
2728
working/*

conftest.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import datetime
2+
13
import pytest
24
from playwright.sync_api import sync_playwright
35

46
from libs import CurrentExecution as ce
57
from libs import file_ops as fo
6-
from libs.generic_constants import fixture_scope
8+
from libs.generic_constants import audit_log_paths, file_mode, fixture_scope
79
from libs.mavis_constants import browsers_and_devices, playwright_constants
810
from libs.wrappers import *
911

@@ -88,3 +90,40 @@ def close_browser(browser, page):
8890
page.get_by_role("button", name="Log out").click()
8991
page.close()
9092
browser.close()
93+
94+
95+
@pytest.hookimpl(tryfirst=True)
96+
def pytest_sessionstart(session):
97+
with open(audit_log_paths.TEST_LEVEL_LOG, file_mode.APPEND) as log_file:
98+
log_file.write(f"Test Session Started: {datetime.now()}\n")
99+
100+
101+
@pytest.hookimpl(trylast=True)
102+
def pytest_sessionfinish(session, exitstatus):
103+
with open(audit_log_paths.TEST_LEVEL_LOG, file_mode.APPEND) as log_file:
104+
log_file.write(f"Test Session Ended: {datetime.now()}\n")
105+
106+
107+
@pytest.hookimpl(hookwrapper=True)
108+
def pytest_runtest_logreport(report):
109+
outcome = yield
110+
111+
if report.when == "call": # Log only actual test results
112+
test_name = report.nodeid
113+
test_result = report.outcome.upper() # 'passed', 'failed', or 'skipped'
114+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
115+
116+
with open(audit_log_paths.TEST_LEVEL_LOG, file_mode.APPEND) as log_file:
117+
log_file.write(f"{timestamp} | {test_name} | {test_result}\n")
118+
119+
120+
# @pytest.fixture
121+
# def step_logger():
122+
# """Fixture to log individual steps within a test."""
123+
124+
# def log_step(step_description):
125+
# timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
126+
# with open(audit_log_paths.TEST_LEVEL_LOG, file_mode.APPEND) as log_file:
127+
# log_file.write(f"{timestamp} | STEP | {step_description}\n")
128+
129+
# return log_step

libs/generic_constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,7 @@ class escape_characters:
134134

135135
class file_encoding:
136136
ASCII: Final[str] = "ascii"
137+
138+
139+
class audit_log_paths:
140+
TEST_LEVEL_LOG: Final[str] = "logs/test_level.log"

0 commit comments

Comments
 (0)