|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System.Reflection; |
| 5 | + |
| 6 | +using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution; |
| 7 | +using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers; |
| 8 | +using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel; |
| 9 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 10 | + |
| 11 | +using Moq; |
| 12 | + |
| 13 | +using TestFramework.ForTestingMSTest; |
| 14 | + |
| 15 | +namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution; |
| 16 | + |
| 17 | +public class ClassCleanupManagerTests : TestContainer |
| 18 | +{ |
| 19 | + public void AssemblyCleanupRunsAfterAllTestsFinishEvenIfWeScheduleTheSameTestMultipleTime() |
| 20 | + { |
| 21 | + ReflectHelper reflectHelper = Mock.Of<ReflectHelper>(); |
| 22 | + MethodInfo methodInfo = typeof(ClassCleanupManagerTests).GetMethod(nameof(FakeTestMethod), BindingFlags.Instance | BindingFlags.NonPublic); |
| 23 | + MethodInfo classCleanupMethodInfo = typeof(ClassCleanupManagerTests).GetMethod(nameof(FakeClassCleanupMethod), BindingFlags.Instance | BindingFlags.NonPublic); |
| 24 | + // Full class name must agree between unitTestElement.TestMethod.FullClassName and testMethod.FullClassName; |
| 25 | + string fullClassName = methodInfo.DeclaringType.FullName; |
| 26 | + TestMethod testMethod = new(nameof(FakeTestMethod), fullClassName, typeof(ClassCleanupManagerTests).Assembly.FullName, isAsync: false); |
| 27 | + |
| 28 | + // Setting 2 of the same test to run, we should run assembly cleanup after both these tests |
| 29 | + // finish, not after the first one finishes. |
| 30 | + List<UnitTestElement> testsToRun = new() |
| 31 | + { |
| 32 | + new(testMethod), |
| 33 | + new(testMethod), |
| 34 | + }; |
| 35 | + |
| 36 | + var classCleanupManager = new ClassCleanupManager(testsToRun, ClassCleanupBehavior.EndOfClass, ClassCleanupBehavior.EndOfClass, reflectHelper); |
| 37 | + |
| 38 | + TestClassInfo testClassInfo = new(typeof(ClassCleanupManagerTests), null, true, null, null, null) |
| 39 | + { |
| 40 | + // This needs to be set, to allow running class cleanup. |
| 41 | + ClassCleanupMethod = classCleanupMethodInfo, |
| 42 | + }; |
| 43 | + TestMethodInfo testMethodInfo = new(methodInfo, testClassInfo, null!); |
| 44 | + classCleanupManager.MarkTestComplete(testMethodInfo, testMethod, out bool shouldRunEndOfClassCleanup); |
| 45 | + |
| 46 | + // The cleanup should not run here yet, we have 1 remaining test to run. |
| 47 | + Assert.IsFalse(shouldRunEndOfClassCleanup); |
| 48 | + Assert.IsFalse(classCleanupManager.ShouldRunEndOfAssemblyCleanup); |
| 49 | + |
| 50 | + classCleanupManager.MarkTestComplete(testMethodInfo, testMethod, out shouldRunEndOfClassCleanup); |
| 51 | + // The cleanup should run here. |
| 52 | + Assert.IsTrue(shouldRunEndOfClassCleanup); |
| 53 | + Assert.IsTrue(classCleanupManager.ShouldRunEndOfAssemblyCleanup); |
| 54 | + } |
| 55 | + |
| 56 | + private void FakeTestMethod() |
| 57 | + { |
| 58 | + } |
| 59 | + |
| 60 | + private void FakeClassCleanupMethod() |
| 61 | + { |
| 62 | + } |
| 63 | +} |
0 commit comments