Exemplo n.º 1
0
        private void do_startup()
        {
            // this includes the executable as s[0], which we don't want
            string[] args = Environment.GetCommandLineArgs();
            Debug.Assert(args.Length >= 2, "Unexpected number of args. args[0]=exe, args[1]=dll to test");
            string[] new_args = args.Skip(2).ToArray();

            if (runUnitTest)
            {
                try
                {
                    // Setup the unit test case
                    unitTestMethod = UnitTestsUtil.FindUnitTestMethodByName(testAssembly, unitTestMethodName, new_args.Length);
                    unitTestCase   = UnitTestsUtil.CreateUnitTestCase(unitTestMethod, CreateTestContext(), new_args);
                }
                catch (Exception ex)
                {
                    ReportErrorAndExit(ex.Message, ChessExitCode.TestFailure, false, ex);
                }
            }
            else
            {
                // Use the ChessTest.Startup method
                try
                {
                    if (startup != null)
                    {
                        bool ret = (bool)startup.Invoke(null, new Object[] { new_args });
                        if (!ret)
                        {
                            ReportErrorAndExit(testclass + ".Startup returned false.", ChessExitCode.TestFailure, false, null);
                        }
                    }

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    manager.RunFinalizers();
                }
                catch (Exception ex)
                {
                    if (ex is TargetInvocationException)
                    {
                        ex = ((TargetInvocationException)ex).InnerException;
                    }

                    string message = testclass + ".Startup threw unexpected exception: " + ex.GetType();
                    ReportErrorAndExit(message, ChessExitCode.UnitTestException, false, ex);
                }
            }
        }
Exemplo n.º 2
0
        protected override void Invoke(ManagedTestCase testCase, object testobject, MethodInfo method, object[] args, string name)
        {
            TaskoMeterTestContext ctx = (TaskoMeterTestContext)testCase.Context;

            Action invoke = delegate() { method.Invoke(testobject, args); };

            var t = Metering.MeasureInteractively(
                invoke,
                ctx.Repetitions,
                invoke,
                ctx.WarmupRepetitions,
                name);

            t.Join();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Runs the test and returns the result xml.
        /// </summary>
        /// <returns></returns>
        public XElement RunTestCase(TestCaseMetadata metadata)
        {
            ManagedTestCase testCase   = null;
            object          testObject = null;

            try
            {
                testCase   = Controller.CreateManagedTestCase(metadata);
                testObject = testCase.CreateNewTestObject();
            }
            catch (Exception ex)
            {
                if (ex is TargetInvocationException)
                {
                    ex = ((TargetInvocationException)ex).InnerException;
                }

                throw new Exception("Unit test threw exception while creating test class.", ex);
            }

            // Exec the unit test
            MethodInfo unitTestMethod = testCase.Method;

            object[] unitTestArgs = testCase.Arguments;
            try
            {
                Invoke(testCase, testObject, unitTestMethod, unitTestArgs, testCase.DisplayNameWithArgs);
                if (testCase.ExpectedExceptionType != null)
                {
                    string errMsg = AssertMessagesUtil.FormatAssertionMessage_ExpectedExceptionNotThrown(testCase);
                    return(TestResultUtil.CreateXTestResult(TestResultType.AssertFailure, errMsg, null));
                }
            }
            catch (Exception ex)
            {
                if (ex is TargetInvocationException)
                {
                    ex = ((TargetInvocationException)ex).InnerException;
                }
                Type exType = ex.GetType();

                // Detect expected exceptions and handle Assert exceptions
                if (testCase.ExpectedExceptionType == null || !testCase.ExpectedExceptionType.IsAssignableFrom(exType))
                {
                    // Handle special UnitTest exceptions
                    if (ex is ConcurrencyUnitTestException)
                    {
                        // Pre-defined exceptions can just pass up their messages
                        if (ex is AssertFailedException)
                        {
                            return(TestResultUtil.CreateXTestResult(TestResultType.AssertFailure, ex.Message, ex));
                        }
                        else if (ex is AssertInconclusiveException)
                        {
                            return(TestResultUtil.CreateXTestResult(TestResultType.Inconclusive, ex.Message, ex));
                        }

#if DEBUG
                        // If there's another ex type that we've defined in the framework but haven't handled
                        // by the prev conditions lets warn the developer.
                        Type cutExType = typeof(ConcurrencyUnitTestException);
                        if (exType != cutExType && exType.Assembly.FullName == cutExType.Assembly.FullName)
                        {
                            System.Diagnostics.Trace.TraceWarning("Unhandled ConcurrencyUnitTestException derived type in the CUT assembly: " + exType.Name + ". Custom handling should be added here.");
                        }
#endif

                        // If not a built in exception, then use the regular handling below.
                    }

                    // If not an assert, then it's an unexpected exception
                    if (testCase.ExpectedExceptionType == null)
                    {
                        string errMsg = AssertMessagesUtil.FormatAssertionMessage_UnExpectedExceptionThrown(testCase, ex);
                        // Write the exception to the trace log so a debugger can see it.
                        System.Diagnostics.Trace.TraceError(errMsg + Environment.NewLine + "Stack Trace: " + ex.StackTrace);
                        return(TestResultUtil.CreateXTestResult(TestResultType.Exception, errMsg, ex));
                    }
                    else // Then the exception isn't what's expected
                    {
                        string errMsg = AssertMessagesUtil.FormatAssertionMessage_ExceptionOfWrongTypeThrown(testCase, ex);
                        // Write the exception to the trace log so a debugger can see it.
                        System.Diagnostics.Trace.TraceError(errMsg + Environment.NewLine + "Stack Trace: " + ex.StackTrace);
                        return(TestResultUtil.CreateXTestResult(TestResultType.AssertFailure, errMsg, ex));
                    }
                }
                // else - The exception was expected, so the test succedded, keep going
            }

            return(TestResultUtil.CreateXTestResult(TestResultType.Passed, null, null));
        }
Exemplo n.º 4
0
 protected virtual void Invoke(ManagedTestCase testCase, object testobject, MethodInfo method, object[] args, string name)
 {
     method.Invoke(testobject, args);
 }