Total Complexity | 3 |
Total Lines | 49 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | """Tests for logger.""" |
||
2 | import logging |
||
3 | import os |
||
4 | from pathlib import Path |
||
5 | |||
6 | import pytest |
||
7 | |||
8 | from deepreg import log |
||
9 | |||
10 | |||
11 | @pytest.fixture() |
||
12 | def log_file_path(tmp_path_factory) -> Path: |
||
13 | """ |
||
14 | Fixture for temporary log file. |
||
15 | |||
16 | :param tmp_path_factory: default fixture. |
||
17 | :return: a temporary file path |
||
18 | """ |
||
19 | return tmp_path_factory.mktemp("test_log") / "log.txt" |
||
20 | |||
21 | |||
22 | @pytest.mark.parametrize("log_level", [0, 1, 2, 3, 4, 5]) |
||
23 | def test_get(log_level: int, log_file_path: Path): |
||
24 | """ |
||
25 | Test loggers by count number of logs. |
||
26 | |||
27 | :param log_level: 0 to 5. |
||
28 | :param log_file_path: path of a temporary log file. |
||
29 | """ |
||
30 | os.environ["DEEPREG_LOG_LEVEL"] = str(log_level) |
||
31 | logger = log.get(__name__) |
||
32 | |||
33 | # save logs into a file |
||
34 | handler = logging.FileHandler(log_file_path) |
||
35 | handler.setLevel(logging.DEBUG) |
||
36 | logger.addHandler(handler) |
||
37 | |||
38 | logger.debug("DEBUG") |
||
39 | logger.info("INFO") |
||
40 | logger.warning("WARNING") |
||
41 | logger.error("ERROR") |
||
42 | logger.critical("CRITICAL") |
||
43 | |||
44 | # count lines in the file |
||
45 | with open(log_file_path, "r") as f: |
||
46 | num_logs = len(f.readlines()) |
||
47 | |||
48 | assert num_logs == 6 - max(1, log_level) |
||
49 |