Skip to content

Commit

Permalink
Support /TestCaseFilter and /Tests arguments at the same time (#2371)
Browse files Browse the repository at this point in the history
* Support /TestCaseFilter and /Tests arguments at the same time
  • Loading branch information
NGloreous authored May 11, 2020
1 parent df61f73 commit bcad654
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,6 @@ public ArgumentProcessorResult Execute()
throw new CommandLineException(string.Format(CultureInfo.CurrentUICulture, CommandLineResources.MissingTestSourceFile));
}

if (!string.IsNullOrWhiteSpace(this.commandLineOptions.TestCaseFilterValue))
{
throw new CommandLineException(string.Format(CultureInfo.CurrentUICulture, CommandLineResources.InvalidTestCaseFilterValueForSpecificTests));
}

this.effectiveRunSettings = this.runSettingsManager.ActiveRunSettings.SettingsXml;

// Discover tests from sources and filter on every discovery reported.
Expand Down
40 changes: 2 additions & 38 deletions test/vstest.console.UnitTests/ExecutorUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,6 @@ public void ExecutorShouldSanitizeNoLogoInput()
Assert.IsTrue(mockOutput.Messages.Any(message => message.Message.Contains(CommandLineResources.NoArgumentsProvided)));
}

[TestMethod]
public void ExecutorShouldSanitizeNoLogoInputAndShouldProcessOtherArgs()
{
// Create temp file for testsource dll to pass FileUtil.Exits()
var testSourceDllPath = Path.GetTempFileName();
string[] args = { testSourceDllPath, "/tests:Test1", "/testCasefilter:Test", "--nologo" };
var mockOutput = new MockOutput();

var exitCode = new Executor(mockOutput, this.mockTestPlatformEventSource.Object).Execute(args);

var errorMessageCount = mockOutput.Messages.Count(msg => msg.Level == OutputLevel.Error && msg.Message.Contains(CommandLineResources.InvalidTestCaseFilterValueForSpecificTests));
Assert.AreEqual(1, errorMessageCount, "Invalid Arguments Combination should display error.");
Assert.AreEqual(1, exitCode, "Invalid Arguments Combination execution should exit with error.");

Assert.IsFalse(mockOutput.Messages.First().Message.Contains(CommandLineResources.MicrosoftCommandLineTitle.Substring(0, 20)),
"First Printed message must be Microsoft Copyright");

File.Delete(testSourceDllPath);
}


/// <summary>
/// Executor should Print Error message and Help contents when no arguments are provided.
/// </summary>
Expand Down Expand Up @@ -190,22 +169,6 @@ public void ExecuteShouldInstrumentVsTestConsoleStop()
this.mockTestPlatformEventSource.Verify(x => x.VsTestConsoleStop(), Times.Once);
}

[TestMethod]
public void ExecuteShouldExitWithErrorOnInvalidArgumentCombination()
{
// Create temp file for testsource dll to pass FileUtil.Exits()
var testSourceDllPath = Path.GetTempFileName();
string[] args = { testSourceDllPath, "/tests:Test1", "/testCasefilter:Test" };
var mockOutput = new MockOutput();

var exitCode = new Executor(mockOutput, this.mockTestPlatformEventSource.Object).Execute(args);

var errorMessageCount = mockOutput.Messages.Count(msg => msg.Level == OutputLevel.Error && msg.Message.Contains(CommandLineResources.InvalidTestCaseFilterValueForSpecificTests));
Assert.AreEqual(1, errorMessageCount, "Invalid Arguments Combination should display error.");
Assert.AreEqual(1, exitCode, "Invalid Arguments Combination execution should exit with error.");
File.Delete(testSourceDllPath);
}

[TestMethod]
public void ExecuteShouldExitWithErrorOnResponseFileException()
{
Expand Down Expand Up @@ -243,7 +206,8 @@ public void ExecuteShouldNotThrowSettingsExceptionButLogOutput()

File.WriteAllText(runSettingsFile, fileContents);

string[] args = { "/settings:" + runSettingsFile };
var testSourceDllPath = Path.GetTempFileName();
string[] args = { testSourceDllPath, "/settings:" + runSettingsFile };
var mockOutput = new MockOutput();

var exitCode = new Executor(mockOutput, this.mockTestPlatformEventSource.Object).Execute(args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Processors
using CoreUtilities.Tracing.Interfaces;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.VisualStudio.TestPlatform.Client;
using Microsoft.VisualStudio.TestPlatform.Client.Discovery;
using Microsoft.VisualStudio.TestPlatform.Client.RequestHelper;
using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors;
using Microsoft.VisualStudio.TestPlatform.CommandLine.Publisher;
Expand Down Expand Up @@ -172,15 +173,35 @@ public void ExecutorExecuteForNoSourcesShouldThrowCommandLineException()
}

[TestMethod]
public void ExecutorExecuteForValidSourceWithTestCaseFilterShouldThrowCommandLineException()
public void ExecutorExecuteForValidSourceWithTestCaseFilterShouldRunTests()
{
var mockTestPlatform = new Mock<ITestPlatform>();
var mockTestRunRequest = new Mock<ITestRunRequest>();
var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();

this.ResetAndAddSourceToCommandLineOptions();
var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, TestPlatformFactory.GetTestPlatform(), TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask, this.mockProcessHelper.Object);

List<TestCase> list = new List<TestCase>();
list.Add(new TestCase("Test1", new Uri("http://FooTestUri1"), "Source1"));
list.Add(new TestCase("Test2", new Uri("http://FooTestUri1"), "Source1"));
mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Raises(dr => dr.OnDiscoveredTests += null, new DiscoveredTestsEventArgs(list));

mockTestPlatform.Setup(tp => tp.CreateTestRunRequest(It.IsAny<IRequestData>(), It.IsAny<TestRunCriteria>(), It.IsAny<TestPlatformOptions>())).Returns(mockTestRunRequest.Object);
mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>(), It.IsAny<TestPlatformOptions>())).Returns(mockDiscoveryRequest.Object);

var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask, this.mockProcessHelper.Object);
var executor = GetExecutor(testRequestManager);

CommandLineOptions.Instance.TestCaseFilterValue = "Filter";
Assert.ThrowsException<CommandLineException>(() => executor.Execute());
executor.Initialize("Test1");
ArgumentProcessorResult argumentProcessorResult = executor.Execute();

mockOutput.Verify(o => o.WriteLine(It.IsAny<string>(), OutputLevel.Warning), Times.Never);
mockTestPlatform.Verify(o => o.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.Is<DiscoveryCriteria>(c => c.TestCaseFilter == "Filter"), It.IsAny<TestPlatformOptions>()), Times.Once());
Assert.AreEqual(ArgumentProcessorResult.Success, argumentProcessorResult);
}


[TestMethod]
public void ExecutorExecuteShouldThrowTestPlatformExceptionThrownDuringDiscovery()
{
Expand Down

0 comments on commit bcad654

Please sign in to comment.