diff options
author | Bojan Rajkovic <bojan.rajkovic@microsoft.com> | 2017-04-07 18:03:16 +0300 |
---|---|---|
committer | Bojan Rajkovic <bojan.rajkovic@microsoft.com> | 2017-04-07 18:03:16 +0300 |
commit | 61068fde945be4d83d066153566ce331632d9af0 (patch) | |
tree | 5503f3773364187dd9e1006c65eda34c5ed24294 | |
parent | 32be3bdfbabbab8b904e69d8ccfea0ad0bf8bfa8 (diff) |
Support global set up and teardown.xammac-net45-global-setup
-rwxr-xr-x | src/framework/Attributes/TestFixtureSetUpAttribute.cs | 10 | ||||
-rw-r--r-- | src/framework/GuiUnit/TestRunner.cs | 49 |
2 files changed, 59 insertions, 0 deletions
diff --git a/src/framework/Attributes/TestFixtureSetUpAttribute.cs b/src/framework/Attributes/TestFixtureSetUpAttribute.cs index 57116a6..106ff14 100755 --- a/src/framework/Attributes/TestFixtureSetUpAttribute.cs +++ b/src/framework/Attributes/TestFixtureSetUpAttribute.cs @@ -33,4 +33,14 @@ namespace NUnit.Framework public class TestFixtureSetUpAttribute : NUnitAttribute { } + + /// <summary> + /// Attribute used to mark a class that contains one-time SetUp + /// and/or TearDown methods that apply to all the tests in a + /// namespace or an assembly. + /// </summary> + [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] + public class SetUpFixtureAttribute : Attribute + { + } } diff --git a/src/framework/GuiUnit/TestRunner.cs b/src/framework/GuiUnit/TestRunner.cs index b7ac06e..596c0cc 100644 --- a/src/framework/GuiUnit/TestRunner.cs +++ b/src/framework/GuiUnit/TestRunner.cs @@ -26,6 +26,7 @@ using System.IO; using System.Linq; using System.Collections; using System.Reflection; +using NUnit.Framework; using NUnit.Framework.Api; using NUnit.Framework.Internal; using NUnit.Framework.Internal.Filters; @@ -223,11 +224,15 @@ namespace GuiUnit } if (MainLoop == null) { + ExecuteGlobalSetupCode (assembly); RunTests (filter); + ExecuteGlobalTearDownCode (assembly); } else { MainLoop.InitializeToolkit (); System.Threading.ThreadPool.QueueUserWorkItem (d => { + ExecuteGlobalSetupCode (assembly); RunTests (filter); + ExecuteGlobalTearDownCode (assembly); Shutdown (); }); MainLoop.RunMainLoop (); @@ -260,6 +265,50 @@ namespace GuiUnit } } + bool IsValidSetUpMethod (MethodInfo method) + { + // See CheckSetUpTearDownMethods in NUnitTestFixtureBuilder + if (method.IsAbstract || !method.IsPublic && !method.IsFamily || + method.GetParameters ().Length > 0 || !method.ReturnType.Equals (typeof (void))) + { +#if NET_4_5 + if (MethodHelper.IsAsyncMethod (method)) + return true; +#endif + return false; + } + + return true; + } + + void ExecuteGlobalSetupCode (Assembly assembly) + { + var classesWithAttribute = assembly.GetExportedTypes () + .Where (t => t.GetCustomAttribute<SetUpFixtureAttribute> () != null); + foreach (var globalSetupClass in classesWithAttribute) { + var setupMethods = Reflect.GetMethodsWithAttribute (globalSetupClass, typeof (SetUpAttribute), false); + var classInstance = Activator.CreateInstance (globalSetupClass); + foreach (var method in setupMethods) { + if (IsValidSetUpMethod (method)) + Reflect.InvokeMethod (method, method.IsStatic ? null : classInstance); + } + } + } + + void ExecuteGlobalTearDownCode (Assembly assembly) + { + var classesWithAttribute = assembly.GetExportedTypes () + .Where (t => t.GetCustomAttribute<SetUpFixtureAttribute> () != null); + foreach (var globalSetupClass in classesWithAttribute) { + var teardownMethods = Reflect.GetMethodsWithAttribute (globalSetupClass, typeof (TearDownAttribute), false); + var classInstance = Activator.CreateInstance (globalSetupClass); + foreach (var method in teardownMethods) { + if (IsValidSetUpMethod (method)) + Reflect.InvokeMethod (method, method.IsStatic ? null : classInstance); + } + } + } + static void Shutdown () { // Run the shutdown method on the main thread |