Writing windows services smoke test form

I work often with windows services, and one of the most painfully experience is that you do not have an UI and quite often they have to do some scheduled task at certain time, so whenever you have a deploy you can find yourself in this situation.

image

The question mark means that you are not sure if your new deploy is really ok, because maybe some of the scheduled tasks will fail for misconfiguration or something else. Usually production machine are different to developement ones, you can miss components, point to a wrong db, forget to update configuration, etc, etc. To avoid this situation you need a series of smoke checks, I usually proceed in this way.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public class TestResult
{
    public Boolean Result { get; set; }
    public String Comment { get; set; }

    public static readonly TestResult Ok = new TestResult() {Result = true};
}

interface ITest
{
    TestResult Execute();
    String Name { get; }
}

This is the interface of the a component that is able to do a test, I can now write stuff like this

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class ResolveIoCTest: ITest
{
    #region ITest Members

    public TestResult Execute()
    {
        IoC.Resolve<IMYInterface>("xxxxx");
        return TestResult.Ok;
    }

    public string Name
    {
        get { return "ResolveIoCTest"; }
    }

    #endregion 
}

Just to verify that I’m able to resolve a specific interface or a specific component, but you can actually verify every stuff you like, then I write a simple form with a button and a ritch text box.

The next step is finding in the assembly all test classes

1
2
3
4
5
6
7
8
Type testInterfaceType = typeof(ITest);
IoC.FluentRegistration(
    AllTypes.Pick()
   .FromAssembly(
        Assembly.GetExecutingAssembly())
   .If(testInterfaceType.IsAssignableFrom)
   .Configure(reg => reg.LifeStyle.Transient)
   .WithService.FirstInterface());

Thanks to fluent registration it is easy to autoregister all test classes, now the test form simply call IoC.ResolveAll<ITest> and for each test execute it in a Try catch, and here is a typical result.

image

Some exception occurred.

Now each time I write a component that needs verification, I write a simple test, then each time I deploy I runs this form and verify that I do not miss anything. This saves me pain, because I know that at least a set of smoke tests are passing correctly.

alk.

Tags: Software Architecture