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.

Calling fixtures directly error not working with fixtures that yield data

See original GitHub issue

Since 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:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
RonnyPfannschmidtcommented, Jan 2, 2019

@asottile thanks for validating the current code against this

0reactions
cript0nautacommented, Jan 2, 2019

Oh you’re right, it’s fixed on the features branch. Thanks!

Read more comments on GitHub >

github_iconTop 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 >

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