-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathtest_insert_pytest_raises.py
More file actions
163 lines (138 loc) · 5.18 KB
/
test_insert_pytest_raises.py
File metadata and controls
163 lines (138 loc) · 5.18 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import os
import sys
import pytest
pytestmark = pytest.mark.skipif(sys.version_info < (3, 8), reason='requires Python 3.8+')
config = "pytest_plugins = ['devtools.pytest_plugin']"
# language=Python
default_test = """\
import re, pytest
def test_ok():
assert 1 + 2 == 3
def test_value_error(insert_pytest_raises):
with insert_pytest_raises():
raise ValueError("Such error")\
"""
def test_insert_pytest_raises(pytester_pretty):
os.environ.pop('CI', None)
pytester_pretty.makeconftest(config)
test_file = pytester_pretty.makepyfile(default_test)
result = pytester_pretty.runpytest()
result.assert_outcomes(passed=2)
assert test_file.read_text() == (
'import re, pytest\n'
'def test_ok():\n'
' assert 1 + 2 == 3\n'
'\n'
'def test_value_error(insert_pytest_raises):\n'
' # with insert_pytest_raises():\n'
" with pytest.raises(ValueError, match=re.escape('Such error')):\n"
' raise ValueError("Such error")'
)
def test_insert_pytest_raises_no_pretty(pytester):
os.environ.pop('CI', None)
pytester.makeconftest(config)
test_file = pytester.makepyfile(default_test)
result = pytester.runpytest('-p', 'no:pretty')
result.assert_outcomes(passed=2)
assert test_file.read_text() == (
'import re, pytest\n'
'def test_ok():\n'
' assert 1 + 2 == 3\n'
'\n'
'def test_value_error(insert_pytest_raises):\n'
' # with insert_pytest_raises():\n'
" with pytest.raises(ValueError, match=re.escape('Such error')):\n"
' raise ValueError("Such error")'
)
def test_insert_pytest_raises_print(pytester_pretty, capsys):
os.environ.pop('CI', None)
pytester_pretty.makeconftest(config)
test_file = pytester_pretty.makepyfile(default_test)
# assert r == 0
result = pytester_pretty.runpytest('--insert-assert-print')
result.assert_outcomes(passed=2)
assert test_file.read_text() == default_test
captured = capsys.readouterr()
assert 'test_insert_pytest_raises_print.py - 6:' in captured.out
assert 'Printed 1 insert_pytest_raises() call in 1 file\n' in captured.out
def test_insert_pytest_raises_fail(pytester_pretty):
os.environ.pop('CI', None)
pytester_pretty.makeconftest(config)
test_file = pytester_pretty.makepyfile(default_test)
# assert r == 0
result = pytester_pretty.runpytest()
assert result.parseoutcomes() == {'passed': 2, 'warning': 1, 'insert': 1}
assert test_file.read_text() != default_test
def test_insert_pytest_raises_repeat(pytester_pretty, capsys):
os.environ.pop('CI', None)
pytester_pretty.makeconftest(config)
test_file = pytester_pretty.makepyfile(
"""\
import pytest, re
@pytest.mark.parametrize('x', [1, 2, 3])
def test_raise_keyerror(x, insert_pytest_raises):
with insert_pytest_raises():
raise KeyError(x)\
"""
)
result = pytester_pretty.runpytest()
result.assert_outcomes(passed=3)
assert test_file.read_text() == (
'import pytest, re\n'
'\n'
"@pytest.mark.parametrize('x', [1, 2, 3])\n"
'def test_raise_keyerror(x, insert_pytest_raises):\n'
' # with insert_pytest_raises():\n'
" with pytest.raises(KeyError, match=re.escape('1')):\n"
' raise KeyError(x)'
)
captured = capsys.readouterr()
assert '2 inserts skipped because an assert statement on that line had already be inserted!\n' in captured.out
def test_insert_pytest_raises_frame_not_found(pytester_pretty, capsys):
os.environ.pop('CI', None)
pytester_pretty.makeconftest(config)
pytester_pretty.makepyfile(
"""\
import pytest, re
def test_raise_keyerror(insert_pytest_raises):
eval('insert_pytest_raises().__enter__()')
"""
)
result = pytester_pretty.runpytest()
result.assert_outcomes(failed=1)
captured = capsys.readouterr()
assert (
'RuntimeError: insert_pytest_raises() was unable to find the frame from which it was called\n' in captured.out
)
def test_insert_pytest_raises_called_outside_with(pytester_pretty, capsys):
os.environ.pop('CI', None)
pytester_pretty.makeconftest(config)
pytester_pretty.makepyfile(
"""\
import pytest, re
def test_raise_keyerror(insert_pytest_raises):
assert insert_pytest_raises().__enter__() == 1
"""
)
result = pytester_pretty.runpytest()
result.assert_outcomes(failed=1)
captured = capsys.readouterr()
assert "RuntimeError: insert_pytest_raises() was called outside of a 'with' statement\n" in captured.out
def test_insert_pytest_raises_called_with_other_with_statements(pytester_pretty, capsys):
os.environ.pop('CI', None)
pytester_pretty.makeconftest(config)
pytester_pretty.makepyfile(
"""\
import pytest, re, contextlib
def test_raise_keyerror(insert_pytest_raises):
with contextlib.nullcontext(), insert_pytest_raises():
raise KeyError(1)
"""
)
result = pytester_pretty.runpytest()
result.assert_outcomes(failed=1)
captured = capsys.readouterr()
assert (
'RuntimeError: insert_pytest_raises() was called alongside other statements, this is not supported\n'
in captured.out
)