Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixture breaks when defined in class method #182

Closed
mcstarioni opened this issue Feb 9, 2021 · 3 comments
Closed

Fixture breaks when defined in class method #182

mcstarioni opened this issue Feb 9, 2021 · 3 comments

Comments

@mcstarioni
Copy link

Using pytest_cases's fixture in a test which is a class method leads to the following error: fixture 'self' not found,. While using pytest.fixture doesn't result in error.
Minimal code to reproduce:

import pytest
from pytest_cases import fixture


class TestMethod:
    @pytest.fixture
    def pytest_fxt(self):
        yield "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"

Versions: pytest: '6.2.1', pytest_cases: '3.2.0'

@mcstarioni mcstarioni changed the title Fixture breaks when used in class method tests Fixture breaks when defined in class method tests Feb 9, 2021
@mcstarioni mcstarioni changed the title Fixture breaks when defined in class method tests Fixture breaks when defined in class method Feb 9, 2021
@smarie
Copy link
Owner

smarie commented Feb 19, 2021

Thanks @mcstarioni for reporting this issue, sorry for not having seen the notification :(
I'll have a look

@smarie
Copy link
Owner

smarie commented Feb 19, 2021

Indeed. The reason seems to be that I was adding a request dependency as first argument as it should be last

TestMethod.pytest_fxt
Out[2]: <function pytest_cases.tests.pytest_extension.issues.test_issue_182.TestMethod.pytest_fxt(self)>
TestMethod.cases_fxt
Out[3]: <function pytest_cases.tests.pytest_extension.issues.test_issue_182.TestMethod.cases_fxt(request, self)>

This additional argument is used when this fixture is used in a union: pytest will always try to call it even if it is not used in this branch of the union, so I create a wrapper inspection the request to detect if it should really be called or not.

I'll fix this in a minute by appending the argument instead of prepending it.

@smarie smarie closed this as completed in 71fb1bc Feb 19, 2021
@smarie
Copy link
Owner

smarie commented Feb 19, 2021

Fixed in version 3.2.1 that should be available in a few minutes when travis has completed. Let me know !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants