From 07a96a435dea4f87b1c211e11e5431f3ce870a85 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Tue, 23 Aug 2022 17:54:31 -0300 Subject: [PATCH] Speed up skipped tests execution This shortcirtuits test runs when they will be skipped, and also avoids launching VS altogether unless at least one test is actually executed. Fixes #39 --- src/Xunit.Vsix/VsixTestCollectionRunner.cs | 25 +++++++++++++++++++--- src/Xunit.Vsix/VsixTestMethodRunner.cs | 3 +++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Xunit.Vsix/VsixTestCollectionRunner.cs b/src/Xunit.Vsix/VsixTestCollectionRunner.cs index 8eaa1ac..a998d4d 100644 --- a/src/Xunit.Vsix/VsixTestCollectionRunner.cs +++ b/src/Xunit.Vsix/VsixTestCollectionRunner.cs @@ -26,7 +26,7 @@ class VsixTestCollectionRunner : XunitTestCollectionRunner, IDisposable _vsVersion = testCollection.VisualStudioVersion; _rootSuffix = testCollection.RootSuffix; - _vs = new VsClient(_vsVersion, _rootSuffix, testCollection.Settings); + _vs = new LazyVsClient(() => new VsClient(_vsVersion, _rootSuffix, testCollection.Settings)); } protected override Task RunTestClassAsync(ITestClass testClass, IReflectionTypeInfo @class, IEnumerable testCases) @@ -35,9 +35,28 @@ protected override Task RunTestClassAsync(ITestClass testClass, IRef Aggregator, CancellationTokenSource, CollectionFixtureMappings).RunAsync(); } - public void Dispose() + public void Dispose() => _vs.Dispose(); + + class LazyVsClient : IVsClient { - _vs.Dispose(); + Lazy _vs; + + public LazyVsClient(Func factory) => _vs = new Lazy(factory); + + public void Dispose() + { + if (_vs.IsValueCreated) + _vs.Value.Dispose(); + } + + public void Recycle() + { + if (_vs.IsValueCreated) + _vs.Value.Recycle(); + } + + public Task RunAsync(VsixTestCase testCase, IMessageBus messageBus, ExceptionAggregator aggregator) + => _vs.Value.RunAsync(testCase, messageBus, aggregator); } } } diff --git a/src/Xunit.Vsix/VsixTestMethodRunner.cs b/src/Xunit.Vsix/VsixTestMethodRunner.cs index 234ea62..5de7faa 100644 --- a/src/Xunit.Vsix/VsixTestMethodRunner.cs +++ b/src/Xunit.Vsix/VsixTestMethodRunner.cs @@ -20,6 +20,9 @@ public VsixTestMethodRunner(IVsClient vsClient, ITestMethod testMethod, IReflect protected override Task RunTestCaseAsync(IXunitTestCase testCase) { + if (testCase.SkipReason != null) + return base.RunTestCaseAsync(testCase); + if (!CancellationTokenSource.IsCancellationRequested) return _vsClient.RunAsync((VsixTestCase)testCase, MessageBus, Aggregator); else