Permalink
Browse files

Added another example for custom argument usage with PyTest

- added a respective link in the documentation
  • Loading branch information...
mrbean-bremen committed Jul 24, 2018
1 parent 65e9bd0 commit 8990455e937ae94f036532482d8e8f89422feaf4
Showing with 20 additions and 17 deletions.
  1. +5 −2 docs/usage.rst
  2. +15 −15 pyfakefs/tests/pytest/pytest_fixture_test.py
View
@@ -95,8 +95,11 @@ patching does not work for some module.
.. note:: If you need these arguments in ``PyTest``, you have to
use ``Patcher`` directly instead of the ``fs`` fixture. Alternatively,
you can add your own fixture with the needed parameters (see
``pyfakefs.tests.pytest.conftest.py`` for a possible implementation).
you can add your own fixture with the needed parameters.
An example for both approches can be found in
`pytest_fixture_test.py <https://github.com/jmcgeheeiv/pyfakefs/blob/master/pyfakefs/tests/pytest/pytest_fixture_test.py>`__
with the example fixture in `conftest.py <https://github.com/jmcgeheeiv/pyfakefs/blob/master/pyfakefs/tests/pytest/conftest.py>`__.
modules_to_reload
~~~~~~~~~~~~~~~~~
@@ -16,36 +16,36 @@
import pytest
import pyfakefs.tests.pytest.example as example
from pyfakefs.fake_filesystem_unittest import Patcher
@pytest.mark.xfail
def test_example_file_failing(fs):
"""Test fails because EXAMPLE_FILE is cached in the module
and not patched."""
fs.create_file(
example.EXAMPLE_FILE, contents='stuff here')
fs.create_file(example.EXAMPLE_FILE, contents='stuff here')
check_that_example_file_is_in_fake_fs()
with open(example.EXAMPLE_FILE) as file:
assert file.read() == 'stuff here'
with example.EXAMPLE_FILE.open() as file:
assert file.read() == 'stuff here'
def test_example_file_passing_using_fixture(fs_reload_example):
"""Test passes if using a fixture that reloads the module containing
EXAMPLE_FILE"""
fs_reload_example.create_file(example.EXAMPLE_FILE, contents='stuff here')
check_that_example_file_is_in_fake_fs()
assert example.EXAMPLE_FILE.read_text() == 'stuff here'
assert example.EXAMPLE_FILE.is_file()
def test_example_file_passing_using_patcher():
"""Test passes if using a Patcher instance that reloads the module
containing EXAMPLE_FILE"""
with Patcher(modules_to_reload=[example]) as patcher:
patcher.fs.create_file(example.EXAMPLE_FILE, contents='stuff here')
check_that_example_file_is_in_fake_fs()
def test_example_file_passing(fs_reload_example):
"""Test passes if using a fixture that reloads the module containing
EXAMPLE_FILE"""
fs_reload_example.create_file(
example.EXAMPLE_FILE, contents='stuff here')
def check_that_example_file_is_in_fake_fs():
with open(example.EXAMPLE_FILE) as file:
assert file.read() == 'stuff here'
with example.EXAMPLE_FILE.open() as file:
assert file.read() == 'stuff here'
assert example.EXAMPLE_FILE.read_text() == 'stuff here'
assert example.EXAMPLE_FILE.is_file()

0 comments on commit 8990455

Please sign in to comment.