Coverage for tests/test_loggers.py: 100.00%

30 statements  

« prev     ^ index     » next       coverage.py v7.6.2, created at 2024-10-12 18:59 +0200

1"""Tests for the loggers module.""" 

2 

3from unittest.mock import MagicMock 

4 

5import pytest 

6 

7from mkdocstrings.loggers import get_logger, get_template_logger 

8 

9 

10@pytest.mark.parametrize( 

11 "kwargs", 

12 [ 

13 {}, 

14 {"once": False}, 

15 {"once": True}, 

16 ], 

17) 

18def test_logger(kwargs: dict, caplog: pytest.LogCaptureFixture) -> None: 

19 """Test logger methods. 

20 

21 Parameters: 

22 kwargs: Keyword arguments passed to the logger methods. 

23 """ 

24 logger = get_logger("mkdocstrings.test") 

25 caplog.set_level(0) 

26 for _ in range(2): 

27 logger.debug("Debug message", **kwargs) 

28 logger.info("Info message", **kwargs) 

29 logger.warning("Warning message", **kwargs) 

30 logger.error("Error message", **kwargs) 

31 logger.critical("Critical message", **kwargs) 

32 if kwargs.get("once", False): 

33 assert len(caplog.records) == 5 

34 else: 

35 assert len(caplog.records) == 10 

36 

37 

38@pytest.mark.parametrize( 

39 "kwargs", 

40 [ 

41 {}, 

42 {"once": False}, 

43 {"once": True}, 

44 ], 

45) 

46def test_template_logger(kwargs: dict, caplog: pytest.LogCaptureFixture) -> None: 

47 """Test template logger methods. 

48 

49 Parameters: 

50 kwargs: Keyword arguments passed to the template logger methods. 

51 """ 

52 logger = get_template_logger() 

53 mock = MagicMock() 

54 caplog.set_level(0) 

55 for _ in range(2): 

56 logger.debug(mock, "Debug message", **kwargs) 

57 logger.info(mock, "Info message", **kwargs) 

58 logger.warning(mock, "Warning message", **kwargs) 

59 logger.error(mock, "Error message", **kwargs) 

60 logger.critical(mock, "Critical message", **kwargs) 

61 if kwargs.get("once", False): 

62 assert len(caplog.records) == 5 

63 else: 

64 assert len(caplog.records) == 10