示例#1
0
        private List <ClassTestResult> Execute(DomainInfo domainInfo)
        {
            if (domainInfo == null || domainInfo.TestClasses == null || domainInfo.TestClasses.Count == 0)
            {
                return(null);
            }

            List <ClassTestResult> results = new List <ClassTestResult>();

            foreach (KeyValuePair <Type, TestInfo> pair in domainInfo.TestClasses)
            {
                Type            testClassType = pair.Key;
                ClassTestResult result        = new ClassTestResult()
                {
                    TestClassName      = testClassType.Name,
                    MethodTestInfoList = new List <MethodTestInfo>()
                };

                string assemblyLocation = testClassType.Assembly.Location;
                string directoryPath    = assemblyLocation.Substring(0, assemblyLocation.LastIndexOf("\\"));
                domainInfo.AppDomain.SetData(Constants.AppDomainBasePath, directoryPath);
                //Directory.SetCurrentDirectory(directoryPath);

                var testInstance = Activator.CreateInstance(testClassType);

                TestInfo testInfo = pair.Value;
                if (testInfo.InitMethod != null)
                {
                    MethodTestInfo methodTestInfo = RunMethod(testInstance, testInfo.InitMethod);
                    result.MethodTestInfoList.Add(methodTestInfo);
                    if (methodTestInfo.ResultCode == TestResultCode.FAILED)
                    {
                        //If init method fails, initial state may not be ready for test methods.
                        continue;
                    }
                }

                foreach (MethodInfo method in testInfo.TestMethods)
                {
                    MethodTestInfo methodTestInfo = RunMethod(testInstance, method);
                    result.MethodTestInfoList.Add(methodTestInfo);
                }

                results.Add(result);
            }

            return(results);
        }
示例#2
0
 private void HandleException(Exception ex, MethodTestInfo methodTestInfo)
 {
     methodTestInfo.ResultCode = TestResultCode.FAILED;
     methodTestInfo.Exception  = new ExceptionInfo()
     {
         Message    = ex.Message,
         StackTrace = ex.StackTrace
     };
     if (ex.InnerException != null)
     {
         methodTestInfo.InnerException = new ExceptionInfo()
         {
             Message    = ex.InnerException.Message,
             StackTrace = ex.InnerException.StackTrace
         };
     }
 }
示例#3
0
        private MethodTestInfo RunMethod(Object testInstance, MethodInfo method)
        {
            MethodTestInfo methodTestInfo = new MethodTestInfo()
            {
                MethodName      = method.Name,
                MethodNamespace = method.Module.FullyQualifiedName,
                ResultCode      = TestResultCode.PASSED
            };

            Stopwatch stopWatch = new Stopwatch();

            try
            {
                stopWatch.Start();
                try
                {
                    method.Invoke(testInstance, null);
                }
                catch (TargetInvocationException ex)
                {
                    if (ex.InnerException != null)
                    {
                        ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                    }
                }
            }
            catch (AssertionException ex)
            {
                HandleException(ex, methodTestInfo);
            }
            catch (Exception ex)
            {
                HandleException(ex, methodTestInfo);
            }
            finally
            {
                stopWatch.Stop();
            }

            methodTestInfo.Duration = stopWatch.ElapsedMilliseconds;

            return(methodTestInfo);
        }