Do I test a class that does nothing?
polymorphism, python, testing, unit-testing
Solution
Of course you can test a class that doesn't do anything. You test that it does, in fact, not do anything.
In practice, that means:
- it exists and can be instantiated;
- it doesn't throw an exception when used; call every method and simply assert that they succeed. This also doubles as checking that the class does, in fact, define every method that it's expected to.
Don't run this test on DummyLog; use it as a generic test and run it on all loggers. When you add another method to the real Log class a year from now and forget to add it to DummyLog, the test will notice. (As long as you do remember to add a general test for the method, of course, but hopefully adding the test should be habitual, even if you forget about related classes.)
Problem
In my application, I have two classes: a logger that actually logs to the database and a dummy logger that does nothing (used when logging is disabled). Here is the entire DummyLog class: ``` class DummyLog(object): def insert_master_log(self, spec_name, file_name, data_source, environment_name): pass def update_master_log(self, inserts, updates, failures, total): pass ``` On one hand, I should probably let this go and not test it since there's not really any code to test. But then, my "test-infected" instinct tells me that this is an excuse and that the simplicity of the class means I should be more willing to test it. I'm just having trouble thinking of what to test. Any ideas? Or should I just let this go and not write any tests?