forked from samuelcolvin/python-devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_insert_assert.py
More file actions
169 lines (143 loc) · 4.72 KB
/
test_insert_assert.py
File metadata and controls
169 lines (143 loc) · 4.72 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
164
165
166
167
168
169
import os
import sys
import pytest
from devtools.pytest_plugin import load_black
pytestmark = pytest.mark.skipif(sys.version_info < (3, 8), reason='requires Python 3.8+')
config = "pytest_plugins = ['devtools.pytest_plugin']"
# language=Python
default_test = """\
def test_ok():
assert 1 + 2 == 3
def test_string_assert(insert_assert):
thing = 'foobar'
insert_assert(thing)\
"""
def test_insert_assert(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)
# print(result.outlines)
assert test_file.read_text() == (
'def test_ok():\n'
' assert 1 + 2 == 3\n'
'\n'
'def test_string_assert(insert_assert):\n'
" thing = 'foobar'\n"
' # insert_assert(thing)\n'
' assert thing == "foobar"'
)
def test_insert_assert_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() == (
'def test_ok():\n'
' assert 1 + 2 == 3\n'
'\n'
'def test_string_assert(insert_assert):\n'
" thing = 'foobar'\n"
' # insert_assert(thing)\n'
' assert thing == "foobar"'
)
def test_insert_assert_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_assert_print.py - 6:' in captured.out
assert 'Printed 1 insert_assert() call in 1 file\n' in captured.out
def test_insert_assert_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_deep(pytester_pretty):
os.environ.pop('CI', None)
pytester_pretty.makeconftest(config)
# language=Python
test_file = pytester_pretty.makepyfile(
"""
def test_deep(insert_assert):
insert_assert([{'a': i, 'b': 2 * 2} for i in range(3)])
"""
)
result = pytester_pretty.runpytest()
result.assert_outcomes(passed=1)
assert test_file.read_text() == (
'def test_deep(insert_assert):\n'
" # insert_assert([{'a': i, 'b': 2 * 2} for i in range(3)])\n"
' assert [{"a": i, "b": 2 * 2} for i in range(3)] == [\n'
' {"a": 0, "b": 4},\n'
' {"a": 1, "b": 4},\n'
' {"a": 2, "b": 4},\n'
' ]'
)
def test_enum(pytester_pretty, capsys):
os.environ.pop('CI', None)
pytester_pretty.makeconftest(config)
# language=Python
pytester_pretty.makepyfile(
"""
from enum import Enum
class Foo(Enum):
A = 1
B = 2
def test_deep(insert_assert):
x = Foo.A
insert_assert(x)
"""
)
result = pytester_pretty.runpytest('--insert-assert-print')
result.assert_outcomes(passed=1)
captured = capsys.readouterr()
assert ' assert x == Foo.A\n' in captured.out
def test_insert_assert_black(tmp_path):
old_wd = os.getcwd()
try:
os.chdir(tmp_path)
(tmp_path / 'pyproject.toml').write_text(
"""\
[tool.black]
target-version = ["py39"]
skip-string-normalization = true"""
)
load_black.cache_clear()
finally:
os.chdir(old_wd)
f = load_black()
# no string normalization
assert f("'foobar'") == "'foobar'\n"
def test_insert_assert_repeat(pytester_pretty, capsys):
os.environ.pop('CI', None)
pytester_pretty.makeconftest(config)
test_file = pytester_pretty.makepyfile(
"""\
import pytest
@pytest.mark.parametrize('x', [1, 2, 3])
def test_string_assert(x, insert_assert):
insert_assert(x)\
"""
)
result = pytester_pretty.runpytest()
result.assert_outcomes(passed=3)
assert test_file.read_text() == (
'import pytest\n'
'\n'
"@pytest.mark.parametrize('x', [1, 2, 3])\n"
'def test_string_assert(x, insert_assert):\n'
' # insert_assert(x)\n'
' assert x == 1'
)
captured = capsys.readouterr()
assert '2 insert skipped because an assert statement on that line had already be inserted!\n' in captured.out