Пример #1
0
 public ClassCleanupManager(
     IEnumerable <UnitTestElement> testsToRun,
     ClassCleanupLifecycle?lifecycleFromMsTest,
     ClassCleanupLifecycle lifecycleFromAssembly,
     ReflectHelper reflectHelper = null)
 {
     this.remainingTestsByClass = testsToRun.GroupBy(t => t.TestMethod.FullClassName)
                                  .ToDictionary(
         g => g.Key,
         g => new HashSet <string>(g.Select(t => t.DisplayName)));
     this.lifecycleFromMsTest   = lifecycleFromMsTest;
     this.lifecycleFromAssembly = lifecycleFromAssembly;
     this.reflectHelper         = reflectHelper ?? new ReflectHelper();
 }
Пример #2
0
 /// <summary>
 /// Initialized the class cleanup manager for the unit test runner. Note, this can run over process-isolation,
 /// and all inputs must be serializable from host process.
 /// </summary>
 /// <param name="testsToRun">the list of tests that will be run in this execution</param>
 /// <param name="classCleanupLifecycle">The assembly level class cleanup lifecycle</param>
 internal void InitializeClassCleanupManager(ICollection <UnitTestElement> testsToRun, ClassCleanupLifecycle classCleanupLifecycle)
 {
     this.classCleanupManager = new ClassCleanupManager(
         testsToRun,
         MSTestSettings.CurrentSettings.ClassCleanupLifecycle,
         classCleanupLifecycle,
         this.reflectHelper);
 }
Пример #3
0
        public string RunClassCleanup(ClassCleanupLifecycle classCleanupLifecycle = ClassCleanupLifecycle.EndOfAssembly)
        {
            if (this.ClassCleanupMethod is null && this.BaseClassInitAndCleanupMethods.All(p => p.Item2 == null))
            {
                return(null);
            }

            if (!this.IsClassCleanupExecuted)
            {
                lock (this.testClassExecuteSyncObject)
                {
                    if (this.IsClassCleanupExecuted)
                    {
                        return(null);
                    }

                    if (this.IsClassInitializeExecuted || this.ClassInitializeMethod is null)
                    {
                        MethodInfo classCleanupMethod = null;

                        try
                        {
                            classCleanupMethod = this.ClassCleanupMethod;
                            classCleanupMethod?.InvokeAsSynchronousTask(null);
                            var baseClassCleanupQueue = new Queue <MethodInfo>(this.BaseClassCleanupMethodsStack);
                            while (baseClassCleanupQueue.Count > 0)
                            {
                                classCleanupMethod = baseClassCleanupQueue.Dequeue();
                                classCleanupMethod?.InvokeAsSynchronousTask(null);
                            }

                            this.IsClassCleanupExecuted = true;

                            return(null);
                        }
                        catch (Exception exception)
                        {
                            var realException = exception.InnerException ?? exception;
                            this.ClassCleanupException = realException;

                            string errorMessage;

                            // special case AssertFailedException to trim off part of the stack trace
                            if (realException is AssertFailedException ||
                                realException is AssertInconclusiveException)
                            {
                                errorMessage = realException.Message;
                            }
                            else
                            {
                                errorMessage = StackTraceHelper.GetExceptionMessage(realException);
                            }

                            var exceptionStackTraceInfo = realException.TryGetStackTraceInformation();

                            errorMessage = string.Format(
                                CultureInfo.CurrentCulture,
                                Resource.UTA_ClassCleanupMethodWasUnsuccesful,
                                classCleanupMethod.DeclaringType.Name,
                                classCleanupMethod.Name,
                                errorMessage,
                                exceptionStackTraceInfo?.ErrorStackTrace);

                            if (classCleanupLifecycle == ClassCleanupLifecycle.EndOfClass)
                            {
                                var testFailedException = new TestFailedException(UnitTestOutcome.Failed, errorMessage, exceptionStackTraceInfo);
                                this.ClassCleanupException = testFailedException;
                                throw testFailedException;
                            }

                            return(errorMessage);
                        }
                    }
                }
            }

            return(null);
        }