2

Currently we don't do much testing at our company, except some manual checking. We occasionally create some unit tests, integration tests and ui tests, but not on a regular basis. Since a new project is coming up I want to take the opportunity to implement a better way of testing.

Question

Since most of our web applications are actually performing basic crud applications, a lot of tests seem to be trivial. And my question is basically, how do I decide where I draw the line, is there an approach for that. Altought unit testing improves the quality, it comes with a cost (in the form of writing them and maintaining them). To get concrete, here are some examples.

Example

I have a productService with the following method:

public Product GetProduct(int productId)
{
   return database.GetObject<Product>("select * FROM products wh..",productId)
}

Should I create a unit test for this method? Let's say that I would, than I'm just checking if I'm calling the right query and if I'm forwarding the productId parameter correctly to the database layer. This feels already a little bit trivial. But to make it worse, I'm already planning to create an integration test for this method to check if it all works correctly together so this unit test won't add any value. But if I would follow TDD (which is dead, partly because of this reason as I have heard), than I should create that unit test.

Any thoughts?

1 Answers1

3

I'm already planning to create an integration test for this method to check if it all works correctly together so this unit test won't add any value.

Your actual goal is to prevent broken software from going into production, isn’t it? Unit testing is just one of many tools to achieve that goal. If your integration test catches the same error as the unit test, then that’s sufficient as far as effectiveness is concerned. No matter how adamant TDD is about testing everything, having that specific unit test will not make you catch any additional errors.

But what about efficiency? When the integration test fails because the GetProduct() method broke, how much time and effort will it take to reach that conclusion from the data the integration test provides? If that number is high then the additional cost of writing and maintaining a “second line of defense” might be worth it.

besc
  • 1,163
  • 6
  • 7