Geeks With Blogs
// ThomasWeller C#/.NET software development, software integrity, life as a freelancer, and all the rest

Yesterday I noticed that a new version of Gallio, my favorite Test and Automation platform, was released a few days ago. Although the official version number suggests only a small progression (we are coming from v.3.0.6 SR2 and now have v3.1), it is a major upgrade that brings not only some minor improvements, but a wealth of new features. Among the highlights are:

  • VS 2010 Beta 1 and .NET 4.0 support
  • full RSpec integration (used for Behavior Driven Development)
  • a new plug-in model makes it very easy to write your own custom test framework

Refer to the release notes for a more detailed list and a comprehensive overview.

When playing around with the new version for the first time these two features attracted me the most:

Automatic screenshots and videos in test reports

The single most attracting feature certainly is the ability to automatically include screenshots and video captures with the test reports. The automatic embedding can be restricted to specific test outcomes, like e.g. failures.

This sample fixture will include a screenshot in the test report for every test step that may fail:

[TestFixture]

public class ScreenShotFixture

{

    [SetUp]

    public void SetUp()

    {

        Capture.SetCaptionAlignment(HorizontalAlignment.Center,

                                    VerticalAlignment.Bottom);

        Capture.SetCaptionFontSize(32);

        Capture.AutoEmbedScreenshot(TriggerEvent.TestFailed, "screenshot",

                                    new CaptureParameters { Zoom = 0.5 });

    }

 

    [Test]

    [Row(1, 2, 3)]

    [Row(1, 2, 4)]

    public void SimpleAdditionWithScreenShot(int x, int y, int result)

    {

 

        Capture.SetCaption(string.Format("{0} + {1} does not have the expected " +

                                         "result of {2}", x, y, result));

        Assert.AreEqual(result, x + y);

    }

}

You can do that even with videos, having generated test reports like this:

TestReportWithVideo

This is the test fixture that automatically embeds a screen video for every failed test, using  a bit of White UI automation to test against Windows calculator:

[TestFixture]

public class WinCalcFixture

{

    private Application application;

    private Window window;

 

    [FixtureSetUp]

    public void FixtureSetUp()

    {

        this.application = Application.Launch("calc");

        this.window = application.GetWindows()[0];

    }

 

    [FixtureTearDown]

    public void FixtureTearDown()

    {

        this.application.Kill();

    }

 

    [SetUp]

    public void SetUp()

    {

        Capture.SetCaptionAlignment(HorizontalAlignment.Center, VerticalAlignment.Top);

        Capture.SetCaptionFontSize(32);

        Capture.AutoEmbedRecording(TriggerEvent.TestFailed,

                                   "screenvideo",

                                    new CaptureParameters { Zoom = 0.5 },

                                    5 /*frames per second*/);

    }

 

    [Test]

    [Row(1, 2, 3)]

    [Row(1, 2, 4)]

    public void SimpleAdditionWithCalculatorVideo(int x, int y, int result)

    {

        Capture.SetCaption(string.Format("{0} + {1} does not have the expected result of {2}",

                                         x, y, result));

 

        this.window.Get<Button>(x.ToString()).Click();

        this.window.Get<Button>("+").Click();

        this.window.Get<Button>(y.ToString()).Click();

        this.window.Get<Button>("=").Click();

 

        string resultFromCalculator = this.window.Get<TextBox>().Text.Substring(0, 1);

 

        Assert.AreEqual(result.ToString(), resultFromCalculator);

    }

 

} // class WinCalcFixture

The example is deliberately silly and simple, but I can clearly see how this feature will be a great help when it comes to large, complex user interfaces (the release notes show a similar example utilizing the WatiN web UI automation framework). You automatically will have a screen capture at hand that shows the failure - no more troublesome mimicking of the test case, and no more playing around with unfamiliar screen recording tools...

My personal favorite: the new AccessorContract verifier.

While the above admittedly is really sexy and impressive, it is not of so much help in everyday TDD-groundwork (except for some kind of UI integration tests, of course).

Therefore, I'd like to present here something that is somewhat unnoticeable at first glance (in fact, it is not even mentioned in the release notes), but promises to become a big time-saver in everyday (TDD-) life. Until now, you had to write quite a handful of unit tests to accurately cover some simple properties with all their possible corner-cases and exceptions, like the ones in this class:

public class Person

{

    private string name;

    private int age;

 

    public string Name

    {

        get { return this.name; }

        set

        {

            if (value == null)

            {

                throw new ArgumentNullException("value");

            }

            if (value == "")

            {

                throw new ArgumentException("Name must not be empty.", "value");

            }

            if (value.Length > 50)

            {

                throw new ArgumentException("Name must not be longer than 50 characters.",

                                            "value");

            }

            this.name = value;

         }

    }

 

    public int Age

    {

        get { return this.age; }

        set

        {

            if (value < 18 || value > 65)

            {

                throw new ArgumentOutOfRangeException("value", "Age must be between 18 and 65.");

            }

            this.age = value;

        }

    }

}

With MbUnit v3.1, the checking becomes as easy as this:

[TestFixture]

public class Fixture

{

    [VerifyContract]

    public readonly IContract TestName = new AccessorContract<Person, string>

    {

        PropertyName = "Name",

        AcceptNullValue = false,

        ValidValues = { "SomeName", new string('x', 50) },

        InvalidValues =

        {

            { typeof(ArgumentException), "", new string('x', 51) }

        }

    };

 

    [VerifyContract]

    public readonly IContract TestAge = new AccessorContract<Person, int>

    {

        PropertyName = "Age",

        ValidValues = { 18, 65 },

        InvalidValues =

        {

            { typeof(ArgumentOutOfRangeException), 17, 66 }

        }

    };

}

As you can see, the new AccessorContract verifier lets you easily specify all the test values that you want to have executed against a specific property, together with the exception type that you expect to be thrown;  it also allows for null-value checks. While this may not be very impressive in technical terms, I think it's very useful in practical ones.

 

Happy unit testing...

Posted on Friday, September 18, 2009 9:55 AM Unit Testing/TDD , Automation , Open Source | Back to top


Comments on this post: Gallio 3.1 is here

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Thomas Weller | Powered by: GeeksWithBlogs.net