Getting a usable errorlevel from MbUnit

I run the MbUnit console program from my command line build process and I want to halt the build process if a test fails. Unfortunately the console program doesn’t appear to return any error code for the version I have (not the latest). To fix that I created a quick and dirty perl script to run it and read the output for the failures. The script also abbreviates some of the output because I didn’t want to clutter up the console too much.

#!/usr/bin/perl

use strict;

my @assemblies = @ARGV;

my $ass = join " ", @assemblies;
my $dir = $ENV{'ProgramFiles(x86)'};
if(!$dir)
{
	$dir = $ENV{'ProgramFiles'} 
}
my $program = $dir . '\MbUnit\MbUnit.Cons.exe';
if(!-f $program)
{
	$program = $dir . '\MbUnit\bin\MbUnit.Cons.exe';
}
my $command = '"' . $program . '" /v ' . $ass;
#print $command, "\n";

open CMD, $command . "|";
my @lines = <CMD>;
close CMD;
foreach my $line (@lines)
{
	if($line =~ /Loading test assemblies/)
	{
	}
	elsif($line =~ /Loading \w+/ || $line =~ /failure/)
	{
		print $line;
	}
	if($line =~ /Tests finished: (\d+) tests, (\d+) success, (\d+) failures/)
	{
		print $line;
		if($3 > 0)
		{
			exit 1;
		}
	}
}


It’s then a simple matter of running your tests from a batch file and aborting if the tests fail,

...
echo Running unit tests
echo.
run_tests.pl UnitTests\bin\%config%\UnitTests.dll WinFormsUnitTests\bin\%config%\WinFormsUnitTests.dll 
if ERRORLEVEL 1 goto :error
...

About these ads

2 thoughts on “Getting a usable errorlevel from MbUnit

  1. Jeff Brown says:

    IIRC MbUnit.Cons from MbUnit v2 does return a couple of different error codes in case there are failures, bad arguments, no tests, whatever. Certainly Gallio.Echo does since the mechanism was behavior copied over.

    Anyways, nifty script. :-)

  2. colinnewell says:

    Thanks.

    I’m using the 2.4 version that came out before the very latest release. I tried probing it for errorlevel’s when I used it but I appeared to get nothing either way. I’ll perhaps have a look in the code to see if I’m missing something.

    I figured this script would be redundant for v3. We just haven’t moved on yet because 2.4 has been doing us fine.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 58 other followers