-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
70 lines (59 loc) · 2.5 KB
/
Copy pathconftest.py
File metadata and controls
70 lines (59 loc) · 2.5 KB
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
import os
from datetime import datetime
import pytest
# Hook to configure pytest and add a custom marker
def pytest_configure(config):
# Add a custom marker that can be used across tests
config.addinivalue_line(
"markers", "smoke: mark test as part of the smoke test suite"
)
# time = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
# report_dir = "reports"
# report_file = f"My_Report_{time}.html"
# if not os.path.exists(report_dir):
# os.makedirs(report_dir)
# if not config.option.htmlpath:
# config.option.htmlpath = os.path.join(report_dir, report_file)
if not config.option.htmlpath:
config.option.htmlpath = "report/test_report.html"
# Hook to add custom metadata to the HTML report
@pytest.hookimpl(tryfirst=True)
def pytest_metadata(metadata):
# Add custom metadata to the report
metadata['Project Name'] = 'My Test Project'
metadata['Module Name'] = 'API Tests'
metadata['Tester'] = 'Haris'
# Hook to modify the title of the HTML report
def pytest_html_report_title(report):
report.title = "My Project Test Report"
# Hook to modify the environment information in the HTML report
def pytest_html_results_summary(prefix, summary, postfix):
prefix.extend([f"Project: My Test Project", "API Test Results"])
postfix.extend(["Tester: Haris"])
# Session-scoped fixture to set up a test environment
@pytest.fixture(scope="session")
def setup_environment():
print("\nSetting up the environment for all tests")
yield
print("\nTearing down the environment after all tests")
# Function-scoped fixture to provide mock data
@pytest.fixture(scope="function")
def mock_data():
print("\nSetting up mock data for the test")
data = {"key": "value", "number": 123}
yield data
print("\nTearing down mock data after the test")
# Hook to add test results to the HTML report
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
# This hook is responsible for adding extra info to the HTML report
outcome = yield
report = outcome.get_result()
if report.when == 'call' and report.failed:
report.extra = getattr(report, 'extra', [])
# Example: Add a screenshot or other extra information (commented out for now)
# report.extra.append(pytest_html.extras.image('screenshot.png'))
# Hook to generate the HTML report
def pytest_sessionfinish(session, exitstatus):
# You can add any final cleanup or reporting tasks here
print(f"Tests finished with exit status: {exitstatus}")