6

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.

  • I'm not overly familiar with Python's testing harness. How are you testing your other code? What aspects of the config file are you testing (or depending upon for a test)? – user40980 Mar 18 '15 at 1:36
  • @MichaelT I'm using python's built-in unittest library, which is pretty bare-bones (essentially, write a function to test an aspect). A certain function called full_url taken in a URL and modifies it using information from the config file. I want to test whether it's modifying it properly. – sinθ Mar 18 '15 at 1:40
  • 2
    Ideally you'd remove the dependency on the config file. – Doval Mar 18 '15 at 1:41
  • What kind of tests are you writing? Unit tests, acceptance tests? I could see how you could be dependent upon a config file in an acceptance test but a unit test should not depend upon a config file. Please elaborate on that. Thank you! – AvetisG Mar 18 '15 at 2:32
  • "Your config class, which contains all the settings should be just a plain old data type..." - if you design things the right way, as explained in answer I linked, testing will be easy. I recently wrote tests for about half hundreds methods depending on config handled this way - it works like a charm – gnat Mar 18 '15 at 14:28
5

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.

1

Depends on how you load data from your config file.

Ideally, write your program to be easily testable and reduce unnecessary dependencies. So make sure that if you change the location or format of your config file, that this affects as little code as possible. Then make sure that code can also deal with the testing.

If you have a single object or class or file or set of functions that deal(s) with the loading from the config file, change that so it can take a different config file or even just stub variables programmatically without needing a config file.

If you load the config file in multiple places and its path is hardcoded, it's a bit more difficult (you're also in a not so ideal situation). You could try to:

  1. Before the test, save your actual config file to a different location (config.back or something)
  2. Create a new config file with the values you want
  3. After the test, delete your newly created config file and restore the old one. Make sure to take exceptions into account!

If you just define a bunch of values in a python file and that is your config, refactor: adapt your config file so that you can change which values it returns. For example, you could create a bunch of functions to return your config values that default to your hardcoded config, but could be edited for a test program.

0

Well, dependency injection comes to mind as a default sort of boxed approach to config testing.

However, there are other ways, specifically designing your code to eliminate the dependency on config file all together.

You can find more details in a canonical example of dependency elimination by Brian Geihsler that talks specifically about config files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.