0

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.

David_O
  • 1,143
  • 7
  • 16
  • This should be possible. I may have a closer look later today, but I don't see why it shouldn't work. The underlying `fs` object is the same, so that should not be the problem. – MrBean Bremen Feb 25 '22 at 13:26
  • 2
    I just tested your fixtures, and they work as expected (e.g. '/path/to/user/config' exists with one fixture, but not with the other). Can you provide a minimal reproducible example? – MrBean Bremen Feb 25 '22 at 15:20
  • I cannot. The dummy code above is (more or less) exactly what my real code used but - in going back to basics - I seem to have stripped out some bug unrelated to `fakesfs` that was causing failure. Embarrassing, but at least this confirmation that it should work has prompted me to go back and try again more carefully. And it does indeed now work in my real case. I'm not sure what SO does about questions like this! You have confirmed that this is supported and should work. Can you submit "Do it again, more carefully" as an answer that I can accept? – David_O Feb 28 '22 at 12:32
  • Glad you got it to work. You can just delete the question, as it proabbaly won't help anyone else. – MrBean Bremen Feb 28 '22 at 12:40

0 Answers0