Calling fixtures directly error not working with fixtures that yield data
See original GitHub issueSince the changes in #4561, doing something like this will throw an error (this is the expected behavior):
import pytest
@pytest.fixture
def test():
return 1
def test_succeed(test):
assert 1 == test
test()
==================================== ERRORS ====================================
_______________________ ERROR collecting sscce_pytest.py _______________________
sscce_pytest.py:10: in <module>
test()
E RemovedInPytest4Warning: Fixture "test" called directly. Fixtures are not meant to be called directly, are created automatically when test functions request them as parameters. See https://docs.pytest.org/en/latest/fixture.html for more information.
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.23 seconds ============================
If I use a fixture that yields a value instead of returning one (detailed in https://docs.pytest.org/en/latest/fixture.html#fixture-finalization-executing-teardown-code) like this:
import pytest
@pytest.fixture
def test():
yield 1
def test_succeed(test):
assert 1 == test
test()
the code will succeed when it should throw an error like in the previous example.
I tested it with pytest 4.0.2 running inside a virtualenv on a NixOS GNU/Linux distribution.
Here’s a quick checklist in what to include:
- Include a detailed description of the bug or suggestion
-
pip list
of the virtual environment you are using - pytest and operating system versions
- Minimal example if possible
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
python - get value and avoid error "Fixture 'X' called directly"
I have an issue with fixtures used in an unittest.TestCase, how do I get the value returned from the fixture and not a...
Read more >How to use fixtures — pytest documentation
Parametrizing fixtures Fixture functions can be parametrized in which case they will be called multiple times, each time executing the set of dependent...
Read more >Five Advanced Pytest Fixture Patterns - Inspired Python
A close look at five pytest fixture patterns that will teach you just how much you can accomplish with pytest's fixtures decorator.
Read more >A comprehensive guide to pytest. - Level Up Coding
Discover the ins and outs of fixtures and mocking with the ultimate ... While I do not have a panacea, if you're working...
Read more >How to Use Pytest Fixtures - Finxter
Pytest fixtures are functions that you can use to initialise your test environment. They can provide consistent test data or set up the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@asottile thanks for validating the current code against this
Oh you’re right, it’s fixed on the
features
branch. Thanks!