Skip to content

Commit

Permalink
Fixed fixture 'self' not found issue when @fixture was used to de…
Browse files Browse the repository at this point in the history
…corate a class method not explicitly depending on `request`. Fixed #182
  • Loading branch information
Sylvain MARIE committed Feb 19, 2021
1 parent 672aacf commit 71fb1bc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pytest_cases/case_parametrizer_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def name_changer(name, i):

# handle @pytest.mark.usefixtures by creating a wrapper where the fixture is added to the signature
if add_required_fixtures:
# create a wrapper with an explicit requirement for the fixtures
# create a wrapper with an explicit requirement for the fixtures. TODO: maybe we should append and not prepend?
case_fun = add_fixture_params(case_fun, add_required_fixtures)
# remove the `usefixtures` mark: maybe we should leave it as it does no harm ?
remove_pytest_mark(case_fun, "usefixtures")
Expand Down
4 changes: 2 additions & 2 deletions pytest_cases/fixture_core1_unions.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ def ignore_unused(fixture_func):
# add request if needed
func_needs_request = 'request' in old_sig.parameters
if not func_needs_request:
new_sig = add_signature_parameters(old_sig,
first=Parameter('request', kind=Parameter.POSITIONAL_OR_KEYWORD))
# Add it last so that `self` argument in class functions remains the first
new_sig = add_signature_parameters(old_sig, last=Parameter('request', kind=Parameter.POSITIONAL_OR_KEYWORD))
else:
new_sig = old_sig

Expand Down
3 changes: 2 additions & 1 deletion pytest_cases/fixture_core2.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,8 @@ def _decorate_fixture_plus(fixture_func,
# add request if needed
func_needs_request = 'request' in old_sig.parameters
if not func_needs_request:
new_sig = add_signature_parameters(new_sig, first=Parameter('request', kind=Parameter.POSITIONAL_OR_KEYWORD))
# Add it last so that `self` argument in class functions remains the first
new_sig = add_signature_parameters(new_sig, last=Parameter('request', kind=Parameter.POSITIONAL_OR_KEYWORD))

# --common routine used below. Fills kwargs with the appropriate names and values from fixture_params
def _map_arguments(*_args, **_kwargs):
Expand Down
27 changes: 27 additions & 0 deletions pytest_cases/tests/pytest_extension/issues/test_issue_182.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
from pytest_cases import fixture


class TestMethod:
@pytest.fixture
def pytest_fxt(self):
return "Hello"

def test_with_pytest(self, pytest_fxt):
# succeeds
assert pytest_fxt == "Hello"

@fixture
def cases_fxt(self):
return "Hello"

def test_with_cases(self, cases_fxt):
# raises an error with regards to 'self'
assert cases_fxt == "Hello"


def test_synthesis(module_results_dct):
assert list(module_results_dct) == [
'test_with_pytest',
'test_with_cases'
]

0 comments on commit 71fb1bc

Please sign in to comment.