Python – How to write tests for function that depends on a config file

programming practicespythontesting

I have a function that uses information from a config file. How do I test the function? Ideally, I'd want to inject my own version of the config file and test from there, but I'm not using dependency injection.

Best Answer

You have to write a test that works independent of the config file, so you can test that depending on the "simulated configuration" the output of the function or behavior of that function is correct.

You would need to inject the configuration file, or the value that you are trying to simulate on your function under test. This is the only way to guarantee that whenever you have that value, your function behavior is the indicated by the configuration file.

Ideally, you would also isolate the class that contains and dictates the behavior of the configuration file to the class/function that reads this file. When writing testing one of the goals should be isolation of the code under test.

Related Topic