question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
konoufocommented, Jun 7, 2018

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

0reactions
nicoddemuscommented, Dec 21, 2018

Fix will be available in pytest 4.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pytest fixtures: explicit, modular, scalable
fixtures have explicit names and are activated by declaring their use from test functions, modules, classes or whole projects. fixtures are implemented in...
Read more >
Pytest fixture for a class through self not as method argument
I wanted to call a fixture once per test class and re-use the value in test ... @pytest.mark.usefixtures("google") class TestGoogle: def ...
Read more >
pytest Documentation - Read the Docs
“How to mark test functions with attributes” for information on the pytest.mark mechanism. • “Fixtures reference” for providing a functional ...
Read more >
Working with custom markers — pytest documentation
Marking test functions and selecting them for a run¶. You can “mark” a test function with custom metadata like this: # content of...
Read more >
Effective Python Testing With Pytest
pytest is a feature-rich, plugin-based ecosystem for testing your Python code. If you haven't had the pleasure of using pytest yet, then you're ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found