Tests running slow? It's a feature! - No Fluff Just Stuff

Tests running slow? It's a feature!

Posted by: Craig Walls on July 25, 2007

Departing from my usual Spring-related topic, I decided to write a little bit about unit-testing...specifically, unit-testing in .NET.

As some of you may know, I've been on a .NET project (gasp!) for the past year. It's been rather enlightening and I'm not nearly as religiously opposed to .NET as I was in the past. (Before you Java fanatics out that faint from this news, let me assure that I'm still a Java programmer at heart.)

Anyhow, lately I've contracted a bit of a test infection and have been trying to add more unit-tests to my project. The tests I've written will no doubt help me write better code and give me the comfort of knowing that my code is behaving as expected...but not now...in a minute...once the tests finish running.

You see, I'm running tests from within VisualStudio, using both ReSharper's unit-testing support and the TestDriven.NET add-in. Each has its wrinkles, but I think I like TestDriven.NET more, if for no other reason than it gives me a code-coverage tool.

But in either case, the testing process is SLOW. I have one test with only two very simple test methods in it. Should pop up a green bar in no time at all, right? Nope...because of a lack of an incremental compiler for C#, VisualStudio feels the need to recompile everything in the project. That's the first 20-30 seconds of testing. Then it sits there doing nothing interesting for another 15 seconds. Then the tests start up. About 45 seconds later, the tests have completed.

Before you say anything, I know that I could've used the NUnit runner...it's certainly faster than both ReSharper and TestDriven.NET. But that's only because it doesn't compile anything before it runs...it works off of already-compiled assemblies. Faster, yes...but it could get me in trouble thinking that a test passed when, in fact, a not-yet-compiled change breaks the test.

A comparable set of tests in Eclipse's JUnit plug-in popped up the familiar green bar in less than 1 second. (Thanks largely to the incremental compiler in Eclipse.)

If anyone knows any secrets for speeding up testing with any of the .NET testing tools, please let me know...I'm very eager to hear about them.

The silver-lining here is that I've discovered that the speed at which these VisualStudio add-ins run unit-tests is proportional to the size of the project they're testing. If you have a large "blob" project with hundreds of classes, then it's going to take awhile for your tests to run (because there's more to compile). This encourages me to break my application down into smaller bite-sized projects...probably a good idea even if the tests were fast. So, slow tests are a feature that encourages better design!

Incidentally, here's a brilliant article on unit-testing. Notice item number 12 and how that hits home with .NET developers.

Oh look! My tests are finally finished. Gotta go.

Craig Walls

About Craig Walls

Craig Walls is a Principal Engineer, Java Champion, Alexa Champion, and the author of Spring AI in Action, Spring in Action, and Build Talking Apps. He's a zealous promoter of the Spring Framework, speaking frequently at local user groups and conferences and writing about Spring. When he's not slinging code, Craig is planning his next trip to Disney World or Disneyland and spending as much time as he can with his wife, two daughters, 1 bird and 2 dogs.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »