David_O
David_O

Reputation: 1153

Maintaining two pyfakefs file systems in pytest with additional files

I'm trying to write unit tests using pytest for a configuration system that should look in a couple of different places for config files. I can use pyfakefs via the fs plugin to create a fixture that provides a set of files, including a config file in one location, but I'd like to have unit testing that both locations are checked and that the correct one is preferred.

My initial thought was that I could create the first fixture and then add a file:

@pytest.fixture()
def fake_files(fs):
    fs.create_file('/path/to/datafile', contents="Foo")
    yield fs

@pytest.fixture()
def user_config(fake_files):
    fake_files.create_file('/path/to/user/config', contents="Bar")
    yield fake_files

The idea behind this is that any test using fake_files would not find the config, but that using user_config would. However, unit tests using that user_config fixture do not find the file. Is this possible?

There is quite a lot more added in the first fixture in reality, so maintaining the two systems as two completely separate fixtures duplicates code and I am unclear if the underlying fs object can even be used in parallel.

Upvotes: 0

Views: 517

Answers (0)

Related Questions