Stories about Software


TDD Even with DateTime.Now

Recently, I posted my incredulity at the idea that someone would go to the effort of writing unit tests and not source control them as a matter of course. I find that claim as absurd today as I did then, but I did happen on an interesting edge case where I purposely discarded a unit test I wrote during TDD that I would otherwise have kept.

I was writing a method that would take a year in integer form and populate a drop down list with all of the years starting with that one up through the current year. In this project, I don’t have access to an isolation framework like Moles or Typemock, so I have no way of making DateTime.Now return some canned value (check out this SO post for an involved discussion of the sensitivity of unit tests involving DateTime.Now).

So, as I thought about what I wanted this method to do, and how to get there, I did something interesting. I wrote the following test:

To get this to pass, I changed the method SeedWithYearsSince() to add a random item to the list. Next test I wrote was:

Now, I had to actually add “2012” in the method, but it was still pretty obtuse. To get serious, I wrote the following test:

Now the method had to do something smart, so I wrote:

And, via TDD, I got to the gist of my method correctly. (I would later write tests that passed in a null list and a negative year and test that descriptive exceptions were thrown, but this is more or less the finished product). But now, let’s think about the unit tests vis a vis source control.

Of the three tests I’ve written, the first two should always pass unless I get around to finishing the time machine that I started building a few years back. We might consolidate those into a single test that’s a little more meaningful, perhaps by dropping the first one. We might also tease out a few more cases here to guard against regressions, say proving that calling it with 2010 adds 2010, 2011 and 2012 or something. While I don’t generally feel good about checking in tests that exercise code dependent on external state (like “Now”), we can feel pretty good about these given the nature of “Now”.

But that last test about 2 items when passed 2011 is only good for the remainder of 2012. When you wake up bright and early on New Year’s morning and run to the office and kick off a test run, this test will fail. Clearly we don’t want to check that test in, so all things being equal, we’ll discard it. That’s a bummer, but it’s okay. The point of the unit tests written here was a design strategy — test driven development. If we can’t keep the artifacts of that because, say, we don’t have access to an isolation framework or permission ot use one, it’s unfortunate, but c’est la vie. We’ll check in the tests that we can and call it a day.

This same reasoning applies within the context of whatever restrictions are placed on you. Say you are assigned to a legacy codebase (using the Michael Feathers definition of “legacy” as code without unit tests) and do not have rights to add a test project, for whatever reason. Well, then write them to help you work, keep them around as best you can to help for as long as you can, and discard them when you have to. If you have a test project but not Moles or Typemock, you do what we did here. If you have code that you have to use that lacks seams, contains things like singeltons/static methods or otherwise presents testability problems, take the same approach. Better to test during TDD and discard then not to test at all since you can at least guard against regressions and get fast feedback during initial development.

I’ve often heard people emphasize that TDD is a development methodology first and the unit tests for source control are a nice ancillary benefit. But I think the example of DateTime.Now really drives home that point. The fact that DateTime.Now (or legacy code, GUI code, threaded code, etc) is fickle and hard to test need not be a blocker from doing TDD. Clearly I think we should strive to write only meaningful tests and to keep them all around, but this isn’t an all or nothing proposition. Make sure you’re verifying your code first and foremost, preserve what you can, and seek to improve through increasingly decoupled code, better tooling, and more practice writing good tests.