Skip to content

Commit 71fb1bc

Browse files
author
Sylvain MARIE
committed
Fixed fixture 'self' not found issue when @fixture was used to decorate a class method not explicitly depending on request. Fixed #182
1 parent 672aacf commit 71fb1bc

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

pytest_cases/case_parametrizer_new.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ def name_changer(name, i):
467467

468468
# handle @pytest.mark.usefixtures by creating a wrapper where the fixture is added to the signature
469469
if add_required_fixtures:
470-
# create a wrapper with an explicit requirement for the fixtures
470+
# create a wrapper with an explicit requirement for the fixtures. TODO: maybe we should append and not prepend?
471471
case_fun = add_fixture_params(case_fun, add_required_fixtures)
472472
# remove the `usefixtures` mark: maybe we should leave it as it does no harm ?
473473
remove_pytest_mark(case_fun, "usefixtures")

pytest_cases/fixture_core1_unions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ def ignore_unused(fixture_func):
209209
# add request if needed
210210
func_needs_request = 'request' in old_sig.parameters
211211
if not func_needs_request:
212-
new_sig = add_signature_parameters(old_sig,
213-
first=Parameter('request', kind=Parameter.POSITIONAL_OR_KEYWORD))
212+
# Add it last so that `self` argument in class functions remains the first
213+
new_sig = add_signature_parameters(old_sig, last=Parameter('request', kind=Parameter.POSITIONAL_OR_KEYWORD))
214214
else:
215215
new_sig = old_sig
216216

pytest_cases/fixture_core2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,8 @@ def _decorate_fixture_plus(fixture_func,
459459
# add request if needed
460460
func_needs_request = 'request' in old_sig.parameters
461461
if not func_needs_request:
462-
new_sig = add_signature_parameters(new_sig, first=Parameter('request', kind=Parameter.POSITIONAL_OR_KEYWORD))
462+
# Add it last so that `self` argument in class functions remains the first
463+
new_sig = add_signature_parameters(new_sig, last=Parameter('request', kind=Parameter.POSITIONAL_OR_KEYWORD))
463464

464465
# --common routine used below. Fills kwargs with the appropriate names and values from fixture_params
465466
def _map_arguments(*_args, **_kwargs):
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
from pytest_cases import fixture
3+
4+
5+
class TestMethod:
6+
@pytest.fixture
7+
def pytest_fxt(self):
8+
return "Hello"
9+
10+
def test_with_pytest(self, pytest_fxt):
11+
# succeeds
12+
assert pytest_fxt == "Hello"
13+
14+
@fixture
15+
def cases_fxt(self):
16+
return "Hello"
17+
18+
def test_with_cases(self, cases_fxt):
19+
# raises an error with regards to 'self'
20+
assert cases_fxt == "Hello"
21+
22+
23+
def test_synthesis(module_results_dct):
24+
assert list(module_results_dct) == [
25+
'test_with_pytest',
26+
'test_with_cases'
27+
]

0 commit comments

Comments
 (0)