|
1 | 1 | using FlaxEditor; |
2 | 2 | using FlaxEditor.GUI; |
| 3 | +using FlaxEngine; |
3 | 4 | using System; |
4 | 5 | using System.Collections.Generic; |
5 | 6 | using System.Linq; |
6 | 7 | using System.Reflection; |
| 8 | +using FlaxCommunity.UnitTesting; |
7 | 9 |
|
8 | | -namespace FlaxEngine.UnitTesting.Editor |
| 10 | +namespace FlaxCommunity.UnitTesting.Editor |
9 | 11 | { |
10 | 12 | public class TestRunner : EditorPlugin |
11 | 13 | { |
12 | | - private static List<Type> suites = new List<Type>(); |
13 | | - private MainMenuButton mmBtn; |
| 14 | + private static readonly List<Type> _suites = new List<Type>(); |
| 15 | + private MainMenuButton _mmBtn; |
14 | 16 |
|
15 | 17 | public override PluginDescription Description => new PluginDescription |
16 | 18 | { |
17 | 19 | Author = "Lukáš Jech", |
18 | | - AuthorUrl ="https://lukas.jech.me", |
| 20 | + AuthorUrl = "https://lukas.jech.me", |
19 | 21 | Category = "Unit Testing", |
20 | 22 | Description = "Simple unit testing framework", |
21 | 23 | IsAlpha = false, |
22 | 24 | IsBeta = false, |
23 | 25 | Name = "Simple Unit Testing", |
24 | | - SupportedPlatforms = new PlatformType[] {PlatformType.Windows}, |
25 | | - Version = new Version(1,0), |
26 | | - RepositoryUrl = "https://github.com/klukule/flax-ut" |
| 26 | + SupportedPlatforms = new PlatformType[] { PlatformType.Windows }, |
| 27 | + Version = new Version(1, 1), |
| 28 | + RepositoryUrl = "https://github.com/FlaxCommunityProjects/FlaxUnitTesting" |
27 | 29 | }; |
28 | | - |
| 30 | + |
29 | 31 | public override void InitializeEditor() |
30 | 32 | { |
31 | 33 | base.InitializeEditor(); |
32 | 34 |
|
33 | | - mmBtn = Editor.UI.MainMenu.AddButton("Unit Tests"); |
34 | | - mmBtn.ContextMenu.AddButton("Run unit tests").Clicked += RunTests; |
| 35 | + _mmBtn = Editor.UI.MainMenu.AddButton("Unit Tests"); |
| 36 | + _mmBtn.ContextMenu.AddButton("Run unit tests").Clicked += RunTests; |
| 37 | + FlaxEditor.Scripting.ScriptsBuilder.ScriptsReloadBegin += ScriptsBuilder_ScriptsReloadBegin; |
| 38 | + } |
35 | 39 |
|
| 40 | + private void ScriptsBuilder_ScriptsReloadBegin() |
| 41 | + { |
| 42 | + // Clear type information as per warning https://docs.flaxengine.com/manual/scripting/plugins/index.html |
| 43 | + _suites.Clear(); |
36 | 44 | } |
37 | 45 |
|
38 | 46 | public override void Deinitialize() |
39 | 47 | { |
40 | 48 | base.Deinitialize(); |
41 | | - if (mmBtn != null) |
| 49 | + if (_mmBtn != null) |
42 | 50 | { |
43 | | - mmBtn.Dispose(); |
44 | | - mmBtn = null; |
| 51 | + _mmBtn.Dispose(); |
| 52 | + _mmBtn = null; |
45 | 53 | } |
46 | 54 | } |
47 | 55 |
|
48 | 56 | private static void GatherTests() |
49 | 57 | { |
50 | 58 | Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); |
51 | | - suites.Clear(); |
| 59 | + _suites.Clear(); |
52 | 60 | foreach (var assembly in assemblies) |
53 | 61 | foreach (var type in assembly.GetTypes()) |
54 | 62 | if (type.GetCustomAttributes<TestFixture>().Count() > 0) |
55 | | - suites.Add(type); |
| 63 | + _suites.Add(type); |
56 | 64 | } |
57 | 65 |
|
58 | 66 | public static void RunTests() |
59 | 67 | { |
60 | 68 | GatherTests(); |
61 | 69 |
|
62 | | - foreach (var suite in suites) |
| 70 | + foreach (var suite in _suites) |
63 | 71 | { |
64 | | - var tests = suite.GetMethods().Where(m => m.GetCustomAttributes<TestCase>().Count() > 0 || m.GetCustomAttributes<Test>().Count() > 0).ToArray(); |
65 | | - var setup = suite.GetMethods().Where(m => m.GetCustomAttributes<SetUp>().Count() > 0).FirstOrDefault(); |
66 | | - var disposer = suite.GetMethods().Where(m => m.GetCustomAttributes<TearDown>().Count() > 0).FirstOrDefault(); |
| 72 | + var suiteMethods = suite.GetMethods(); |
| 73 | + |
| 74 | + var tests = suiteMethods.Where(m => m.GetCustomAttributes<Test>().Count() > 0 || m.GetCustomAttributes<TestCase>().Count() > 0).ToArray(); |
| 75 | + var setup = suiteMethods.Where(m => m.GetCustomAttributes<OneTimeSetUp>().Count() > 0).FirstOrDefault(); |
| 76 | + var disposer = suiteMethods.Where(m => m.GetCustomAttributes<OneTimeTearDown>().Count() > 0).FirstOrDefault(); |
| 77 | + var beforeEach = suiteMethods.Where(m => m.GetCustomAttributes<SetUp>().Count() > 0).FirstOrDefault(); |
| 78 | + var afterEach = suiteMethods.Where(m => m.GetCustomAttributes<TearDown>().Count() > 0).FirstOrDefault(); |
67 | 79 |
|
68 | 80 | var instance = Activator.CreateInstance(suite); |
69 | 81 |
|
70 | 82 | setup?.Invoke(instance, null); |
71 | 83 |
|
72 | 84 | foreach (var testMethod in tests) |
73 | 85 | { |
74 | | - // Mitigates the AttributeNullException |
75 | | - foreach (var test in testMethod.GetCustomAttributes<Test>()) |
| 86 | + if (testMethod.GetCustomAttributes<Test>().Count() > 0) |
76 | 87 | { |
77 | 88 | bool failed = false; |
| 89 | + beforeEach?.Invoke(instance, null); |
78 | 90 | try |
79 | 91 | { |
80 | 92 | testMethod?.Invoke(instance, null); |
81 | 93 | } |
82 | | - catch (Exception e) |
| 94 | + catch (TargetInvocationException e) |
83 | 95 | { |
84 | | - if(e.GetType() != typeof(SuccessException)) |
| 96 | + if (!(e.InnerException is SuccessException)) |
85 | 97 | failed = true; |
86 | 98 | } |
| 99 | + catch (Exception e) |
| 100 | + { |
| 101 | + failed = true; |
| 102 | + } |
87 | 103 | finally |
88 | 104 | { |
| 105 | + afterEach?.Invoke(instance, null); |
89 | 106 | Debug.Log($"Test '{suite.Name} {testMethod.Name}' finished with " + (failed ? "Error" : "Success")); |
90 | 107 | } |
91 | 108 | } |
92 | | - |
93 | | - var testCases = testMethod.GetCustomAttributes<TestCase>(); |
94 | | - int successCount = 0; |
95 | | - foreach (var testCase in testCases) |
| 109 | + else |
96 | 110 | { |
97 | | - bool failed = false; |
98 | | - try |
| 111 | + var testCases = testMethod.GetCustomAttributes<TestCase>(); |
| 112 | + int successCount = 0; |
| 113 | + foreach (var testCase in testCases) |
99 | 114 | { |
100 | | - var result = testMethod?.Invoke(instance, testCase.Attributes); |
101 | | - if (testCase.ExpectedResult != null) |
102 | | - failed = !testCase.ExpectedResult.Equals(result); |
103 | | - } |
104 | | - catch (Exception e) |
105 | | - { |
106 | | - if(e.GetType() != typeof(SuccessException)) |
| 115 | + bool failed = false; |
| 116 | + beforeEach?.Invoke(instance, null); |
| 117 | + try |
| 118 | + { |
| 119 | + var result = testMethod?.Invoke(instance, testCase.Attributes); |
| 120 | + if (testCase.ExpectedResult != null) |
| 121 | + failed = !testCase.ExpectedResult.Equals(result); |
| 122 | + } |
| 123 | + catch (TargetInvocationException e) |
| 124 | + { |
| 125 | + if (!(e.InnerException is SuccessException)) |
| 126 | + failed = true; |
| 127 | + } |
| 128 | + catch (Exception e) |
| 129 | + { |
107 | 130 | failed = true; |
| 131 | + } |
| 132 | + finally |
| 133 | + { |
| 134 | + afterEach?.Invoke(instance, null); |
| 135 | + |
| 136 | + if (!failed) |
| 137 | + successCount++; |
| 138 | + } |
108 | 139 | } |
109 | | - finally |
110 | | - { |
111 | | - if (!failed) |
112 | | - successCount++; |
113 | | - } |
114 | | - } |
115 | 140 |
|
116 | | - if(testCases.Count() > 0) |
117 | 141 | Debug.Log($"Test '{suite.Name} {testMethod.Name}' finished with {successCount}/{testCases.Count()} successfull test cases."); |
| 142 | + } |
118 | 143 | } |
119 | 144 |
|
120 | 145 | disposer?.Invoke(instance, null); |
|
0 commit comments