Unit Testing – What not to test..

Yesterday I came across this question in StackOverflow:

I have gone through a number posts on stackoverflow and numerous articles about Unit Tests. I am just trying to figure out that what I have understood is right.

  1. Do not test anything that does not involve logic. For example: If there is a method in the service layer which simply invokes another method in the data access layer, don’t test it.

  2. Do not test basic database operations. If I have a method in the DAL which simple inserts an object in the database, say "public void save(Object object){…}" and there is no processing done on the object received from the service layer, don’t test it.

  3. I don’t need to validate objects at all layers. This means a certain field in an object should be not null, say the emailId in User Object, and this is verified and validated at the JSP(Using JS), I don’t need to test how the DAL method behaves if it receives emailId=NULL, because ideally it should not, and this should be taken care by the JS.

What else should I be not testing?

It seems to me that every software developer who has been writing unit tests for some time get to a point in which he ask himself (or his peers if he’s lucky) – “should I unit test this?”

There are many instances in which writing a unit test for a specific piece of code is a waste of time or even harmful to the project.

The (wise) people who answered the questions belong to one of two camps:

  • Test everything – if you can test it you should.
  • It depends camp – you shouldn’t test everything only logic or integration or …

I belong to the 2nd camp. There are times in which writing a test can be counter productive, in fact sometimes it is better not to have a test at all:

  1. If the test constantly breaks – till we ignore it or delete it
  2. If the test always passes – even when I delete the code it was suppose to test.
  3. Testing infrastructure – check that webservices actually work is not you job unless you’re part of that team in Microsoft. Because more tests means more build time means it takes longer to fail when an error is introduced.

If you’ll look at the question you’ll notice that its author already specified three things you shouldn’t test, do you know of other scenarios you do not want to write unit test for?

TOP