Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
88 lines (70 sloc) 2.72 KB
using System;
using System.Threading;
using Xunit.Runners;
namespace TestRunner
{
class Program
{
// We use consoleLock because messages can arrive in parallel, so we want to make sure we get
// consistent console output.
static object consoleLock = new object();
// Use an event to know when we're done
static ManualResetEvent finished = new ManualResetEvent(false);
// Start out assuming success; we'll set this to 1 if we get a failed test
static int result = 0;
static int Main(string[] args)
{
if (args.Length == 0 || args.Length > 2)
{
Console.WriteLine("usage: TestRunner <assembly> [typeName]");
return 2;
}
var testAssembly = args[0];
var typeName = args.Length == 2 ? args[1] : null;
using (var runner = AssemblyRunner.WithAppDomain(testAssembly))
{
runner.OnDiscoveryComplete = OnDiscoveryComplete;
runner.OnExecutionComplete = OnExecutionComplete;
runner.OnTestFailed = OnTestFailed;
runner.OnTestSkipped = OnTestSkipped;
Console.WriteLine("Discovering...");
runner.Start(typeName);
finished.WaitOne();
finished.Dispose();
return result;
}
}
static void OnDiscoveryComplete(DiscoveryCompleteInfo info)
{
lock (consoleLock)
Console.WriteLine($"Running {info.TestCasesToRun} of {info.TestCasesDiscovered} tests...");
}
static void OnExecutionComplete(ExecutionCompleteInfo info)
{
lock (consoleLock)
Console.WriteLine($"Finished: {info.TotalTests} tests in {Math.Round(info.ExecutionTime, 3)}s ({info.TestsFailed} failed, {info.TestsSkipped} skipped)");
finished.Set();
}
static void OnTestFailed(TestFailedInfo info)
{
lock (consoleLock)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[FAIL] {0}: {1}", info.TestDisplayName, info.ExceptionMessage);
if (info.ExceptionStackTrace != null)
Console.WriteLine(info.ExceptionStackTrace);
Console.ResetColor();
}
result = 1;
}
static void OnTestSkipped(TestSkippedInfo info)
{
lock (consoleLock)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("[SKIP] {0}: {1}", info.TestDisplayName, info.SkipReason);
Console.ResetColor();
}
}
}
}