/
Program.cs
129 lines (108 loc) · 3.86 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace JilTests.Core
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine($"Starting test run: {DateTime.UtcNow:u}");
#if DEBUG
Console.WriteLine("\tDEBUG");
#endif
#if RELEASE
Console.WriteLine("\tRELEASE");
#endif
#if NETCORE
Console.WriteLine("\tNETCORE");
#endif
#if EXHAUSTIVE_TEST
Console.WriteLine("\tEXHAUSTIVE_TEST");
#endif
var asm = Assembly.GetEntryAssembly();
var testGroups = asm.GetTypes();
var ran = 0;
var passed = 0;
var failed = new HashSet<string>();
foreach(var group in testGroups.OrderBy(t => t.Name))
{
var isTestClass = group.GetTypeInfo().CustomAttributes.Any(c => c.AttributeType == typeof(Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute));
if (!isTestClass) continue;
var cons = group.GetConstructor(Type.EmptyTypes);
var first = true;
var mtds = group.GetMethods().OrderBy(m => m.Name);
foreach(var mtd in mtds)
{
var isTestMethod = mtd.CustomAttributes.Any(c => c.AttributeType == typeof(Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute));
if (!isTestMethod) continue;
if (first)
{
Console.WriteLine("Starting Group: " + group.Name);
first = false;
}
Console.Write("\t" + mtd.Name + ": ");
var res = Run(cons, mtd);
Console.Write(res.Runtime.TotalMilliseconds + "ms");
Console.WriteLine(res.Passed ? " passed" : " !FAILED!");
if (!res.Passed)
{
Console.WriteLine(res.ErrorMessage);
Console.WriteLine(res.StackTrace);
failed.Add(group.Name + "." + mtd.Name);
}
ran++;
if (res.Passed) passed++;
}
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(passed + "/" + ran + " passsed");
Console.WriteLine();
if(failed.Count > 0)
{
Console.WriteLine("Failed " + failed.Count + ":");
foreach(var fail in failed.OrderBy(_ => _))
{
Console.WriteLine("\t" + fail);
}
}
Console.WriteLine($"Test run completed: {DateTime.UtcNow:u}");
Console.ReadKey();
}
class TestResult
{
public bool Passed => ErrorMessage == null;
public TimeSpan Runtime { get; set; }
public string ErrorMessage { get; set; }
public string StackTrace { get; set; }
}
static TestResult Run(ConstructorInfo makeTestClass, MethodInfo mtd)
{
var inst = makeTestClass.Invoke(new object[0]);
var watch = new Stopwatch();
watch.Start();
Exception failure;
try
{
mtd.Invoke(inst, new object[0]);
failure = null;
}
catch (Exception e)
{
failure = e;
}
watch.Stop();
return
new TestResult
{
Runtime = watch.Elapsed,
ErrorMessage = failure?.Message,
StackTrace = failure?.StackTrace
};
}
}
}