-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathUnitTestRunner.cs
More file actions
59 lines (53 loc) · 2.01 KB
/
UnitTestRunner.cs
File metadata and controls
59 lines (53 loc) · 2.01 KB
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
// Copyright (C) SomaSim LLC.
// Open source software. Please see LICENSE file for details.
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
namespace SomaSim
{
public class UnitTestRunner : MonoBehaviour
{
#if UNITY_EDITOR
public void Start () {
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Start();
int count = RunUnitTests();
timer.Stop();
Debug.Log("Unit tests: " + count + " tests ran in " + timer.Elapsed.TotalSeconds + " seconds");
}
private int RunUnitTests () {
int sum = 0;
foreach (Type testClass in GetTestClasses()) {
sum += RunTestMethods(Activator.CreateInstance(testClass));
}
return sum;
}
private int RunTestMethods (object testInstance) {
int sum = 0;
foreach (MethodInfo method in testInstance.GetType().GetMethods()) {
if (method.GetCustomAttributes(typeof(TestMethod), true).Length > 0) {
sum++;
try {
method.Invoke(testInstance, null);
} catch (UnitTestException e) {
Debug.LogError("UNIT TEST FAILURE in " + method.ToString() + "\n" + e.Message + "\n" + e.StackTrace);
} catch (Exception e) {
Debug.LogError("UNIT TEST ERROR in " + method.ToString() + "\n" + e.InnerException);
}
}
}
return sum;
}
private static IEnumerable<Type> GetTestClasses () {
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) {
foreach (Type type in assembly.GetTypes()) {
if (type.GetCustomAttributes(typeof(TestClass), true).Length > 0) {
yield return type;
}
}
}
}
#endif
}
}