-
Notifications
You must be signed in to change notification settings - Fork 16
/
pytest_deadfixtures.py
234 lines (182 loc) · 6.77 KB
/
pytest_deadfixtures.py
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""
Some functions are basically copy n' paste version of code already in pytest.
Precisely the get_fixtures, get_used_fixturesdefs and write_docstring funtions.
"""
from collections import namedtuple
from itertools import combinations
from textwrap import dedent
import _pytest.config
import py
from _pytest.compat import getlocation
DUPLICATE_FIXTURES_HEADLINE = "\n\nYou may have some duplicate fixtures:"
UNUSED_FIXTURES_FOUND_HEADLINE = (
"Hey there, I believe the following fixture(s) are not being used:"
)
UNUSED_FIXTURES_NOT_FOUND_HEADLINE = "Cool, every declared fixture is being used."
EXIT_CODE_ERROR = 11
EXIT_CODE_SUCCESS = 0
AvailableFixture = namedtuple("AvailableFixture", "relpath, argname, fixturedef")
CachedFixture = namedtuple("CachedFixture", "fixturedef, relpath, result")
def pytest_addoption(parser):
group = parser.getgroup("deadfixtures")
group.addoption(
"--dead-fixtures",
action="store_true",
dest="deadfixtures",
default=False,
help="Show fixtures not being used",
)
group.addoption(
"--dup-fixtures",
action="store_true",
dest="showrepeated",
default=False,
help="Show duplicated fixtures",
)
def pytest_cmdline_main(config):
if config.option.deadfixtures:
config.option.show_fixture_doc = config.option.verbose
config.option.verbose = -1
if _show_dead_fixtures(config):
return EXIT_CODE_ERROR
return EXIT_CODE_SUCCESS
def _show_dead_fixtures(config):
from _pytest.main import wrap_session
return wrap_session(config, show_dead_fixtures)
def get_best_relpath(func, curdir):
loc = getlocation(func, curdir)
return curdir.bestrelpath(loc)
def get_fixtures(session):
available = []
seen = set()
fm = session._fixturemanager
curdir = py.path.local()
for argname, fixturedefs in fm._arg2fixturedefs.items():
assert fixturedefs is not None
if not fixturedefs:
continue
for fixturedef in fixturedefs:
loc = getlocation(fixturedef.func, curdir)
if (fixturedef.argname, loc) in seen:
continue
seen.add((fixturedef.argname, loc))
module = fixturedef.func.__module__
if (
not module.startswith("_pytest.")
and not module.startswith("pytest_")
and not ("site-packages" in loc)
and not ("dist-packages" in loc)
and not ("<string>" in loc)
):
available.append(
AvailableFixture(
curdir.bestrelpath(loc), fixturedef.argname, fixturedef
)
)
available.sort(key=lambda a: a.relpath)
return available
def get_used_fixturesdefs(session):
fixturesdefs = []
for test_function in session.items:
try:
info = test_function._fixtureinfo
except AttributeError:
# doctests items have no _fixtureinfo attribute
continue
if not info.name2fixturedefs:
# this test item does not use any fixtures
continue
for _, fixturedefs in sorted(info.name2fixturedefs.items()):
if fixturedefs is None:
continue
fixturesdefs.append(fixturedefs[-1])
return fixturesdefs
def get_parametrized_fixtures(session, available_fixtures):
params_values = []
for test_function in session.items:
try:
for _, v in test_function.callspec.params.items():
params_values.append(v)
except AttributeError:
continue
return [
available.fixturedef
for available in filter(
lambda x: x.fixturedef.argname in params_values, available_fixtures
)
]
def write_docstring(tw, doc):
INDENT = " "
doc = doc.rstrip()
if "\n" in doc:
firstline, rest = doc.split("\n", 1)
else:
firstline, rest = doc, ""
if firstline.strip():
tw.line(INDENT + firstline.strip())
if rest:
for line in dedent(rest).split("\n"):
tw.write(INDENT + line + "\n")
def write_fixtures(tw, fixtures, write_docs):
for fixture in fixtures:
tplt = "Fixture name: {}, location: {}"
tw.line(tplt.format(fixture.argname, fixture.relpath))
doc = fixture.fixturedef.func.__doc__ or ""
if write_docs and doc:
write_docstring(tw, doc)
cached_fixtures = []
def pytest_fixture_post_finalizer(fixturedef):
if getattr(fixturedef, "cached_result", None):
curdir = py.path.local()
loc = getlocation(fixturedef.func, curdir)
cached_fixtures.append(
CachedFixture(
fixturedef, curdir.bestrelpath(loc), fixturedef.cached_result[0]
)
)
def same_fixture(one, two):
def result_same_type(a, b):
return isinstance(a.result, type(b.result))
def same_result(a, b):
if not a.result or not b.result:
return False
if hasattr(a.result, "__dict__") or hasattr(b.result, "__dict__"):
return a.result.__dict__ == b.result.__dict__
return a.result == b.result
def same_loc(a, b):
return a.relpath == b.relpath
return result_same_type(one, two) and same_result(one, two) and not same_loc(one, two)
def pytest_sessionfinish(session, exitstatus):
if exitstatus or not session.config.getvalue("showrepeated"):
return exitstatus
tw = _pytest.config.create_terminal_writer(session.config)
duplicated_fixtures = []
for a, b in combinations(cached_fixtures, 2):
if same_fixture(a, b):
duplicated_fixtures.append((a, b))
if duplicated_fixtures:
tw.line(DUPLICATE_FIXTURES_HEADLINE, red=True)
msg = "Fixture name: {}, location: {}"
for a, b in duplicated_fixtures:
tw.line(msg.format(a.fixturedef.argname, a.relpath))
tw.line(msg.format(b.fixturedef.argname, b.relpath))
def show_dead_fixtures(config, session):
session.perform_collect()
tw = _pytest.config.create_terminal_writer(config)
show_fixture_doc = config.getvalue("show_fixture_doc")
used_fixtures = get_used_fixturesdefs(session)
available_fixtures = get_fixtures(session)
param_fixtures = get_parametrized_fixtures(session, available_fixtures)
unused_fixtures = [
fixture
for fixture in available_fixtures
if fixture.fixturedef not in used_fixtures
and fixture.fixturedef not in param_fixtures
]
tw.line()
if unused_fixtures:
tw.line(UNUSED_FIXTURES_FOUND_HEADLINE, red=True)
write_fixtures(tw, unused_fixtures, show_fixture_doc)
else:
tw.line(UNUSED_FIXTURES_NOT_FOUND_HEADLINE, green=True)
return unused_fixtures