Coverage for tests/utils/base_test_class.py: 100%

42 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 19:49 +0000

1import logging 

2import pytest 

3 

4from abc import ABC 

5from time import perf_counter 

6from typing import Any, Generator 

7from monero import MoneroUtils 

8 

9logger: logging.Logger = logging.getLogger("BaseTestClass") 

10 

11 

12class BaseTestClass(ABC): 

13 """Base test class with common fixtures.""" 

14 

15 _elapsed_ms: float = 0.0 

16 """Milliseconds that the last test took, set when the test ends and before `after_each`.""" 

17 

18 # Setup and teardown of test class 

19 @pytest.fixture(scope="class", autouse=True) 

20 @classmethod 

21 def global_setup_and_teardown(cls) -> Generator[None, Any, None]: 

22 """Executed once before all tests. 

23 

24 :returns Generator[None, Any, None]: yields control to the test class after setup, then 

25 runs teardown once all tests in the class have completed. 

26 """ 

27 # the hooks are methods of an instance, which is not the one of a test: what they set on it would be seen by none 

28 instance: BaseTestClass = cls() 

29 instance.before_all() 

30 yield 

31 instance.after_all() 

32 

33 # Setup and teardown of each test 

34 @pytest.fixture(autouse=True) 

35 def setup_and_teardown(self, request: pytest.FixtureRequest) -> Generator[None, Any, None]: 

36 """Executed before each test. 

37 

38 :param pytest.FixtureRequest request: the pytest request fixture for the running test. 

39 :returns Generator[None, Any, None]: yields control to the test after setup, then runs 

40 teardown once the test has completed. 

41 """ 

42 self.before_each(request) 

43 # the time of the test itself: after what before_each does, until the test ends 

44 started: float = perf_counter() 

45 yield 

46 self._elapsed_ms = (perf_counter() - started) * 1000 

47 self.after_each(request) 

48 

49 # Before all tests 

50 def before_all(self) -> None: 

51 """Executed once before all tests.""" 

52 msg: str = f"Setup test class {type(self).__name__}" 

53 MoneroUtils.log_info(msg) 

54 logger.info(msg) 

55 

56 # After all tests 

57 def after_all(self) -> None: 

58 """Executed once after all tests.""" 

59 msg: str = f"Teardown test class {type(self).__name__}" 

60 MoneroUtils.log_info(msg) 

61 logger.info(msg) 

62 

63 # Before each test 

64 def before_each(self, request: pytest.FixtureRequest) -> None: 

65 """Executed before each test. 

66 

67 :param pytest.FixtureRequest request: Request fixture. 

68 """ 

69 msg: str = f"Before {request.node.name}" 

70 MoneroUtils.log_info(msg) 

71 logger.info(msg) 

72 

73 # After each test 

74 def after_each(self, request: pytest.FixtureRequest) -> None: 

75 """Executed after each test. 

76 

77 :param pytest.FixtureRequest request: Request fixture. 

78 """ 

79 msg: str = self.after_message(request) 

80 MoneroUtils.log_info(msg) 

81 logger.info(msg) 

82 

83 def after_message(self, request: pytest.FixtureRequest) -> str: 

84 """The message that says that a test ended, with the time that it took. 

85 

86 :param pytest.FixtureRequest request: Request fixture. 

87 :returns str: `After <test> (<milliseconds> ms)`. 

88 """ 

89 return f"After {request.node.name} ({self._elapsed_ms:.1f} ms)"