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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With