C# – Unit Testing and Test Driven Development Questions

asp.net-mvcctddunit testing

I'm working on an ASP.NET MVC website which performs relatively complex calculations as one of its functions.

This functionality was developed some time ago (before I started working on the website) and defects have occurred whereby the calculations are not being calculated properly (basically these calculations are applied to each user which has certain flags on their record etc).

Note; these defects have only been observed by users thus far, and not yet investigated in code while debugging.

My questions are:

  • Because the existing unit tests all pass and therefore do not indicate that the defects that have been reported exist; does this suggest the original code that was implemented is incorrect? i.e either the requirements were incorrect and were coded accordingly or just not coded as they were supposed to be coded?

  • If I use the TDD approach, would I disgregard the existing unit tests as they don't show there are any problems with the calculations functionality – and I start by making some failing unit tests which test/prove there are these problems occuring, and then add code to make them pass?

Note; if it's simply a bug that is occurring that can be found while debugging the code, do the unit tests need to be updated since they are already passing?

Best Answer

Short Answer: Most probably unit tests are not well designed to cover the critical calculation scenarios. In addition, there might be business rule modifications/changes that are not covered in the existing unit tests, because of million reasons that one may face in a tightly scheduled development environment.

As was also mentioned:

Automatic unit testing doesn't imply that the final tests will cover all cases and code will be absolutely correct and bug-free. It is easy not to see some complex use-cases and possible bug.

There might be million reasons why it is NOT done right. My point is that you need to cover these cases with product owner, and then go through each unit test and verify the implementation by applying correct naming of each unit test, as well as by adding the missing once.

If I use the TDD approach, would I disgregard the existing unit tests as they don't show there are any problems with the calculations functionality - and I start by making some failing unit tests which test/prove there are these problems occuring, and then add code to make them pass?

TDD (Test-driven development) will for sure help, but i would NOT discard the current set of unit tests as far as they do what they supposed to be doing.

Related Topic