I\'ve been trying to implement some unit tests for a module. An example module named alphabet.py is as follows:
import database
def length_letters(
I ran into a problem where I was trying to mock out variables that were used outside of any function or class, which is problematic because they are used the moment you try to mock the class, before you can mock the values.
I ended up using an environment variable. If the environment variable exists, use that value, otherwise use the application default. This way I could set the environment variable value in my tests.
In my test, I had this code before the class was imported
os.environ["PROFILER_LOG_PATH"] = "./"
In my class:
log_path = os.environ.get("PROFILER_LOG_PATH",config.LOG_PATH)
By default my config.LOG_PATH is /var/log/, but now when the test is running, the log path is set to the current directory. This way you don't need root access to run the tests.