Posts Tagged: linqpad

Unit Testing in Linqpad

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