1
|
|
|
# pylint: disable=redefined-outer-name,unused-variable,expression-not-assigned,singleton-comparison |
2
|
|
|
|
3
|
|
|
import pytest |
|
|
|
|
4
|
|
|
|
5
|
|
|
import log |
6
|
|
|
|
7
|
|
|
from . import demo, other |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
def describe_text(): |
11
|
|
|
|
12
|
|
|
def it_includes_the_caller_location(expect, caplog): |
13
|
|
|
demo.greet("caller") |
14
|
|
|
expect(caplog.text) == \ |
15
|
|
|
"demo.py 5 ERROR Hello, caller!\n" |
16
|
|
|
|
17
|
|
|
@pytest.mark.last |
18
|
|
|
def it_can_be_formatted_with_init(expect, caplog): |
19
|
|
|
log.init(format=log.helpers.DEFAULT_FORMAT, level=log.WARNING) |
20
|
|
|
demo.greet("format") |
21
|
|
|
expect(caplog.text) == "ERROR: tests.demo: Hello, format!\n" |
22
|
|
|
|
23
|
|
|
def it_can_include_exceptions(expect, caplog): |
24
|
|
|
try: |
25
|
|
|
print(1 / 0) |
26
|
|
|
except ZeroDivisionError: |
27
|
|
|
log.exception("exception") |
28
|
|
|
expect(caplog.text).contains('Traceback ') |
29
|
|
|
expect(caplog.text).contains('test_records.py", line 25, ') |
30
|
|
|
expect(caplog.text).contains('ZeroDivisionError') |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
def describe_silence(): |
34
|
|
|
|
35
|
|
|
def when_off(expect, caplog): |
36
|
|
|
log.silence('3rd-party') |
37
|
|
|
other.do_3rd_party_thing() |
38
|
|
|
expect(caplog.records) == [] |
39
|
|
|
|
40
|
|
|
def with_errors(expect, caplog): |
41
|
|
|
log.silence('3rd-party', allow_error=True) |
42
|
|
|
other.do_3rd_party_thing() |
43
|
|
|
expect(len(caplog.records)) == 1 |
44
|
|
|
|
45
|
|
|
def with_warnings(expect, caplog): |
46
|
|
|
log.silence('3rd-party', allow_warning=True) |
47
|
|
|
other.do_3rd_party_thing() |
48
|
|
|
expect(len(caplog.records)) == 2 |
49
|
|
|
|
50
|
|
|
def with_infos(expect, caplog): |
51
|
|
|
log.silence('3rd-party', allow_info=True) |
52
|
|
|
other.do_3rd_party_thing() |
53
|
|
|
expect(len(caplog.records)) == 3 |
54
|
|
|
|