-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
79 lines (60 loc) · 2.04 KB
/
test.py
File metadata and controls
79 lines (60 loc) · 2.04 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
import logging
import pytest
from log_decorator import log
@log(ignore=["api_key"])
def foo(x, y, api_key):
return x + y
class MyClass:
@log(ignore=["api_key"])
def foo(self, x, y, api_key):
return x + y
@pytest.fixture
def caplog(caplog):
caplog.set_level(logging.DEBUG)
return caplog
def test_function_log(caplog):
# given, when
result = foo(1, y=2, api_key="my_secret_key")
# then
assert result == 3
assert len(caplog.records) == 1
assert caplog.records[0].levelname == "DEBUG"
log_message = caplog.records[0].message
assert (
log_message
== "foo successfully called {'args': {'x': 1, 'y': 2, 'api_key': '**SECRET**'}, 'return_value': 3}"
)
def test_function_error_log(caplog):
with pytest.raises(TypeError):
foo("a", 2, api_key="my_secret_key")
assert len(caplog.records) == 1
assert caplog.records[0].levelname == "ERROR"
log_message = caplog.records[0].message
assert (
log_message
== "foo encountered an error {'args': {'x': 'a', 'y': 2, 'api_key': '**SECRET**'}, 'error': 'can only concatenate str (not \"int\") to str'}"
)
def test_class_method_log(caplog):
# given, when
result = MyClass().foo(1, y=2, api_key="my_secret_key")
# then
assert result == 3
assert len(caplog.records) == 1
assert caplog.records[0].levelname == "DEBUG"
log_message = caplog.records[0].message
assert (
log_message
== "MyClass::foo successfully called {'args': {'x': 1, 'y': 2, 'api_key': '**SECRET**'}, 'return_value': 3}"
)
def test_class_method_error_log(caplog):
# given
with pytest.raises(TypeError):
MyClass().foo("a", 2, api_key="my_secret_key")
# then
assert len(caplog.records) == 1
assert caplog.records[0].levelname == "ERROR"
log_message = caplog.records[0].message
assert (
log_message
== "MyClass::foo encountered an error {'args': {'x': 'a', 'y': 2, 'api_key': '**SECRET**'}, 'error': 'can only concatenate str (not \"int\") to str'}"
)