Am I checking a class that does nothing?

In my application, I have two classes: a log that actually logs into the database and a dummy logger that does nothing (used when logging is disabled). Here is the whole 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 the one hand, I should probably let this go, and not test it, since there really is no code to test. But then my β€œinfected” instinct tells me that this is an excuse and that the simplicity of the class means that I should be more prepared to test it. I'm just having problems with what to test.

Any ideas? Or should I just let it go and not write any tests?

+4
source share
15 answers

Of course, you can test a class that does nothing. You check that he really does nothing.

In practice, this means:

  • it exists and can be created;
  • it does not throw an exception when used; call each method and simply claim that they succeed. It also doubles as a check that the class really defines every method that it expected.

Do not run this test in DummyLog; use it as a general test and run it on all registrars. When you add another method to the real log class in a year and don't forget to add it to DummyLog, the test will notice. (If you, of course, do not forget to add a general test for this method, but hopefully adding a test should be familiar even if you forget about related classes.)

+4
source

If you are not experiencing, how do you really know that he is not doing anything? :)

Sorry - I could not resist. Seriously - I would experience it, because someday it can do more?

+12
source

If he cannot fail. Nothing to test.

Test results must contain at least one successful condition and at least one failed condition. If any entry into the test leads to a successful result. Then there is no test that you could create, ever fail.

+4
source

Be pragmatic, there's nothing to test here.

+3
source

Depends on your theory.

If you use a type controlled by a test, then in order for this code to exist, you had to write a test for it.

If you think about it, I wrote it, how to test it, then I think it requires a test, because you rely on it to do nothing. You need a test to make sure that someone is not behind you and will not delete this code (maybe you too).

+2
source

If anyone is interested, here is a test I wrote:

 def test_dummy_loader(): from loader_logs import DummyLog from copy import copy dummy = DummyLog() initial = copy(dummy.__dict__) dummy.insert_master_log('', '', '', '') dummy.update_master_log(0, 0, 0, 0) post = copy(dummy.__dict__) assert initial == post 

Essentially, it checks that attributes are not set on the object when calling two stub methods. Of course, he still doesn't check that the methods really do nothing, but at least it's something.

+2
source

This is clearly something or you would not have written.

I'm going to guess that it is intended to mirror the interface of your real logging class. Therefore, check that it has the same interface as the same arguments. You will probably change your journal, forgetting to update the layout. If this seems redundant, this is because they probably should just inherit from the same interface.

And then yes, you can verify that it is not registering anything. This may seem silly, but surprising what maintenance programmers will do.

+2
source

If he does nothing, then there is nothing to test. If you really want to, you can make sure that it does not modify any state. I'm not familiar enough with Python to find out if there is an easy way to make sure your methods don't call any other methods, but you can do this if you want.

+1
source

I do not know Python, but I can recall one test test, which the class creates without errors. This will be at least a class regression test and means that it should work under any circumstances.

You never know that someone can edit the class in the future and make it throw an exception or something strange!

Personally, although if you are not targeting an insanely high level of test coverage, I would not bother.

Said it would be disastrous if this class made an exception? I assume that this will be one of those errors that without unit test will only get into the field.

+1
source

Argument: you do not base your tests on reading the implementation, but on the intended behavior. You verify that the black thing is not crashing when called.

This case is a bit intrusive, perhaps, and frankly, I might not have bothered. But for these null functions, only a small increase is required so that they can be tested.

+1
source

This DummyLogger is what is called in the design pattern as a "Null Object". To subclass your real Logger from this, create some test for the real logger, and then use the same test, but with DummyLogger.

 class TestLogger(unittest.TestCase): def setUp(self): self.logger = RealLogger() def test_log_debug .. def test_log_error .. class TestNullLogger(TestLogger): def setUp(self): self.logger = DummyLogger() 

But many suggested that you do not need it. When it slows down, correct it.

+1
source

You can check the arguments that are passed to it. If this is a dummy object that will be called with a specific set of arguments, then changing these arguments will fail. A test like this ensures that if it really changes at least no other code break that depends on it.

0
source

According to the rule β€œYou do not want it to be,” you should not write a test when there is nothing to test, even if one day it can do something.

How to verify that something was not doing anything? This is a good philosophical question :)

0
source

I think that the only useful advantage of the test for this class is, I hope, to catch if someone starts to modify it on the way. Otherwise, I would not bother.

0
source

At least you want it to not violate anything when used in place of the actual registrar. Therefore, reuse the actual logger tests and adjust the statements that verify that they are actually being logged.

0
source

All Articles