Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest fixture of fixture, not found

Tags:

python

pytest

Based on this stackoverflow: pytest fixture of fixtures

I have the following code in the same file:

@pytest.fixture
def form_data():
    return { ... }

@pytest.fixture
def example_event(form_data):
    return {... 'data': form_data, ... }

But when I run pytest, it complains that fixture 'form_data' not found

I am new to pytest so I am not even sure if this is possible?

like image 528
James Lin Avatar asked Nov 20 '17 22:11

James Lin


People also ask

What is pytest fixture ()?

Pytest fixtures are functions that can be used to manage our apps states and dependencies. Most importantly, they can provide data for testing and a wide range of value types when explicitly called by our testing software. You can use the mock data that fixtures create across multiple tests.

Where do I put pytest fixtures?

They don't require importing, but if you defined them in a test_*. py file, they'll only be available in that file. You can however put them in a (project- or subfolder-wide) conftest.py to use them in multiple files. pytest-internal fixtures are simply defined in a core plugin, and thus available everywhere.

Are pytest fixtures cached?

Pytest only caches one instance of a fixture at a time, which means that when using a parametrized fixture, pytest may invoke a fixture more than once in the given scope.


1 Answers

Yes, it is possible.

If you have the test and all the fixtures in 1 file: test.py

import pytest  @pytest.fixture def foo():     return "foo"  @pytest.fixture def bar(foo):     return foo, "bar"  def test_foo_bar(bar):     expected = ("foo", "bar")     assert bar == expected 

and run pytest test.py then Success!!!

======================================= test session starts ======================================== platform darwin -- Python 3.6.8, pytest-4.3.0 collected 1 item                                                                                     test.py .                                                                                    [100%]  ===================================== 1 passed in 0.02 seconds =====================================  

But if you put the fixtures in a different file: test_foo_bar.py

from test import bar  def test_foo_bar(bar):     expected = ("foo", "bar")     assert bar == expected 

and run pytest test_foo_bar.py expecting (like I did) that importing only the bar fixture is enough since on importing it would already have executed the foo fixture then you get the error you are getting.

======================================= test session starts ======================================== platform darwin -- Python 3.6.8, pytest-4.3.0 collected 1 item                                                                                     test2.py E                                                                                   [100%]  ============================================== ERRORS ============================================== __________________________________ ERROR at setup of test_foo_bar __________________________________ file .../test_foo_bar.py, line 3   def test_foo_bar(bar): .../test.py, line 7   @pytest.fixture   def bar(foo): E       fixture 'foo' not found >       available fixtures: TIMEOUT, bar, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, cov, doctest_namespace, monkeypatch, no_cover, once_without_docker, pytestconfig, record_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory >       use 'pytest --fixtures [testpath]' for help on them.  .../test.py:7 ===================================== 1 error in 0.03 seconds ======================================  

To fix this also import the foo fixture in the test_foo_bar.py module.

like image 134
m. bron Avatar answered Oct 12 '22 13:10

m. bron