Assume we have two files some_module.py
and some_config_file.py
. The latter is a file that contains paths to files, parameters for functions, etc. The former has now functions that use these parameters, e.g.
# some_module.py
import some_config_file as config
def func():
path = config.path_to_some_data
# do some logic that involves `path`
I would like to write some tests to ensure that func
does what it is supposed to do. To do this, I would need to prepare some data in a specific file to be able to predict what func
will do with it. I see two way of doing this:
- I write a specific
test_config_file.py
that points to data and parameters specific for tests. This would be my preferred way of doing it, but I don't really know how to letsome_module.py
know that I'm runningunittest
on it... - I modify my functions, i.e. in the above example we would have something like
.
# some module
import some_config_file as config
def func(test: bool = False):
path = config.path_to_some_data
if test:
path = "path/to/test_data"
# do some logic that involves `path`
This works but is somewhat cumbersome since I need to effectively change every function in the project in a very repetitive way...
Is it possible to use method 1., can method 2. be achieved more elegantly, or is there a completely different way?