public void LoadAssemblyTestCases(Assembly assembly) { foreach (Type t in assembly.GetTypes()) { if (t.GetCustomAttributes(typeof(TestSetAttribute), true).Length > 0) { bool allTargets = ( Filters == null || Filters.Length==0) || Filters.Any(f => f.StartsWith(t.Name)); TestSetAttribute testSetAttr = t.GetCustomAttributes( typeof(TestSetAttribute), true)[0] as TestSetAttribute; #if DEBUG if (testSetAttr != null && testSetAttr.DebugEnabled) #else if (testSetAttr != null && testSetAttr.ReleaseEnabled) #endif { object testSetInstance = System.Activator.CreateInstance(t); if (testSetInstance != null) { TestSetInfo testSetInfo = new TestSetInfo() { Instance = testSetInstance, Name = t.Name, }; foreach (MethodInfo method in testSetInstance.GetType().GetMethods( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { if ((allTargets || ((Filters == null || Filters.Length==0) || Filters.Any(f => f.StartsWith(method.Name, StringComparison.CurrentCultureIgnoreCase)))) && method.GetCustomAttributes(typeof(TestCaseAttribute), false).Length > 0) { TestCaseAttribute testCaseAttr = method.GetCustomAttributes(typeof(TestCaseAttribute), false)[0] as TestCaseAttribute; if (testCaseAttr != null && testCaseAttr.DebugEnabled) { TestCaseInfo testCaseInfo = new TestCaseInfo { Name = method.Name, Method = method, Sort = testCaseAttr.Sort, }; testSetInfo.TestCases.Add(testCaseInfo); } } } if (testSetInfo.TestCases.Count > 0) { testSets.Add(testSetInfo); } } } } } }
internal bool RunTestCase(TestSetInfo testSetInfo, TestCaseInfo testCaseInfo) { object testSet = testSetInfo.Instance; stop.Reset(); if (BeforePerformTestCase != null) { var evtArg = new TestCaseEventArgs(testSetInfo, testCaseInfo); BeforePerformTestCase(this, evtArg); if (evtArg.Cancel) return true; } GC.Collect(0, GCCollectionMode.Forced); long mem = GetProcessMemoryUsage(); stop.Start(); try { testCaseInfo.Method.Invoke(testSet, null); } catch (TargetInvocationException x) { testCaseInfo.Exception = x.InnerException; } catch (TestCaseFailureException x) { testCaseInfo.Exception = x; } stop.Stop(); testCaseInfo.Performed = true; testCaseInfo.ElapsedMilliseconds = stop.ElapsedMilliseconds; testCaseInfo.MemoryUsage = GetProcessMemoryUsage() - mem; GC.Collect(0, GCCollectionMode.Forced); if (AfterPerformTestCase != null) { AfterPerformTestCase(this, new TestCaseEventArgs(testSetInfo, testCaseInfo)); } return testCaseInfo.Exception != null; }
public TestCaseEventArgs(TestSetInfo testSetInfo, TestCaseInfo testCaseInfo) { this.testSetInfo = testSetInfo; this.testCaseInfo = testCaseInfo; }