Testing DateTime.Now - Introducing Wrap and Override

Monday 2 February 2009

Abstract:
The student said to his zen master: "What time is it?"
"It is TimeWarper.Now", the master replied.
"What time is that?", asked the student.
"It is whatever time I need it to be.", said the master.
And the student was enlightened.


Quite often you'll want your application to save a timestamp of when something occured. Due to the nature of time this presents a special set of challenges when it comes to testing.

Let's look at an example for logging exceptions.
Here's an initial attempt at a test:


[Test]
public void TestLogException_LogsCorrectValues()
{
try
{
throw new AbandonedMutexException("The poor mutex was abandoned!");
}
catch (Exception ex)
{
// Log the exception
ExceptionLog log = _exceptionLogger.LogException(ex);

// Assert that the logged values are correct
Assert.AreEqual("System.Threading.AbandonedMutexException", log.ExceptionType);
Assert.AreEqual("The poor mutex was abandoned!", log.Message);
Assert.That(!string.IsNullOrEmpty(log.StackTrace));
Assert.AreEqual(1, log.NumOccurences);
Assert.AreEqual(DateTime.Now, log.FirstOccurrence); // <-- Mind the dates!
Assert.AreEqual(DateTime.Now, log.LastOccurrence); // <-- Mind the dates!
}
}
That looks straightforward enough doesn't it?
Let's make the test work:


public class ExceptionLogger
{
public ExceptionLog LogException(Exception ex)
{
ExceptionLog log = new ExceptionLog();
log.ExceptionType = ex.GetType().ToString();
log.Message = ex.Message;
log.NumOccurences = 1;
log.StackTrace = ex.StackTrace;
log.FirstOccurrence = DateTime.Now;
log.LastOccurrence = DateTime.Now;
return log;
}
}

public class ExceptionLog
{
public string ExceptionType { get; set; }
public string Message { get; set; }
public string StackTrace { get; set; }
public DateTime FirstOccurrence { get; set; }
public DateTime LastOccurrence { get; set; }
public int NumOccurences { get; set; }
}
Running the test, we see that it fails!



The line failing is:
Assert.AreEqual(DateTime.Now, log.FirstOccurrence);
With this message:
NUnit.Framework.AssertionException:   
Expected: 2009-02-02 11:39:42.820
But was:  2009-02-02 11:39:42.787


As you can see 33 milliseconds have past between saving DateTime.Now to log.FirstOccurence and comparing the results to DateTime.Now in the Assert.AreEqual method.
This makes perfect sense. Time tends to pass even for very fast computers.

But what do we do about it? There are a few possible solutions that spring to mind:

Remove the Assert - We could just decide it's too much trouble to test and move on. That's not very nice. If we go down that road we could just stop testing right now and finish the code.

Make the Assert less precise - We could just strip the milliseconds. Who cares about them anyway? We could do that, but there's a 3,3% chance that the test is run in the milliseconds that overlaps one second changing to another. This would cause the test to inexplicably fail every now and again. Unacceptable.

Overload ExceptionLogger.LogException 
We could get control of what DateTime to set by overloading LogException like this:


[Test]
public void TestLogException_LogsCorrectValues()
{
try
{
throw new AbandonedMutexException("The poor mutex was abandoned!");
}
catch (Exception ex)
{
DateTime now = DateTime.Now; //<-- New variable!

// Log the exception
ExceptionLog log = _exceptionLogger.LogException(ex, now);

// Assert that the logged values are correct
Assert.AreEqual("System.Threading.AbandonedMutexException", log.ExceptionType);
Assert.AreEqual("The poor mutex was abandoned!", log.Message);
Assert.That(!string.IsNullOrEmpty(log.StackTrace));
Assert.AreEqual(1, log.NumOccurences);
Assert.AreEqual(now, log.FirstOccurrence); // <-- Compare now instead
Assert.AreEqual(now, log.LastOccurrence); // <-- Compare now instead
}
}

...

public class ExceptionLogger
{
public ExceptionLog LogException(Exception ex)
{
return LogException(ex, DateTime.Now);
}

public ExceptionLog LogException(Exception ex, DateTime timeOfException)
{
ExceptionLog log = new ExceptionLog();
log.ExceptionType = ex.GetType().ToString();
log.Message = ex.Message;
log.NumOccurences = 1;
log.StackTrace = ex.StackTrace;
log.FirstOccurrence = timeOfException; //Use the passed in value
log.LastOccurrence = timeOfException; //Use the passed in value
return log;
}
}

This solution is actually not too shabby. The test passes:



But it comes with two drawbacks:
  • It's making the method signature more complex. People using ExceptionLogger might not be sure if they're required to pass in a datetime and what it should be.
  • We're not actually testing that DateTime.Now is used as the timestamp in the default case.

Let's look at an alternative!

The Solution: Bending time and space

Let's face it, DateTime.Now is one of those things that gets called a lot and that you don't want to think about. The previous solution works well for a test this small but in more complex scenarios we probably have enough stuff to throw around that we don't want to complicate things further by passing confusing DateTime arguments that are not really needed.

To get complete control of time itself for unit testing here is a class I always create within two hours of joining a software project:


public class TimeWarper
{
private static DateTime? _now;

/// <summary>
/// TimeWarper.Now is a utility for getting the current time.
/// In production code it simply wraps DateTime.Now, but allows the current
/// DateTime to be overridden for unit testing purposes.
/// </summary>
public static DateTime Now
{
get
{
return _now ?? DateTime.Now;
}
set
{
_now = value;
}
}
}
This is a comon trick known as Wrap and Override. 
You wrap the thing you want to fake in your unit test and override it with something under your control. In this case we just want to make time stand still so we can accurately measure it.

Here's how it looks if we use it in the example above:


[TestFixture]
public class ExceptionLoggerTests
{

private ExceptionLogger _exceptionLogger;

[SetUp]
public void SetUp()
{
TimeWarper.Now = DateTime.Now; //Freeze!
_exceptionLogger = new ExceptionLogger();
}

[Test]
public void TestLogException_LogsCorrectValues()
{
try
{
throw new AbandonedMutexException("The poor mutex was abandoned!");
}
catch (Exception ex)
{
// Log the exception
ExceptionLog log = _exceptionLogger.LogException(ex);

// Assert that the logged values are correct
Assert.AreEqual("System.Threading.AbandonedMutexException", log.ExceptionType);
Assert.AreEqual("The poor mutex was abandoned!", log.Message);
Assert.That(!string.IsNullOrEmpty(log.StackTrace));
Assert.AreEqual(1, log.NumOccurences);
Assert.AreEqual(TimeWarper.Now, log.FirstOccurrence); // <-- TimeWarper!
Assert.AreEqual(TimeWarper.Now, log.LastOccurrence); // <-- TimeWarper!
}
}
}

...

public class ExceptionLogger
{
public ExceptionLog LogException(Exception ex)
{
ExceptionLog log = new ExceptionLog();
log.ExceptionType = ex.GetType().ToString();
log.Message = ex.Message;
log.NumOccurences = 1;
log.StackTrace = ex.StackTrace;
log.FirstOccurrence = TimeWarper.Now; //Tada.wav!
log.LastOccurrence = TimeWarper.Now; //Tada.wav!
return log;
}
}

There you have it.
Freezing time for unit tests is a great way to make them simpler. You can deal with DateTimes as a value type rather than having to declare multiple variables. Simply refer to any unique point in time you want to set or compare against as TimeWarper.Now.AddMinutes(x) 


Super mega quickstart for Unit Testing with Visual Studio .NET

Sunday 1 February 2009

Abstract:
  • This is a quickstart for setting up a testing environment in Visual Studio
  • Step 1 - Create a new class libary for your tests.
  • Step 2 - Add a reference to nunit.framework.dll
  • Step 3 - Get a test runner like the one in Resharper, TestMatrix or NUnit.
  • Step 4 - Start writing tests and run them with your test runner!
  • Step 7 - Here are some mocking frameworks: Rhino mocks, NMock, TypeMock, Moq
  • Step 10 - Profit!

For the benefit on any beginners stumbling across this blog, here is what you need to get started writing Unit Tests on the .NET platform. 

Step 1 - Create a new class library in Visual Studio 
Call it something along the lines of YourProjectNamespace.Testing

Step 2 - Download the NUnit framework.
The NUnit framework is pretty much the industry standard for running Unit Tests on the .NET platform.
Microsoft has created something called MSTest that nobody uses. To be honest I haven't looked into it very much and I see no reason to start now. :)




Then copy bin/nunit.framework.dll to your test project bin folder (that is: add it to your ThridPartyLibraries folder and create a reference to it in your testing project)

Step 3 - Get a test runner
To run unit tests you need a program to run them, preferably one integrated with Visual Studio.

I have found the test runner that comes with Resharper to be the nicest one out there right now.
Resharper is a plugin for Visual Studio that unfortunately is not free, but is probably one of the best investments you can make as a developer. Read all the hype on their homepage and give it a spin. 

If you don't want to use resharper to run your tests here are some alternatives:
TestMatrix - Another commercial tool that works.
NUnit - NUnit comes with a free test runner that is functional but crude. Only use it if you have to.

If none of these work for you use the power of google.

Step 4 - Create a unit test!

Wohoo, let's get cracking!
As an example, let's take the testing of a class validating the format of an email address.

Start by creating a file in your test project called EmailValidatorTests.cs

Copy and paste this code into the file:



using System;
using NUnit.Framework;

namespace OmgWtfTdd.Testing //You'll want to change this to match your project
{

[TestFixture] //This tells the test runner that this class contains tests
public class EmailValidatorTests
{

private EmailValidator _emailValidator;

[SetUp]
public void SetUp()
{
// The code in SetUp will be run before the execution of each unit test.
// Here we create things that will be used in multiple tests
_emailValidator = new EmailValidator();
}

[TearDown]
public void TearDown()
{
// The TearDown method is run after each test completes.
// Use it to tear down anything not caught by normal garbage collection
}

[Test] // <-- Look! A Test!
public void TestIsValidEmail_NoInput()
{
Assert.IsFalse(_emailValidator.IsEmailValid(String.Empty));
Assert.IsFalse(_emailValidator.IsEmailValid(null));
// Assert is a way of evaluating a condition
// If the Assert is not fullfilled the test fails.
// If an unexpected exception is thrown the test fails.
} // <-- If the test manages to get all the way to the end the test succeeds!

[Test]
public void TestIsValidEmail_TrueForValidEmails()
{
Assert.IsTrue(_emailValidator.IsEmailValid("erik@thisisnotmyrealemail.com"));
Assert.IsTrue(_emailValidator.IsEmailValid("erik@providerwithtwodots.co.uk"));
Assert.IsTrue(_emailValidator.IsEmailValid("erik.rydeman@dotsinthename.com"));
}

[Test]
public void TestIsValidEmail_Badformats()
{
Assert.IsFalse(_emailValidator.IsEmailValid("@noname.com"));
Assert.IsFalse(_emailValidator.IsEmailValid("noprovider@.com"));
Assert.IsFalse(_emailValidator.IsEmailValid("noprovidertall@"));
Assert.IsFalse(_emailValidator.IsEmailValid("nodomain@lol."));
Assert.IsFalse(_emailValidator.IsEmailValid("erik@domain.reallylongtopdomain"));
Assert.IsFalse(_emailValidator.IsEmailValid("wheredidtheatgo.com"));
}
}

/// <summary>
/// This class would normally be placed somewhere in your project.
/// The classes in the Testing project is for testing only and not used in production code.
/// </summary>
public class EmailValidator
{
public bool IsEmailValid(string email)
{
// This line is what you'd normally start out
// with writing your tests before your code:
//throw new NotImplementedException();

// I've cheated and started a bit. See if you can improve it!
return email.Contains("@");
}
}
}


That's a Unit Test for you! Actually that's three unit tests. Let's see if they work:

To run the tests with Resharper:
Right click somewhere in EmailValidatorTests and select "Run Unit Tests" from the menu.
Or: In the top menu select Resharper/Unit Testing/Run All Tests From Solution

To run the tests with the NUnit Test Runner:
Compile the Testing project, open the NUnit Test runner and find the dll for the testing project.
Run the tests.

The result should look like this:



In order for you to be able to test your environment I have made on of the tests succeed already. Note that following the principle of writing tests first you should always see it fail before you fix it.

As you can see the IsEmailValid method is quite incomplete. As an exercise, try making the two other tests succeed!

Step 7 - Get a mocking framework!

Since this post is about setting up your test environment let's skip ahead a few steps and briefly mention another thing you'd typically need to do in a project.

In any non trivial project you'll usually come accross the need to fake some classes in order to be able to test other classes. One way to do this is to create mock objects. We'll talk about this later, but here are some mocking frameworks you can use:
Rhino Mocks - My current favourite. Easy to use, powerful, strongly typed mocking.
NMock - Uses strings to specify which methods to mock. Not too elegant but very easy to understand.
TypeMock - Very powerful and useful but a bit magical. Requires integration into the VS environment which can sometimes cause problems.
Moq - Haven't tried it yet but it sure looks very nice.

They all pretty much do the same thing so use the one you like!
If you wouldn't know what to do with any of them just yet, don't worry. Start writing some regular tests and we'll tackle mocking later!

Step 10 - Profit!

There, if you've completed all the steps above you now have the power to write Unit Tests!
Use it wisely.