Skip to content

Instantly share code, notes, and snippets.

@JohannesRudolph
Last active December 16, 2015 12:18
Show Gist options
  • Save JohannesRudolph/5433368 to your computer and use it in GitHub Desktop.
Save JohannesRudolph/5433368 to your computer and use it in GitHub Desktop.
Simple teamcity test status reporter for for nunit.lite
public class TeamcityTestReporter : ITestReporter
{
TextWriter writer;
public TeamcityTestReporter( System.IO.TextWriter writer )
{
this.writer = writer;
}
public void TestSuiteStarted( NUnit.Framework.Internal.TestSuite ts )
{
bool isRootSuite = ts.Parent == null;
if (isRootSuite)
writer.WriteLine( "##teamcity[testSuiteStarted name='{0}']", GetSuiteName( ts ) );
}
public void TestSuiteFinished( NUnit.Framework.Internal.TestSuite ts )
{
bool isRootSuite = ts.Parent == null;
if (isRootSuite)
writer.WriteLine( "##teamcity[testSuiteFinished name='{0}']", GetSuiteName( ts ) );
}
static string GetSuiteName( TestSuite ts )
{
var suiteName = ts.FullName;
if (!String.IsNullOrEmpty( suiteName ))
return suiteName;
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
}
public void TestStarted( NUnit.Framework.Api.ITest test )
{
if (test is TestSuite)
return;
writer.WriteLine( "##teamcity[testStarted name='{0}' captureStandardOutput='true']", test.FullName );
}
public void TestFinished( NUnit.Framework.Api.ITestResult result )
{
if (result.Test is TestSuite)
return;
switch (result.ResultState.Status)
{
case TestStatus.Passed:
writer.WriteLine( "##teamcity[testFinished name='{0}' duration='{1}']", result.Test.FullName, result.Time );
break;
case TestStatus.Failed:
case TestStatus.Inconclusive:
writer.WriteLine( "##teamcity[testFailed name='{0}' message='{1}' details='{2}' duration='{3}']", result.Test.FullName, EscapeTeamcityString( result.Message ), EscapeTeamcityString( result.StackTrace ), result.Time );
break;
case TestStatus.Skipped:
writer.WriteLine( "##teamcity[testIgnored name='{0}' message='{1}']", result.Test.FullName, result.Message );
break;
default:
break;
}
}
public void TestOutput( NUnit.Framework.Api.TestOutput testOutput )
{
// just write, teamcity captures stdout
writer.WriteLine( testOutput.Text );
}
static string EscapeTeamcityString( string original )
{
StringBuilder sb = new StringBuilder( original );
sb.Replace( "|", "||'" );
sb.Replace( "\n", "|n" );
sb.Replace( "\r", "|r" );
sb.Replace( "[", "|[" );
sb.Replace( "]", "|]" );
return sb.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment