Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I test a class that does nothing?

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?

like image 807
Jason Baker Avatar asked Nov 27 '22 05:11

Jason Baker


2 Answers

If you don't test, how will you really know it does nothing? :)

Sorry - couldn't resist. Seriously - I would test because some day it might do more?

like image 156
n8wrl Avatar answered Nov 29 '22 17:11

n8wrl


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.)

like image 24
Glenn Maynard Avatar answered Nov 29 '22 18:11

Glenn Maynard