-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest-runner.cake
81 lines (67 loc) · 2.81 KB
/
test-runner.cake
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
// ***********************************************************************
// Copyright (c) Charlie Poole and contributors.
// Licensed under the MIT License. See LICENSE.txt in root directory.
// ***********************************************************************
#load ./assertions.cake
#load ./constraints.cake
using System.Reflection;
//////////////////////////////////////////////////////////////////////
// A tiny test framework for use in cake scripts.
//
// NOTE: This is an experiment, so it currently only handles a very
// limited set of constraints and assertions needed by this project.
// Eventiually, I'll combine various experiments in this and other
// projects to a Cake version of TC-Lite.
//////////////////////////////////////////////////////////////////////
public static class TestRunner
{
public static void Run(params Type[] types)
{
Assert.FailureCount = 0;
foreach (Type type in types)
{
Console.WriteLine($"\n=> {type.Name}");
int testCount = 0;
int failCount = 0;
foreach (var method in type.GetMethods())
{
if (!method.IsDefined(typeof(TestAttribute)))
continue;
Assert.FailureMessages.Clear();
testCount++;
try
{
var obj = !type.IsStatic() ? System.Activator.CreateInstance(type) : null;
method.Invoke(obj, new object[0]);
}
catch (Exception ex)
{
if (ex is TargetInvocationException)
ex = ex.InnerException;
Assert.ReportFailure(ex.Message);
}
if (Assert.FailureMessages.Count == 0)
{
Console.WriteLine($" => {method.Name}");
}
else
{
failCount++;
Console.WriteLine($" => {method.Name} FAILED!");
foreach (string message in Assert.FailureMessages)
Console.WriteLine($" {message}");
Assert.FailureMessages.Clear();
}
}
bool failed = Assert.FailureCount > 0;
string runResult = failed ? "FAILED" : "PASSED";
Console.WriteLine($"\nTest Run Summary - {runResult}");
Console.WriteLine($" Tests: {testCount}, Passed: {testCount - failCount}, Failed: {failCount}");
Console.WriteLine($" Asserts: {Assert.AssertCount}, Passed: {Assert.SuccessCount}, Failed: {Assert.FailureCount}\n");
if (failed)
throw new System.Exception();
}
}
}
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class TestAttribute : Attribute { }