Code Coverage: What’s Enough?

I’ve been happily coding along on an ASP.NET MVC project using Test Driven Development (TDD). When you write the test first, you usually end up with very high code coverage numbers…

Media_httpblogdavebou_hejkh

(Obviously the LoginController was not written TDD – creating tests for it is next on my list!)

Anyhow – I’m currently working on the SecurityHelper – which does some rather ugly User.Roles to Workflow-Status comparisons to determine if a user can View/Edit fields on the Project domain object. Since this is really the core of this system’s security model, I really wanted this to have high coverage. With TDD I should have 100% right? What’s with that 99%

Digging into NCoverExplorer, we can see the coverage by method…

Media_httpblogdavebou_kclmc

So – my problem is the GetEditAccess method. When we dig in further, we can see the actual code that’s not executed during the test run… (how awesome is NCoverExplorer!)

Media_httpblogdavebou_fhjap

This is the default clause in a switch/case block that is switching on an enumeration. I even noted that this code would only be hit if someone added a value to the enum, re-compiled the app and passed that into the method. And, should this occur, I want to ensure that security is not compromised.

So -  the question becomes – if I can’t* write a test to exercise this code, should it be there? Rigorous TDD would say no, but having guard clauses is a good idea – especially a default in a switch/case.

Personally, I’m leaving the guard clause in there and I’ll just have to live with 99% coverage for this class, but it’s something to think about – what amount of coverage is enough? What is too much?

For this project, we have used code-generation to create some “domain object” starting points (we can’t do pure Domain Driven design here because we have inherited a populated database). At this point in the project, we’ve got about 30 of these domain classes, which are dominantly just properties. Since we “trust” the .NET CLR to do it’s job and correctly handle the getters and setters, we are not spending time with testing that code. When we add logic to these classes, we add tests, and we expect high-coverage on them.

What’s  your take on Code Coverage? Worth it or waste of time?

*[Ok, you could likely do something with Reflection, but that would waste a few hours figuring out how to do it - does not see like very efficient way to spend time.]

Advertisement
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s