Posts Tagged ‘Unit testing’

NavigateToTest VS2013 extension

Saturday, July 26th, 2014
Extension is available for other Visual Studio versions:

You can download the extension here:
NavigateToTest Visual Studio 2013 extension

Here’s the latest version that supports Visual Studio 2013.

Extension is convention based. It matches ClassName file with ClassNameTest or ClassNameTests and vice-versa, so you can easily navigate to the test file and back.

Here are some screenshots:

Here’s the toolbar and two opened files:

You can download the extension here:
NavigateToTest Visual Studio 2013 extension

Testing test assembly

Wednesday, October 10th, 2012

I recently discovered that almost 50 unit tests don’t run for Mail.dll email library test suite.

Mail.dll uses NUnit as its unit testing framework, and with over 3000 tests it’s hard to spot that some are missing. The problem was that most (of those 50 that didn’t run) test methods where missing TestAttribute and 2 test classes were missing TestFixtureAttribute.

I decided that it’s time to create 2 unit tests that guard my unit test assembly.

First one checks if all public methods, in classes marked with TestFixtureAttribute, have TestAttribute applied.

Of curse we are using reflection for that.

It would be nice if we could just double click on the trace output and be taken, by VisualStudio, to the source file. Unfortunately it’s not possible, with standard .NET reflection, to get source code file name and line. I decided against using Mono.Cecil for that, especially that with R# it’s easy enough to navigate to method name.

[Test]
public void Methods_InTestFixtureTypes_HaveTestAttribute()
{
    int count = 0;
    foreach (Type type in this.GetType().Assembly.GetTypes())
    {
        if (Attribute.IsDefined(type, typeof(TestFixtureAttribute)))
        {
            foreach (MethodInfo method in type.GetMethods())
            {
                if (method.IsPublic
                    && method.IsSpecialName == false
                    && method.DeclaringType == type
                    && !Attribute.IsDefined(method, typeof(TearDownAttribute))
                    && !Attribute.IsDefined(method, typeof(SetUpAttribute))
                    && !Attribute.IsDefined(method, typeof(TestAttribute))
                    )
                {
                    Trace.WriteLine(type.FullName + "." + method.Name);
                    count++;
                }
            }
        }
    }
    Assert.AreEqual(0, count);
}

Second test checks if all public classes, that have at least one method marked with TestAttribute, have TestFixtureAttribute applied.

[Test]
public void Types_WithTestMethods_HaveTestFixtureAttribute()
{
    int count = 0;
    foreach (Type type in this.GetType().Assembly.GetTypes())
    {
        bool hasAtLeastOneTestMethod = new List<MethodInfo>(type.GetMethods()).Find(
            method => Attribute.IsDefined(method, typeof(TestAttribute))) != null;

        if (hasAtLeastOneTestMethod
            && Attribute.IsDefined(type, typeof(TestFixtureAttribute)) == false)
        {
            Trace.WriteLine(type.FullName);
            count++;
        }
    }
    Assert.AreEqual(0, count);
}

Some of you might argue that we shouldn’t be using Trace class inside the unit test. StringBuilder, WriteLine and
Assert.AreEqual(object expected, object actual, string message) are your friends in such case.

NavigateToTest VS2012 extension

Sunday, August 26th, 2012
Extension is available for other Visual Studio versions:

You can download the extension here:
NavigateToTest Visual Studio 2012 extension

Here’s the latest version that supports Visual Studio 2012.

Extension is convention based. It matches ClassName file with ClassNameTest or ClassNameTests and vice-versa, so you can easily navigate to the test file and back.

Here are some screenshots:

Here’s the toolbar and two opened files:

You can download the extension here:
NavigateToTest Visual Studio 2012 extension

NavigateToTest VS extension

Monday, May 3rd, 2010
Extension is available for other Visual Studio versions:

You can download the extension here:
NavigateToTest Visual Studio extension

Recently I decided to create a Visual Studio 2010 extension.

Extension is convention based. It matches ClassName file with ClassNameTest or ClassNameTests and vice-versa, so you can easily navigate to the test file and back.

Here are some screenshots:

You’ll need to add toolbar manually:

Here’s the toolbar:

And two opened files:

You can download the extension here:
NavigateToTest Visual Studio extension

Testing DateTime.Now

Monday, February 1st, 2010

When you are doing Test Driven Development (TDD) many time during the testing phase you’ll find yourself with a code that depends on current time.

It’s not possible write tests for this code as DateTime.Now changes constantly.

Let’s take a look at following method:

public string CreateName(...)
{
   return "name " + DateTime.Now;
}

The solution to this problem is simple. We’ll introduce Clock class with the same interface as DateTime:

public string CreateName(...)
{
   return "name " + Clock.Now;
}

We’d like the test to look something like this:

[Test]
public void CreateName_AddsCurrentTimeAtEnd()
{
    using (Clock.NowIs(new DateTime(2010, 12, 31, 23, 59, 00)))
    {
        string name = new ReportNameService().CreateName(...);
        Assert.AreEqual("name 2010-12-31 23:59:00", name);
    }
}

Tests should not leave any side effects. This is why we are using IDisposable pattern. After text execution Clock.Now is reverted and again returns current time.

Finally this is how Clock class looks like:

public class Clock : IDisposable
{
    private static DateTime? _nowForTest;

    public static DateTime Now
    {
        get { return _nowForTest ?? DateTime.Now; }
    }

    public static IDisposable NowIs(DateTime dateTime)
    {
        _nowForTest = dateTime;
        return new Clock();
    }

    public void Dispose()
    {
        _nowForTest = null;
    }
};