Skip to content

Commit c37b8b0

Browse files
Sup3rGeosseliverstov
authored andcommitted
use index if name is not available for finalizer (fixes #282 fixes #278 via #284)
1 parent 6912b3c commit c37b8b0

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

allure-pytest/src/listener.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ def pytest_fixture_setup(self, fixturedef, request):
136136

137137
finalizers = getattr(fixturedef, '_finalizers', [])
138138
for index, finalizer in enumerate(finalizers):
139-
name = '{fixture}::{finalizer}'.format(fixture=fixturedef.argname, finalizer=finalizer.__name__)
139+
name = '{fixture}::{finalizer}'.format(fixture=fixturedef.argname,
140+
finalizer=getattr(finalizer, "__name__", index))
140141
finalizers[index] = allure_commons.fixture(finalizer, parent_uuid=container_uuid, name=name)
141142

142143
@pytest.hookimpl(hookwrapper=True)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def fixture_one():
6+
yield
7+
8+
9+
def test_yield_fixture(fixture_one):
10+
"""
11+
>>> allure_report = getfixture('allure_report')
12+
>>> assert_that(allure_report,
13+
... has_test_case('test_yield_fixture',
14+
... with_status('passed')
15+
... )
16+
... )
17+
"""
18+
pass

allure-python-commons/src/utils.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,53 @@
1212
import threading
1313
import traceback
1414
import collections
15+
from functools import partial
16+
17+
18+
def getargspec(func):
19+
"""
20+
Used because getargspec for python 2.7 does not accept functools.partial
21+
which is the type for pytest fixtures.
22+
23+
getargspec excerpted from:
24+
25+
sphinx.util.inspect
26+
~~~~~~~~~~~~~~~~~~~
27+
Helpers for inspecting Python modules.
28+
:copyright: Copyright 2007-2018 by the Sphinx team, see AUTHORS.
29+
:license: BSD, see LICENSE for details.
30+
31+
Like inspect.getargspec but supports functools.partial as well.
32+
"""
33+
# type: (Any) -> Any
34+
if inspect.ismethod(func):
35+
func = func.__func__
36+
parts = 0, () # type: Tuple[int, Tuple[unicode, ...]]
37+
if type(func) is partial:
38+
keywords = func.keywords
39+
if keywords is None:
40+
keywords = {}
41+
parts = len(func.args), keywords.keys()
42+
func = func.func
43+
if not inspect.isfunction(func):
44+
raise TypeError('%r is not a Python function' % func)
45+
args, varargs, varkw = inspect.getargs(func.__code__)
46+
func_defaults = func.__defaults__
47+
if func_defaults is None:
48+
func_defaults = []
49+
else:
50+
func_defaults = list(func_defaults)
51+
if parts[0]:
52+
args = args[parts[0]:]
53+
if parts[1]:
54+
for arg in parts[1]:
55+
i = args.index(arg) - len(args) # type: ignore
56+
del args[i]
57+
try:
58+
del func_defaults[i]
59+
except IndexError:
60+
pass
61+
return inspect.ArgSpec(args, varargs, varkw, func_defaults) # type: ignore
1562

1663

1764
if six.PY3:
@@ -252,7 +299,7 @@ def func_parameters(func, *args, **kwargs):
252299
253300
"""
254301
parameters = {}
255-
arg_spec = inspect.getargspec(func) if six.PY2 else inspect.getfullargspec(func)
302+
arg_spec = getargspec(func) if six.PY2 else inspect.getfullargspec(func)
256303
arg_order = list(arg_spec.args)
257304
args_dict = dict(zip(arg_spec.args, args))
258305

0 commit comments

Comments
 (0)