1# Licensed under the Apache License, Version 2.0 (the "License"); 2# you may not use this file except in compliance with the License. 3# You may obtain a copy of the License at 4# 5# http://www.apache.org/licenses/LICENSE-2.0 6# 7# Unless required by applicable law or agreed to in writing, software 8# distributed under the License is distributed on an "AS IS" BASIS, 9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10# See the License for the specific language governing permissions and 11# limitations under the License. 12 13# Example for a test using a custom pytest fixture with an argument to Patcher 14# Needs Python >= 3.6 15 16import pytest 17 18import pyfakefs.pytest_tests.example as example 19 20 21@pytest.mark.xfail 22def test_example_file_failing(fs): 23 """Test fails because EXAMPLE_FILE is cached in the module 24 and not patched.""" 25 fs.create_file(example.EXAMPLE_FILE, contents='stuff here') 26 check_that_example_file_is_in_fake_fs() 27 28 29@pytest.mark.parametrize('fs', [[None, [example]]], indirect=True) 30def test_example_file_passing_using_parametrized_fixture(fs): 31 """Test passes if using a fixture that reloads the module containing 32 EXAMPLE_FILE""" 33 fs.create_file(example.EXAMPLE_FILE, contents='stuff here') 34 check_that_example_file_is_in_fake_fs() 35 36 37def check_that_example_file_is_in_fake_fs(): 38 with open(example.EXAMPLE_FILE) as file: 39 assert file.read() == 'stuff here' 40 with example.EXAMPLE_FILE.open() as file: 41 assert file.read() == 'stuff here' 42 assert example.EXAMPLE_FILE.read_text() == 'stuff here' 43 assert example.EXAMPLE_FILE.is_file() 44