Coverage for tests/tests_pkg_one/conftest.py: 71%

21 statements  

« prev     ^ index     » next       coverage.py v7.2.1, created at 2024-07-25 16:40 +0300

1import sys 

2 

3# def teardown_function(function): 

4# """teardown any state that was previously setup with a setup_function call.""" 

5# print(f"Teardown_function {function.__name__}", file=sys.stderr) 

6 

7 

8# SaveFailedTestsPlugin.py 

9import pdb 

10import json 

11import os 

12from _pytest.reports import TestReport 

13import pytest 

14 

15 

16# class SaveFailedTestsPlugin: 

17# def __init__(self, config): 

18# self.failed_tests = [] 

19# 

20# # @pytest.hookimpl(tryfirst=True) 

21# def pytest_runtest_teardown(self, item, nextitem): 

22# # pdb.set_trace() 

23# result = item._result # Access the result object 

24# 

25# # Check if the test has failed 

26# if result and result.when == "call" and result.failed: 

27# test_info = { 

28# "name": item.name, 

29# "location": item.location, 

30# "parameters": item.keywords.get("fixturemanager").params 

31# } 

32# self.failed_tests.append(test_info) 

33# 

34# def pytest_sessionfinish(self, session, exitstatus): 

35# # Save information about failed tests to a JSON file 

36# output_file = "failed_tests.json" 

37# with open(output_file, "w") as f: 

38# json.dump(self.failed_tests, f) 

39# 

40# 

41# def pytest_configure(config): 

42# config.pluginmanager.register(SaveFailedTestsPlugin(config), "save-failed-tests") 

43 

44 

45# def pytest_runtest_teardown(item, next_item): 

46# pdb.set_trace() 

47 

48 

49def pytest_sessionstart(session): 

50 session.results = dict() 

51 

52 

53@pytest.hookimpl(tryfirst=True, hookwrapper=True) 

54def pytest_runtest_makereport(item, call): 

55 outcome = yield 

56 result = outcome.get_result() 

57 

58 if result.when == 'call': 

59 # check if its failure 

60 if result.failed: 

61 # add the result to the session 

62 # pdb.set_trace() 

63 # get the parameters 

64 # params = item.funcargs 

65 item.session.results[item] = item.funcargs['param'] 

66 # add marker to the test that filed 

67 item.add_marker(pytest.mark.xxaymanxx) 

68 # item.session.results[item] = result 

69 

70def pytest_sessionfinish(session, exitstatus): 

71 print() 

72 print('run status code:', exitstatus) 

73 # passed_amount = sum(1 for result in session.results.values() if result.passed) 

74 # failed_amount = sum(1 for result in session.results.values() if result.failed) 

75 # print(f'there are {passed_amount} passed and {failed_amount} failed tests') 

76 # pdb.set_trace() 

77 for item in session.results: 

78 print(f'Failed Tests: {item.name}, with params: {session.results[item]}')