Skip to content

pytest.fixture with a yield statement doesn't work as expected #343

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

Closed
pytestbot opened this issue Aug 16, 2013 · 2 comments
Closed

pytest.fixture with a yield statement doesn't work as expected #343

pytestbot opened this issue Aug 16, 2013 · 2 comments
Labels
type: bug problem that needs to be addressed

Comments

@pytestbot
Copy link
Contributor

Originally reported by: Emily Bache (BitBucket: emilybache, GitHub: emilybache)


I'm looking at the documentation:

https://pytest.readthedocs.org/en/latest/fixture.html#fixture-finalization-teardowns

and I can't get it to work with a yield statement in my fixture. Pytest is passing the generator function to my test case instead of the resource. Or maybe I've just misunderstood how this is supposed to work. Code is attached - commented out are two forms of the test fixture that seem to work fine. The code that is not commented out produces this kind of error:

$ python3 -m pytest
=================================== test session starts ====================================
platform darwin -- Python 3.3.2 -- pytest-2.3.5
collected 3 items

test_phonebook.py FFF

========================================= FAILURES =========================================
____________________________________ test_lookup_entry _____________________________________

phonebook = <generator object phonebook at 0x1013e3e60>

def test_lookup_entry(phonebook):
  phonebook.add("Bob", "123")

E AttributeError: 'generator' object has no attribute 'add'

test_phonebook.py:27: AttributeError

I expected the one with the yield statement to be interchangable with the one with using 'addfinalizer' and it isn't.


@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


I am not sure how you got to that readthedocs page, it's not advertised from pytest.org i hope. It's "developer" documentation and its features are only implemented on trunk. See http://pytest.org for the official documentation (pytest-2.3.5 doesn't have yield-syntax for fixtures).

@pytestbot
Copy link
Contributor Author

Original comment by Emily Bache (BitBucket: emilybache, GitHub: emilybache):


Ah that explains it! I look forward to the new pytest version then. I found the readthedocs from a DuckDuckGo search - should have been more alert to the "latest" in the url...

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

1 participant