CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeremy D. Miller -- The Shade Tree Developer

Under the hood and working with .Net, TDD, Software Design, and Agile Stuff

Want Testable Code? Be Careful with Static State

This post isn't really a rant, more of a warning.  I really, really think that testability should be a first class consideration for doing software design.  From experience, retrofitting an existing codebase with automated testing is problematic.  One of the worst culprits has consistently been dealing with application state.

Yeah, I know that I've written this post before, but one more time. Static state in your application can potentially cripple your ability to do automated testing.  The only way to create reliable automated tests is to completely control the inputs coming into the test.  Cached data really needs to be controlled as well to keep tests from being skewed by previous tests.  In order to be reliable and maintainable, automated tests should be isolated from one another and independent upon the order in which the tests are executed.

Yes, you most certainly have legitimate needs for static members, singletons, and cross request state.  Just remember to leave yourself an easy way to flush that state or replace those singletons on demand inside automated tests so that you can reliably establish a known starting state with know inputs. It might mean a little more code to write, and maybe even additional complexity.  A little more coding in exchange for easier testing can lead to shipping that same code earlier.  It's an awfully good idea to keep your eyes on the bigger picture.

O/R mappers can be particularly problematic because they often implement some sort of stateful cache beneath the covers.  We were getting burned today because we were resetting the database state of some domain objects directly to the database in the SetUp of a FitNesse suite.  The service had a cached version from the previous test and confusing errors ensued.  In my previous job we had a wrapper around our NHibernate access that had a "ResetSession()" method that we could call between tests to tear down the stateful NHibernate caches to guarantee a clean slate for each test.  We left ourselves a grammar in a FitNesse fixture that we could use to flush all of the cached data between test runs.  I would strongly recommend leaving yourself some sort of hook to do exactly that.

I've never employed this tactic much, but going to randomized data might be a good idea inside of test data.  In our case, I think we could make our problems largely go away by using a bit fancier FitNesse machinery to grab new surrogate key values inside our tests to avoid test data collision.  The test mechanics get a little bit harder, but I'll gladly take a bit more coding in return for tests that can run back to back.

More:

Chill out on the Singleton Fetish

TDD Design Starter Kit - Static Methods and Singletons May Be Harmful

*A* way to get around the Singleton problem without trashing your testability and creating strong coupling:  http://structuremap.sourceforge.net/SingletonInjection.htm
 



Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Jeremy D. Miller

Jeremy began his IT career writing "Shadow IT" applications to automate his engineering documentation, then wandered into software development because it looked like more fun. Jeremy previously worked as a systems architect building mission critical supply chain software for a Fortune 100 company and learned agile development practices as a .Net consultant at ThoughtWorks, one of the pioneers of agile development. Jeremy is the author of the open source StructureMap (http://structuremap.sourceforge.net) tool for Dependency Injection with .Net and the forthcoming StoryTeller (http://storyteller.tigris.org) tool for supercharged FIT testing in .Net. Jeremy's thoughts on just about everything software related can be found on his weblog "The Shade Tree Developer" at http://codebetter.com/blogs/jeremy.miller, part of the popular CodeBetter site. Jeremy is a Microsoft MVP for C#. Check out Devlicio.us!

This Blog

Syndication

News

All opinions expressed here constitute my (Jeremy D. Miller's) personal opinion, and do not necessarily represent the opinion of any other organization or person, including (but not limited to) my fellow employees, my employer, its clients or their agents.

About Me

"Best Of" Compendium

StructureMap (Dependency Injection for .Net)

StoryTeller (Supercharged Fit)

Build your own Cab

TestDriven

MVP