Coverage for tests/pytest_SaveFailedTestsPlugin.py: 0%
19 statements
« prev ^ index » next coverage.py v7.2.1, created at 2024-07-25 16:40 +0300
« prev ^ index » next coverage.py v7.2.1, created at 2024-07-25 16:40 +0300
1# SaveFailedTestsPlugin.py
3import json
4import os
5from _pytest.reports import TestReport
6import pytest
9class SaveFailedTestsPlugin:
10 def __init__(self, config):
11 self.failed_tests = []
13 @pytest.hookimpl(tryfirst=True)
14 def pytest_runtest_protocol(self, item, nextitem):
15 result = nextitem._result # Access the result object
17 # Check if the test has failed
18 if result and result.when == "call" and result.failed:
19 test_info = {
20 "name": item.name,
21 "location": item.location,
22 "parameters": item.keywords.get("fixturemanager").params
23 }
24 self.failed_tests.append(test_info)
26 def pytest_sessionfinish(self, session, exitstatus):
27 # Save information about failed tests to a JSON file
28 output_file = "failed_tests.json"
29 with open(output_file, "w") as f:
30 json.dump(self.failed_tests, f)
33def pytest_configure(config):
34 config.pluginmanager.register(SaveFailedTestsPlugin(config), "save-failed-tests")