Exemple #1
0
 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);
 }
Exemple #2
0
 /**
  * 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);
 }
Exemple #3
0
        /**
         * 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();
            }
        }