@@ -9,112 +9,121 @@ namespace FlaxEngine.UnitTesting.Editor
99{
1010 public class TestRunner : EditorPlugin
1111 {
12- private static List < Type > suites = new List < Type > ( ) ;
13- private MainMenuButton mmBtn ;
12+ private static readonly List < Type > _suites = new List < Type > ( ) ;
13+ private MainMenuButton _mmBtn ;
1414
1515 public override PluginDescription Description => new PluginDescription
1616 {
1717 Author = "Lukáš Jech" ,
18- AuthorUrl = "https://lukas.jech.me" ,
18+ AuthorUrl = "https://lukas.jech.me" ,
1919 Category = "Unit Testing" ,
2020 Description = "Simple unit testing framework" ,
2121 IsAlpha = false ,
2222 IsBeta = false ,
2323 Name = "Simple Unit Testing" ,
24- SupportedPlatforms = new PlatformType [ ] { PlatformType . Windows } ,
25- Version = new Version ( 1 , 0 ) ,
26- RepositoryUrl = "https://github.com/klukule/flax-ut "
24+ SupportedPlatforms = new PlatformType [ ] { PlatformType . Windows } ,
25+ Version = new Version ( 1 , 1 ) ,
26+ RepositoryUrl = "https://github.com/FlaxCommunityProjects/FlaxUnitTesting "
2727 } ;
2828
2929 public override void InitializeEditor ( )
3030 {
3131 base . InitializeEditor ( ) ;
3232
33- mmBtn = Editor . UI . MainMenu . AddButton ( "Unit Tests" ) ;
34- mmBtn . ContextMenu . AddButton ( "Run unit tests" ) . Clicked += RunTests ;
33+ _mmBtn = Editor . UI . MainMenu . AddButton ( "Unit Tests" ) ;
34+ _mmBtn . ContextMenu . AddButton ( "Run unit tests" ) . Clicked += RunTests ;
3535
3636 }
3737
3838 public override void Deinitialize ( )
3939 {
4040 base . Deinitialize ( ) ;
41- if ( mmBtn != null )
41+ if ( _mmBtn != null )
4242 {
43- mmBtn . Dispose ( ) ;
44- mmBtn = null ;
43+ _mmBtn . Dispose ( ) ;
44+ _mmBtn = null ;
4545 }
4646 }
4747
4848 private static void GatherTests ( )
4949 {
5050 Assembly [ ] assemblies = AppDomain . CurrentDomain . GetAssemblies ( ) ;
51- suites . Clear ( ) ;
51+ _suites . Clear ( ) ;
5252 foreach ( var assembly in assemblies )
5353 foreach ( var type in assembly . GetTypes ( ) )
5454 if ( type . GetCustomAttributes < TestFixture > ( ) . Count ( ) > 0 )
55- suites . Add ( type ) ;
55+ _suites . Add ( type ) ;
5656 }
5757
5858 public static void RunTests ( )
5959 {
6060 GatherTests ( ) ;
6161
62- foreach ( var suite in suites )
62+ foreach ( var suite in _suites )
6363 {
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 ( ) ;
64+ var suiteMethods = suite . GetMethods ( ) ;
65+
66+ var tests = suiteMethods . Where ( m => m . GetCustomAttributes < Test > ( ) . Count ( ) > 0 || m . GetCustomAttributes < TestCase > ( ) . Count ( ) > 0 ) . ToArray ( ) ;
67+ var setup = suiteMethods . Where ( m => m . GetCustomAttributes < OneTimeSetUp > ( ) . Count ( ) > 0 ) . FirstOrDefault ( ) ;
68+ var disposer = suiteMethods . Where ( m => m . GetCustomAttributes < OneTimeTearDown > ( ) . Count ( ) > 0 ) . FirstOrDefault ( ) ;
69+ var beforeEach = suiteMethods . Where ( m => m . GetCustomAttributes < SetUp > ( ) . Count ( ) > 0 ) . FirstOrDefault ( ) ;
70+ var afterEach = suiteMethods . Where ( m => m . GetCustomAttributes < TearDown > ( ) . Count ( ) > 0 ) . FirstOrDefault ( ) ;
6771
6872 var instance = Activator . CreateInstance ( suite ) ;
6973
7074 setup ? . Invoke ( instance , null ) ;
7175
7276 foreach ( var testMethod in tests )
7377 {
74- // Mitigates the AttributeNullException
75- foreach ( var test in testMethod . GetCustomAttributes < Test > ( ) )
78+ if ( testMethod . GetCustomAttributes < Test > ( ) . Count ( ) > 0 )
7679 {
7780 bool failed = false ;
81+ beforeEach ? . Invoke ( instance , null ) ;
7882 try
7983 {
8084 testMethod ? . Invoke ( instance , null ) ;
8185 }
8286 catch ( Exception e )
8387 {
84- if ( e . GetType ( ) != typeof ( SuccessException ) )
88+ if ( e . GetType ( ) != typeof ( SuccessException ) )
8589 failed = true ;
8690 }
8791 finally
8892 {
93+ afterEach ? . Invoke ( instance , null ) ;
8994 Debug . Log ( $ "Test '{ suite . Name } { testMethod . Name } ' finished with " + ( failed ? "Error" : "Success" ) ) ;
9095 }
9196 }
92-
93- var testCases = testMethod . GetCustomAttributes < TestCase > ( ) ;
94- int successCount = 0 ;
95- foreach ( var testCase in testCases )
97+ else
9698 {
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 )
99+ var testCases = testMethod . GetCustomAttributes < TestCase > ( ) ;
100+ int successCount = 0 ;
101+ foreach ( var testCase in testCases )
105102 {
106- if ( e . GetType ( ) != typeof ( SuccessException ) )
107- failed = true ;
108- }
109- finally
110- {
111- if ( ! failed )
112- successCount ++ ;
103+ bool failed = false ;
104+ beforeEach ? . Invoke ( instance , null ) ;
105+ try
106+ {
107+ var result = testMethod ? . Invoke ( instance , testCase . Attributes ) ;
108+ if ( testCase . ExpectedResult != null )
109+ failed = ! testCase . ExpectedResult . Equals ( result ) ;
110+ }
111+ catch ( Exception e )
112+ {
113+ if ( e . GetType ( ) != typeof ( SuccessException ) )
114+ failed = true ;
115+ }
116+ finally
117+ {
118+ afterEach ? . Invoke ( instance , null ) ;
119+
120+ if ( ! failed )
121+ successCount ++ ;
122+ }
113123 }
114- }
115124
116- if ( testCases . Count ( ) > 0 )
117125 Debug . Log ( $ "Test '{ suite . Name } { testMethod . Name } ' finished with { successCount } /{ testCases . Count ( ) } successfull test cases.") ;
126+ }
118127 }
119128
120129 disposer ? . Invoke ( instance , null ) ;
0 commit comments