-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathtest_logging.py
More file actions
147 lines (104 loc) · 3.49 KB
/
test_logging.py
File metadata and controls
147 lines (104 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from __future__ import annotations
import logging
import pytest
from cleo.application import Application
from cleo.logging.cleo_handler import CleoHandler
from cleo.testers.application_tester import ApplicationTester
from tests.fixtures.foo4_command import Foo4Command
@pytest.fixture
def app() -> Application:
app = Application()
cmd = Foo4Command()
app.add(cmd)
app._default_command = cmd.name
return app
@pytest.fixture
def tester(app: Application) -> ApplicationTester:
app.catch_exceptions(False)
return ApplicationTester(app)
@pytest.fixture
def root_logger() -> logging.Logger:
root = logging.getLogger()
root.setLevel(logging.NOTSET)
return root
def test_cleohandler_normal(
tester: ApplicationTester,
root_logger: logging.Logger,
) -> None:
handler = CleoHandler(tester.io.output)
root_logger.addHandler(handler)
status_code = tester.execute("")
expected = "This is an warning log record\nThis is an error log record\n"
assert status_code == 0
assert tester.io.fetch_output() == expected
def test_cleohandler_quiet(
tester: ApplicationTester,
root_logger: logging.Logger,
) -> None:
handler = CleoHandler(tester.io.output)
root_logger.addHandler(handler)
status_code = tester.execute("-q")
assert status_code == 0
assert tester.io.fetch_output() == ""
def test_cleohandler_verbose(
tester: ApplicationTester,
root_logger: logging.Logger,
) -> None:
handler = CleoHandler(tester.io.output)
root_logger.addHandler(handler)
status_code = tester.execute("-v")
expected = (
"This is an info log record\n"
"This is an warning log record\n"
"This is an error log record\n"
)
assert status_code == 0
assert tester.io.fetch_output() == expected
def test_cleohandler_very_verbose(
tester: ApplicationTester,
root_logger: logging.Logger,
) -> None:
handler = CleoHandler(tester.io.output)
root_logger.addHandler(handler)
status_code = tester.execute("-vv")
expected = (
"This is an debug log record\n"
"This is an info log record\n"
"This is an warning log record\n"
"This is an error log record\n"
)
assert status_code == 0
assert tester.io.fetch_output() == expected
def test_cleohandler_exception_normal(
tester: ApplicationTester,
root_logger: logging.Logger,
) -> None:
handler = CleoHandler(tester.io.output)
root_logger.addHandler(handler)
status_code = tester.execute("--exception")
assert status_code == 0
lines = tester.io.fetch_output().splitlines()
assert len(lines) == 7
assert lines[0] == "This is an exception that I raised"
def test_cleohandler_exception_verbose(
tester: ApplicationTester,
root_logger: logging.Logger,
) -> None:
handler = CleoHandler(tester.io.output)
root_logger.addHandler(handler)
status_code = tester.execute("-v --exception")
assert status_code == 0
lines = tester.io.fetch_output().splitlines()
assert len(lines) == 20
assert lines[0] == "This is an exception that I raised"
def test_cleohandler_exception_very_verbose(
tester: ApplicationTester,
root_logger: logging.Logger,
) -> None:
handler = CleoHandler(tester.io.output)
root_logger.addHandler(handler)
status_code = tester.execute("-vv --exception")
assert status_code == 0
lines = tester.io.fetch_output().splitlines()
assert len(lines) == 20
assert lines[0] == "This is an exception that I raised"