Replies: 4 comments
-
As a workaround, I patched the flask.testing module in the app fixture function as follows:
It looks like _werkzeug_version is used only by the _get_werkzeug_version() function in this Flask version, so this shouldn't affect any other code, but I'm still a bit wary of doing this. |
Beta Was this translation helpful? Give feedback.
-
Thanks, I will have a look! |
Beta Was this translation helpful? Give feedback.
-
Ok, the problem is that This kind of problems can usually be fixed by mapping the relevant files into the fake filesystem, though in this case it is a bit ugly: import importlib
...
from pyfakefs.fake_filesystem_unittest import Pause
@pytest.fixture(scope="function")
def app_fixture(fs) -> YieldFixture[Flask]:
with Pause(fs):
# need to pause the patching here as this accesses the real fs
metadata = [f for f in importlib.metadata.distribution("Werkzeug").files if "METADATA" in str(f)][0]
metadata_path = metadata.locate()
fs.add_real_file(metadata_path)
with create_app() as _app:
yield _app Maybe a convenience function to map the metadata of a package into the fake fs would make sense. At least there shall be a recipe for resolving this in the troubleshooting section. |
Beta Was this translation helpful? Give feedback.
-
I added a convenience method (currently in main branch, not yet released), that makes this a bit easier: @pytest.fixture(scope="function")
def app_fixture(fs) -> YieldFixture[Flask]:
fs.add_package_metadata("Werkzeug")
with create_app() as _app:
yield _app |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
When the fs fixture is used as input into a fixture yielding a Flask test client, using the test client fixture causes the test to error with the error message "importlib.metadata.PackageNotFoundError: No package metadata was found for werkzeug"
Test stack trace:
How To Reproduce
Import these packages: Flask 3.1.0, pyfakefs 5.8.0, pytest 8.3.5
Create the following files:
app.py
local_types.py
conftest.py
test_app.py
Run pytest and you will see the error. If you remove fs from the fixture parameters, the tests will pass normally.
Your environment
macOS-15.3.2-x86_64-i386-64bit
Python 3.10.5 (main, Apr 1 2025, 16:28:06) [Clang 16.0.0 (clang-1600.0.26.6)]
pyfakefs 5.8.0
pytest 8.3.5
Beta Was this translation helpful? Give feedback.
All reactions