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

Test collection fails when using pytest.mark.usefixtures and a test function is a classmethod #3537

Closed
konoufo opened this issue Jun 5, 2018 · 7 comments
Labels
type: bug problem that needs to be addressed

Comments

@konoufo
Copy link

konoufo commented Jun 5, 2018

This minimal example using django TestCase class causes an error on collection:

from django.test import TestCase
import pytest


@pytest.fixture
def harry_potter(request):
    return 1


@pytest.mark.usefixtures('harry_potter')
class FooTestCase(TestCase):
    @classmethod
    def test_foo(cls):
        assert 1

The output of running pytest is as such:

platform linux -- Python 3.6.5, pytest-3.6.1, py-1.5.3, pluggy-0.6.0
Django settings: project.settings.development (from ini file)
rootdir: /users/username/project/src, inifile: pytest.ini
plugins: django-3.2.1, celery-4.1.1
collected 11 items / 1 errors                                                                                                                                                                                                               

================================================================================================================== ERRORS ===================================================================================================================
_______________________________________________________________________________________________ ERROR collecting tests/fooTestCase.py ________________________________________________________________________________________________
../../../../../.virtualenvs/myvenv/lib/python3.6/site-packages/_pytest/runner.py:198: in __init__
    self.result = func()
../../../../../.virtualenvs/myvenv/lib/python3.6/site-packages/_pytest/runner.py:419: in <lambda>
    call = CallInfo(lambda: list(collector.collect()), "collect")
../../../../../.virtualenvs/myvenv/lib/python3.6/site-packages/_pytest/unittest.py:57: in collect
    transfer_markers(funcobj, cls, module)
../../../../../.virtualenvs/myvenv/lib/python3.6/site-packages/_pytest/mark/structures.py:265: in transfer_markers
    store_legacy_markinfo(funcobj, mark)
../../../../../.virtualenvs/myvenv/lib/python3.6/site-packages/_pytest/mark/structures.py:248: in store_legacy_markinfo
    setattr(func, mark.name, holder)
E   AttributeError: 'method' object has no attribute 'usefixtures'

OS: Fedora 27
(See log output for everything else)
If no test function is a classmethod it works alright. I don't know if this is a feature. Nothing seems to imply that.
Thank you!

@pytestbot
Copy link
Contributor

GitMate.io thinks possibly related issues are #2114 (Could pytest --last-failed skip test collection?), #3308 (Question: Can someone explain the below uses of @pytest.mark.usefixtures), #378 (@pytest.mark.usefixtures take no effects on fixtures), #1045 (pytest-warnings fails tests since 2.8), and #342 (pytest is identifying a function as a test case that shouldn't be).

@pytestbot pytestbot added the type: bug problem that needs to be addressed label Jun 5, 2018
@RonnyPfannschmidt
Copy link
Member

this is a bug

its quite unexpected that someone uses actual class-methods for tests

@konoufo
Copy link
Author

konoufo commented Jun 6, 2018

Maybe there are things someone wouldn't want to expose to some instance attributes. I could imagine this 2 celery applications with one assigned to a class attribute and the other to an instance attribute. So does it warrant a PR ? Do you think it could be a good first contribution ?

@RonnyPfannschmidt
Copy link
Member

it warrants a pr after all its a bug ^^
it could be a good first contribution - there is only the test as per proposal needed and there is 1 code path needed to fix (to fetch the right function at the right time)

@konoufo
Copy link
Author

konoufo commented Jun 7, 2018

Yeah alright ^^. I'll submit a PR soon. Thanks.

@avihaie
Copy link

avihaie commented Aug 1, 2018

Encountered this as well with pytest-3.6.1 , any update on this issue?

@nicoddemus
Copy link
Member

Fix will be available in pytest 4.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

5 participants