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.
Python – How to write tests for function that depends on a config file
programming practicespythontesting
Related Solutions
You have to mock or emulate, and you have two very good reasons to do so.
You have to mock or emulate whatever parts of the system your code interacts with, precisely because:
- You don't want to alter the system, and
You don't want your tests to depend on something as uncontrollable as the system.
Yes, your emulation will not be perfect; yes, you will in fact be modelling your knowledge and assumptions about how the system works; and that's perfectly fine. You are essentially taking a spec and restating it in code: nothing wrong with that. The spec in this case is written by the creators of the operating system and not by you.
You have to mock or emulate everything that deals with time, even your own stuff.
- You have to be able to set the emulated clock time to a certain fake date, pretend that files are created/modified, and then check their creation/modification date and make sure it is the fake date, and all this without of course changing the system clock.
- You also have to be able to check that a certain date in the future is correctly set, or that something happens at that moment in time, all during the execution of a single test, without having to wait for real time to advance.
There are a couple of things you can do to make testing software like that easier. First, try to abstract as much as you can into layers that aren't visual. That will let you just write standard unit tests on those lower layers. For example, if you have a button that performs a certain calculation, make sure you have a way to perform that calculation in a unit test with a regular function call.
The other suggestion for testing graphics-heavy programs is to create some output that a tester can easily manually verify. A minecraft example is:
I've also worked on projects that had a bunch of tests that rendered something on the screen, then prompted the tester to manually verify it matched the description. This makes sure you don't forget test cases later.
Testing is often extremely difficult if you didn't originally design with testing in mind. Just work on putting your most fragile code under test first. When you find a bug, make a test that will catch that bug if it happens again. That often prompts writing related tests while you're at it.
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.