Unit Test Runs Stale Code Because Code Coverage Is Enabled in Visual Studio

Wednesday, April 28, 2010

A quick post to (maybe) save some of you some troubleshooting time one day. Executive summary: If your Visual Studio unit tests work when a debugger is attached and fail when there’s no debugger attached, make sure that the test is running the latest version of your code, and if it’s not, try turning off code coverage in your test run configuration. During the last two weeks I wasted at least an accumulated 5 hours trying to understand why my Visual Studio unit tests fail even after I fixed the bug. In particular, I couldn’t wrap my...
one comment

PDC 2009 Day 1: Code Contracts and Pex: Power Charge Your Assertions and Unit Tests

Wednesday, November 18, 2009

Code Contracts are a mechanism coming out of incubation at Microsoft Research for expressing design intent when implementing an API. The idea is to specify assertions that are verified at runtime to ensure that callers provide proper information to your methods. There are ways to specify contractual information on methods – Contract.Requires and Contract.Ensures, and there are also interface contracts (for any implementation of the interface) and object invariants. Finally, there are automatic tools that perform runtime checking and static checking and even generate documentation from contracts! (It’s embedded in the XML documentation and Sandcastle can be used to...

What I Learned About Writing Unit Tests: Dependency Injection Mess With Mocks

Sunday, October 18, 2009

Armed with some experience, I embraced dependency injection in all its might. I started writing subsystems and components that interact only through well-defined interfaces, which was relatively easy for me because my previous infrastructure project relied heavily on dynamically-generated proxies that worked only with interfaces. This allowed me to abstract away and stub away everything a component needed under a test. And then my tests had the following shape and form (I’m not using any specific mock framework syntax, for illustration purposes): public void LoggingFramework_LogToDB_Works() ...

What I Learned About Writing Unit Tests: Test Infrastructure

Saturday, October 10, 2009

In the previous installment I mentioned that I wasn’t satisfied with tests that check whether a single constructor throws a single exception. Over the years I started looking for something with a little more value, a test that I could show developers writing code for my framework and tell them that’s the way to use my APIs. Naturally, the next thing I started doing was writing infrastructure frameworks for writing tests for my framework. This means I would have all kinds of TestHelper and ContextHelper and TestSetupReflector classes that would take care of everything necessary to make the...
one comment

What I Learned About Writing Unit Tests: My First Tests

Wednesday, October 7, 2009

Over the last 4 years, I embraced the idea of writing unit tests for my software. Most of what I’ve written during these years has been software frameworks, with very high reliability and stability guarantees and explicit needs for backwards compatibility—all perfectly ripe for a good unit test suite. In the following short series of posts, I will try to share the story of what I learned from writing unit tests, and why the tests I write today are nothing like the tests I wrote 4 years ago. The first unit tests I’ve written were taken straight from...