Attaching Gallio test reports to TeamCity using MSBuild

This is just one of those things that took a while to figure out, and there wasn’t much information about it on the internet. So I’m adding that information to the internet now.

I have a large suite of automated GUI tests. I wanted them to take screenshots on failure and show the results on the TeamCity dashboard.

Capturing screenshots from tests using Gallio

First, the Gallio stuff. If you’re using Gallio (formerly called MbUnit) to drive your tests, you can take screenshots using the following C# code. We wrapped it in a try-catch to prevent screenshot failure from interfering with the rest of the test teardown (for us, it’s not that big a deal if the screenshots fail). Sorry about the rubbish formatting, my WordPress plugin is just not playing nice with me on this one.

public static void CaptureScreenOnFailure(String name)
{
  try
  {
    if (TestContext.CurrentContext.IsTriggerEventSatisfied(TriggerEvent.TestFailed))
    {
      TestLog.EmbedImage(name, Capture .Screenshot());
    }
  }
  catch { }
}

Important: If the workstation is locked at the time the screen capture tries to do its thing, this screen capture will fail. We had this issue on our Windows XP virtual machines. We worked around this by changing the Windows settings so that the machines never auto-locked.

Saving reports to a specific directory using MSBuild

If you are using MSBuild to run your tests, just add ReportDirectory and ReportTypes attributes to the MSBuild script. Gallio saves a report to a default directory, but finding that directory on your local disk is a royal pain. It’s much easier just to set the directory to someplace more convenient for you. If you are going to use TeamCity or similar to copy the report, make sure it saves the report to a directory that is visible to that service.

<RemoveDir Directories=" $(TestResultsDirectory)" />;
<Gallio IgnoreFailures=" true" Files=" @(TestFiles)"
<strong>ReportDirectory=" $(TestResultsDirectory)"
</strong><strong>ReportTypes=" html">
</strong><Output TaskParameter=" ExitCode" PropertyName=" ExitCode"/>
</Gallio>

<RemoveDir Directories=" $(TestResultsDirectory)" /><Gallio IgnoreFailures=" true" Files=" @(TestFiles)" ReportDirectory=" $(TestResultsDirectory)"  ReportTypes=" html"><Output TaskParameter=" ExitCode" PropertyName=" ExitCode"/></Gallio>

It’s a good idea to add a RemoveDir command before the Gallio command, in order to clean up the old directory before creating the new one. Don’t worry, the old files will be copied across to TeamCity anyway.

Adding reports as an artifact to TeamCity builds

For TeamCity, go to the build settings for your test suite. Edit General Settings, and add the following line to “Artifact paths”. The artifact directory should be the same as the ReportDirectory value that you put in the MSBuild script. The directory to copy to on TeamCity can be pretty much whatever you want.

(Artifact directory) => (Directory to copy to on TeamCity server)
e.g.
\\SharedLocation\GallioReport\ => TestReport

You may want to edit the cleanup settings under Administration -> Build History Clean-up so that old artifacts are cleaned out regularly, as the screenshot images can potentially take up quite a bit of space.

And that’s it. Now after your test suite runs, there should be a link next to the build result called “Artifacts”. This should contain the latest test report, with all your screenshots.

4 thoughts on “Attaching Gallio test reports to TeamCity using MSBuild

  1. Pingback: Tweets that mention Attaching Gallio test reports to TeamCity using MSBuild | Purple Box Testing -- Topsy.com

  2. Trish Khoo Post author

    Unfortunately I can’t provide screenshots because since writing this we’ve moved from Gallio to Nunit and we use a different reporting mechanism.

  3. Julian

    Curious re your reply to a comment- moving from Mbunit to Nunit- can you share why?

    I’m thinking of doing the opposite; Gallio looks very tempting- especially it’s ability to capture video of failing tests.

    Thanks in advance!

Leave a Reply