Unit Testing in Linqpad

Ancient Knowledge

This article is getting old. It was written in the ancient times and the world of software development has changed a lot since then. I'm keeping it here for historical purposes, but I recommend you check out the newer articles on my site.

Linqpad is great for writing a quick test or exploring some api features. If you need to write a group of tests, you can use this quick little snippet to run Linqpad like a unit test.

public interface IRunner { void Execute(); }

// Add any unit tests by implementing IRunner and Linqpad will run it
public class UserTest_1 : IRunner
{
    public void Execute()
    {
        Debug.WriteLine("Beginning test");

        var x = 4 + 5;

        Debug.Assert(x == 9, "Test failed!");
    }
}

// Linqpad main method
void Main()
{
    var tests = typeof(UserQuery)
        .GetNestedTypes()
        .Where(type => typeof(IRunner)
        .IsAssignableFrom(type) && !type.IsInterface);

    foreach(var t in tests){
        (Activator.CreateInstance(t) as IRunner).Execute();
    }
}

Also available as a gist