Home > Enterprise >  Testing with configuration files
Testing with configuration files

Time:03-19

my Google-fu has failed me by giving me results I don't understand, so I'm asking here.

I'm working on a Python project and I currently have a configuration file, which is also .py, that holds various python objects to load when everything starts. I'm trying to get some practice unit testing with pytest and I don't know exactly how to go about this issue. My guess is that I am probably going to make a dedicated testing config file that doesn't change, but I have no clue how to tell my code when to use the actual config file and when to use the testing config file. My current setup in my code is just using import config and setting values from it. I would greatly appreciate some help here!

CodePudding user response:

Figured out something based on this.

I ended up creating a testing config file in my folder named "tests" and having this code at the top of every file that used it:

import sys
if "pytest" in sys.modules:
    import tests.testing_config as config
else:
    import config

I don't think it's quite optimal since I feel something like this should be in the testing code, plus it kind of makes the new config file a dependency that should never be changed lest you break everything, but I guess it works for now if you have no clue how these cracked testing libraries work like me.

  • Related