|
| 1 | +using FlaxEditor; |
| 2 | +using FlaxEditor.GUI; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Reflection; |
| 7 | + |
| 8 | +namespace FlaxEngine.UnitTesting.Editor |
| 9 | +{ |
| 10 | + public class TestRunner : EditorPlugin |
| 11 | + { |
| 12 | + private static List<Type> suites = new List<Type>(); |
| 13 | + private MainMenuButton mmBtn; |
| 14 | + |
| 15 | + public override PluginDescription Description => new PluginDescription |
| 16 | + { |
| 17 | + Author = "Lukáš Jech", |
| 18 | + AuthorUrl ="https://lukas.jech.me", |
| 19 | + Category = "Unit Testing", |
| 20 | + Description = "Simple unit testing framework", |
| 21 | + IsAlpha = false, |
| 22 | + IsBeta = false, |
| 23 | + Name = "Simple Unit Testing", |
| 24 | + SupportedPlatforms = new PlatformType[] {PlatformType.Windows}, |
| 25 | + Version = new Version(1,0), |
| 26 | + RepositoryUrl = "https://github.com/klukule/flax-ut" |
| 27 | + }; |
| 28 | + |
| 29 | + public override void InitializeEditor() |
| 30 | + { |
| 31 | + base.InitializeEditor(); |
| 32 | + |
| 33 | + mmBtn = Editor.UI.MainMenu.AddButton("Unit Tests"); |
| 34 | + mmBtn.ContextMenu.AddButton("Run unit tests").Clicked += RunTests; |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + public override void Deinitialize() |
| 39 | + { |
| 40 | + base.Deinitialize(); |
| 41 | + if (mmBtn != null) |
| 42 | + { |
| 43 | + mmBtn.Dispose(); |
| 44 | + mmBtn = null; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private static void GatherTests() |
| 49 | + { |
| 50 | + Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); |
| 51 | + suites.Clear(); |
| 52 | + foreach (var assembly in assemblies) |
| 53 | + foreach (var type in assembly.GetTypes()) |
| 54 | + if (type.GetCustomAttributes<TestFixture>().Count() > 0) |
| 55 | + suites.Add(type); |
| 56 | + } |
| 57 | + |
| 58 | + public static void RunTests() |
| 59 | + { |
| 60 | + GatherTests(); |
| 61 | + |
| 62 | + foreach (var suite in suites) |
| 63 | + { |
| 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(); |
| 67 | + |
| 68 | + var instance = Activator.CreateInstance(suite); |
| 69 | + |
| 70 | + setup?.Invoke(instance, null); |
| 71 | + |
| 72 | + foreach (var testMethod in tests) |
| 73 | + { |
| 74 | + // Mitigates the AttributeNullException |
| 75 | + foreach (var test in testMethod.GetCustomAttributes<Test>()) |
| 76 | + { |
| 77 | + bool failed = false; |
| 78 | + try |
| 79 | + { |
| 80 | + testMethod?.Invoke(instance, null); |
| 81 | + } |
| 82 | + catch (Exception e) |
| 83 | + { |
| 84 | + if(e.GetType() != typeof(SuccessException)) |
| 85 | + failed = true; |
| 86 | + } |
| 87 | + finally |
| 88 | + { |
| 89 | + Debug.Log($"Test '{suite.Name} {testMethod.Name}' finished with " + (failed ? "Error" : "Success")); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + var testCases = testMethod.GetCustomAttributes<TestCase>(); |
| 94 | + int successCount = 0; |
| 95 | + foreach (var testCase in testCases) |
| 96 | + { |
| 97 | + bool failed = false; |
| 98 | + try |
| 99 | + { |
| 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)) |
| 107 | + failed = true; |
| 108 | + } |
| 109 | + finally |
| 110 | + { |
| 111 | + if (!failed) |
| 112 | + successCount++; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if(testCases.Count() > 0) |
| 117 | + Debug.Log($"Test '{suite.Name} {testMethod.Name}' finished with {successCount}/{testCases.Count()} successfull test cases."); |
| 118 | + } |
| 119 | + |
| 120 | + disposer?.Invoke(instance, null); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments