forked from xunit/samples.xunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
87 lines (70 loc) · 2.72 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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();
}
}
}
}