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 is being applied more than once to the same function" in 3.6.0, not in 3.2.1 #3518

Closed
simonw opened this issue May 28, 2018 · 4 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@simonw
Copy link

simonw commented May 28, 2018

I'm getting the following test failure in my test suite running against pytest 3.6.0:

(venv) datasette $ pip install pytest==3.6.0
...
Successfully installed pytest-3.6.0
(venv) datasette $ pytest
=============================================== test session starts ================================================
platform darwin -- Python 3.6.5, pytest-3.6.0, py-1.5.3, pluggy-0.6.0
rootdir: /Users/simonw/Dropbox/Development/datasette, inifile:
collected 140 items / 1 errors                                                                                     

====================================================== ERRORS ======================================================
_______________________________________ ERROR collecting tests/test_html.py ________________________________________
tests/test_html.py:7: in <module>
    pytest.fixture(scope='module')(app_client)
venv/lib/python3.6/site-packages/_pytest/fixtures.py:857: in __call__
    "fixture is being applied more than once to the same function")
E   ValueError: fixture is being applied more than once to the same function
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================= 1 error in 1.22 seconds ==============================================

The exact same test suite runs without any errors under pytest 3.2.1:

(venv) datasette $ pip install pytest==3.2.1
Collecting pytest==3.2.1
...
Successfully installed pytest-3.2.1
(venv) datasette $ pytest
=============================================== test session starts ================================================
platform darwin -- Python 3.6.5, pytest-3.2.1, py-1.5.3, pluggy-0.4.0
rootdir: /Users/simonw/Dropbox/Development/datasette, inifile:
collected 175 items                                                                                                 

tests/test_api.py .............................................................................
tests/test_docs.py ............
tests/test_html.py ...................................
tests/test_inspect.py ..
tests/test_utils.py .................................................

=========================================== 175 passed in 21.16 seconds ============================================

This is the exact checkout of the code I ran the above tests against: https://github.com/simonw/datasette/tree/76d11eb768e2f05f593c4d37a25280c0fcdf8fd6

This appears to be a regression in 3.6.0.

The underlying issue is that in my test_html.py I have the following:

https://github.com/simonw/datasette/blob/76d11eb768e2f05f593c4d37a25280c0fcdf8fd6/tests/test_html.py#L1-L7

from .fixtures import app_client
import pytest

pytest.fixture(scope='module')(app_client)

And in my test_api.py file I have this:

https://github.com/simonw/datasette/blob/76d11eb768e2f05f593c4d37a25280c0fcdf8fd6/tests/test_api.py#L1-L14

from .fixtures import (
    app_client,
    app_client_shorter_time_limit,
    app_client_returend_rows_matches_page_size,
)
import pytest

pytest.fixture(scope='module')(app_client)
pytest.fixture(scope='module')(app_client_shorter_time_limit)
pytest.fixture(scope='module')(app_client_returend_rows_matches_page_size)

The pytest.fixture(scope='module')(app_client) lines in the two modules seem to be conflicting with each other in 3.6.0 even though they worked fine together in 3.2.1

@pytestbot
Copy link
Contributor

GitMate.io thinks possibly related issues are #616 ((2.6.3) Conftest is ran, but fixtures not brought in.), #2782 (PyTest won't inject fixtures into decorated functions in Python 2.7), #1627 (Drop Python 3.0/3.1/3.2/2.6?), #1909 (Monkeypatch docs lost method reference between 3.0.1 and 3.0.2), and #1875 (Cannot create two fixtures based on the same function).

@nicoddemus
Copy link
Member

Hi @simonw,

That's by design, we added this check intentionally in #2334, here's the relevant changelog entry:

Now when @pytest.fixture is applied more than once to the same function a ValueError is raised. This buggy behavior would cause surprising problems and if was working for a test suite it was mostly by accident.

The solution is to not apply the @fixture decorator twice to the function.

I'm closing this for now, feel free to follow up with any other questions you have.

@pytestbot pytestbot added the type: bug problem that needs to be addressed label May 28, 2018
@RonnyPfannschmidt RonnyPfannschmidt added type: question general question, might be closed after 2 weeks of inactivity and removed type: bug problem that needs to be addressed labels May 28, 2018
@simonw
Copy link
Author

simonw commented May 31, 2018

Thanks - now that I understand the underlying issue I've fixed it in my code by moving my calls to @pytest.fixture to be decorators in my fixtures module instead: simonw/datasette@9697717

@RonnyPfannschmidt
Copy link
Member

@simonw also please note that its problematic to import fixtures, in particular if they have a scope as pytest detects each import location as distinct fixture

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

4 participants