Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining which classes would benefit most from unit testing?

I am working on a project where we have only 13% of code coverage with our unit tests. I would like to come up with a plan to improve that but by focusing first on the areas where increasing coverage would bring the greatest value. This project is in C#, we're using VS 2008 and TFS 2008 and out unit tests are written using MSTest.

What methodology should I use to determine which classes we should tackle first? Which metrics (code or usage) should I be looking at (and how can I get those metrics if this is not obvious)?

like image 911
Benoit Martin Avatar asked Mar 17 '10 21:03

Benoit Martin


People also ask

Which are the main benefits of unit testing?

One of the benefits of unit tests is that they isolate a function, class or method and only test that piece of code. Higher quality individual components create overall system resiliency. Thus, the result is reliable code. Unit tests also change the nature of the debugging process.

When we should use unit testing?

Tests can be set to run either a one-time check at a certain time interval or can be run immediately in real-time to review changes. In short, unit tests help developers detect problems immediately, then fix them quickly. With fewer resources spent finding bugs, teams can move on to the next phase of a project.


4 Answers

I would recommend adding unit tests to all the classes you touch, not retrofitting existing classes.

Most of the advantages of unit testing is in helping programmers code and ensuring that "Fixes" don't actually break anything, if you are not adding code to a new section of code that isn't every modified, the benefits of unit tests start to drop off.

You might also want to add unit tests to classes that you rely on if you have nothing better to do.

You absolutely should add tests to new functionality you add, but you should probably also add tests to existing functionality you may break.

If you are doing a big refactor, consider getting 80-100% coverage on that section first.

like image 144
Bill K Avatar answered Oct 01 '22 20:10

Bill K


For some good statistics and deterministic querying of certain methods you could definitely look at NDepend: http://www.ndepend.com/

NDepend exposes a query language called CQL (Code Query Language) that allows you to write queries against your code relating to certain statistics and static analysis.

There is no true way to determine which classes might benefit the most, however by setting your own thresholds in CQL you could establish some rules and conventions.

like image 42
Stuart Thompson Avatar answered Oct 01 '22 21:10

Stuart Thompson


The biggest value of a unit test is for maintenance, to ensure that the code still works after changes.

So, concentrate on methods/classes that are most likely / most frequently changed.

Next in importance are classes/methods with less-than-obvious logic. The unit tests will make them less fragile while serving as extra "documentation" for their contracted API

like image 30
DVK Avatar answered Oct 01 '22 20:10

DVK


In general unit tests are a tool to protect against regression and regression is most likely to occur in classes with the most dependencies. You should not have to choose, you should test all classes, but if you have to, test the classes that have the most dependencies in general.

like image 35
Paul Creasey Avatar answered Oct 01 '22 20:10

Paul Creasey