Skip to content

Commit

Permalink
Use pytest.mark.usefixtures in code snippet
Browse files Browse the repository at this point in the history
- avoids linter warning about unused argument
- closes #834
  • Loading branch information
mrbean-bremen committed Jun 11, 2023
1 parent 6b2949e commit 9ec929b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 8 additions & 2 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ fake filesystem via the argument ``target_path``.
# make the file accessible in the fake file system
self.fs.add_real_directory(self.fixture_path)
def test_using_fixture1(self):
def test_using_fixture(self):
with open(os.path.join(self.fixture_path, "fixture1.txt")) as f:
# file contents are copied to the fake file system
# only at this point
Expand All @@ -751,10 +751,16 @@ You can do the same using ``pytest`` by using a fixture for test setup:
yield fs
def test_using_fixture1(my_fs):
@pytest.mark.usefixtures("my_fs")
def test_using_fixture():
with open(os.path.join(fixture_path, "fixture1.txt")) as f:
contents = f.read()
.. note::
If you are not using the fixture directly in the test, you can use
``@pytest.mark.usefixtures`` instead of passing the fixture as an argument.
This avoids warnings about unused arguments from linters.

When using ``pytest`` another option is to load the contents of the real file
in a fixture and pass this fixture to the test function **before** passing
the ``fs`` fixture.
Expand Down
3 changes: 2 additions & 1 deletion pyfakefs/pytest_tests/pytest_module_fixture_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def use_fs(fs_module):
yield fs_module


def test_fs_uses_fs_module(fs):
@pytest.mark.usefixtures("fs")
def test_fs_uses_fs_module():
# check that `fs` uses the same filesystem as `fs_module`
assert os.path.exists(os.path.join("foo", "bar"))

0 comments on commit 9ec929b

Please sign in to comment.