private static Assembly loadAssemblyFromPath(string assemblyPath) { try { Assembly assembly = Assembly.LoadFile(assemblyPath); return(assembly); } catch (Exception ex) { Console.WriteLine("Can not load input test assembly from " + assemblyPath); Console.WriteLine("Stack Trace below:"); TestReporter.printStackTrace(ex); } return(null); }
/** * Running all test cases. */ public void RunTestCases(TestReporter reporter) { TestResultEventHandler += ((o, e) => { reporter.report((TestResultEvent)e); }); foreach (TestMethod m in TestCases) { m.TestResultEventHandler += ((o, e) => { reporter.report((TestResultEvent)e); }); } foreach (TestMethod m in FrameworkTestMethods) { m.TestResultEventHandler += ((o, e) => { reporter.report((TestResultEvent)e); }); } if (OneTimeSetUp != null && !OneTimeSetUp.run(null)) { // if OneTimeSetUp fails mark all testcases as NOT_RUN foreach (TestMethod testCase in TestCases) { TestResultEventHandler(this, new TestResultEvent( RESULT_TYPE.NO_RUN_FAILED_ONE_TIME_SETUP, testCase, null)); } return; } foreach (TestMethod testCase in TestCases) { object testInstance = CreateTestInstance(); if (SetUp != null) { SetUp.run(testInstance); } testCase.run(testInstance); if (TearDown != null) { TearDown.run(testInstance); } } if (OneTimeTearDown != null) { OneTimeTearDown.run(null); } }
/** * Runs itself by reflection. * @param target on which object the method is invoked. * null implies it is static method * * @return false if method has failed to execute. * true if method has not run or null. */ public bool run(object target) { try { NotifyTestEvent(new TestResultEvent(RESULT_TYPE.TEST_STARTED, this, null)); Method.Invoke(target, EMPTY_METHOD_PARAMETERS); NotifyTestEvent(createResultEvent()); return(true); } catch (TargetInvocationException ex) { NotifyTestEvent(createResultEvent(ex.InnerException)); } catch (Exception ex) { TestReporter.printStackTrace(ex); NotifyTestEvent(new TestResultEvent(RESULT_TYPE.FAILED_EXECUTION, this, ex)); } return(false); }
/** * Loads an assempbly and inspects all exported types and if a type * is annotated with [TestClass] runs it. * * @param args[0] Assembly containng the test classes */ public static void Main(string[] args) { if (args.Length == 0) { printUsage(); return; } TestReporter reporter = new TestReporter(); for (int i = 0; i < args.Length; i++) { Assembly assembly = loadAssemblyFromPath(args[i]); if (assembly == null) { continue; } Regex testClassPattern = null; Regex testCasePattern = null; if (i < (args.Length - 1) && args[i + 1][0] == '+') { i += 1; string pattern = args[i].Substring(1); int k = pattern.IndexOf('.'); if (k >= 0) { testClassPattern = new Regex(pattern.Substring(0, k)); testCasePattern = new Regex(pattern.Substring(k + 1)); } else { testClassPattern = new Regex(pattern); } } foreach (Type type in assembly.ExportedTypes) { try { if (testClassPattern != null && !testClassPattern.IsMatch(type.Name)) { continue; } TestClass testClass = MakeTestClass(type); if (testClass != null) { testClass.TestCasePattern = testCasePattern; if (!testClass.Initialize()) { continue; } Console.WriteLine(Environment.NewLine + "Running " + testClass.Type + " " + testClass.TestCount + " testcases"); testClass.RunTestCases(reporter); } else { Console.WriteLine("Invalid test class " + type); } } catch (Exception ex) { Console.WriteLine("Error running test class " + type); TestReporter.printStackTrace(ex); } } reporter.reportStats(); } }