Machine.Specifications (mspec) only output errors

Our team is very command line oriented, we’ll often run our test suit from the command line.  A typical workflow would be to commit, pull build then push (assuming we didn’t break the build!).

You’ll notice that when you run mspec from the command line you get a lot of output.  You can turn off ALL output other than the summary at the end but that doesn’t help when you want to know which tests have failed.

We use psake for our build, Adrian has already posted about integrating mspec with psake.  We made the following tweak to our build to only show test failures (we parse the XML output generated by mspec):

task MSpecUnitTests -depends Compile -description "MSpec unit tests" {
  $testDlls = ls "$srcDir\*\bin\$buildConfiguration" -rec `
    | where { $_.Name.EndsWith(".Tests.dll") } `
    | where { (Test-Path ([System.IO.Path]::GetDirectoryName($_.FullName) + "\Machine.Specifications.dll")) -eq $True } `
    | foreach { $_.FullName }

  $mspecExePath = Join-Path $packagesDir "Machine.Specifications.0.5.8\tools\mspec-clr4.exe";

  if($env:TEAMCITY_VERSION -ne $Null)
  {
      exec{ & $mspecExePath $testDlls --teamcity }
  }
  else
  {
    $mspecOutputPath = Join-Path $buildDir 'mspec.xml'
    try{
      exec{ & $mspecExePath $testDlls -s --xml $mspecOutputPath }
    }
    catch {
      [xml]$testOutput = Get-Content $mspecOutputPath
      Select-Xml "//context[specification/@status='failed']"; $testOutput | % {
        Write-Host "> " -NoNewLine
        Write-Host $_.Node.name -NoNewLine -foregroundcolor "yellow"
        Write-Host " failed the following specifications:"
        foreach ($spec in $_.Node.specification) {
          if ($spec.status -eq 'failed') {
            Write-Host ">> " -NoNewLine
            Write-Host $spec.name -NoNewLine
            Write-Host " (" -NoNewLine
            Write-Host $spec.error.message.innerText -NoNewLine -foregroundcolor "red"
            Write-Host ")"
          }
        }
      }
    }
  }
}
Advertisements

About Tom Peplow

C# .Net developer based in London and the South Coast
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

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