Search code examples
unit-testingjunitmicrosoft-fakes

Which methods absolutely need Unit Testing


My team is working on the development of an application running for several years already, but no unit test has ever been coded. Now that we wish starting doing so, we realise we cannot possibly go over all the existing methods to test them because that would take years of work.

The question is : how can one decide which method absolutely need unit testing, and which doesn't?

Would you rather unit test a method that is often called or a method that is often modified? I read that Unit Testing is rather inefficient on DAO classes. Should I restrain the tests to methods containing logic?

Most important : Will the tests put in place any useful as far as only part of the application is unit tested?


Solution

  • how can one decide which method absolutely need unit testing, and which doesn't?

    This is a difficult question to answer without knowing about your code base and what its history and future are. But in general, write tests for the parts of the code that are hard to understand, will get modified in the near future or are known to have bugs. When testing legacy applications, the best bang for your buck is to have tests make the program both easier to maintain going forward, to fix bugs and to keep old bugs from coming back.

    Would you rather unit test a method that is often called or a method that is often modified?

    As stated above, it depends. Is the method that is called often trivial? Easy to understand? I would probably lean towards "often modified" just to make future development easier. But ideally both should get tested.

    I read that Unit Testing is rather inefficient on DAO classes.

    I don't know where you read that. Unit testing can be very efficient with DAOs if you use mock objects.

    Will the tests put in place any useful as far as only part of the application is unit tested?

    Any tests are useful. A program that is only 10% covered by tests is better than a program with 0% coverage. Especially if that 10% is the most important or trickiest part of the program.

    If you haven't read it yet, I highly recommend Michael Feather's Working Effectively with Legacy Code where "legacy code" means code that doesn't have tests.