Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take TestCaseFilter from runsettings #2356

Merged
merged 15 commits into from
Mar 25, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,10 @@ public bool ResultsDirectorySet
public bool CollectSourceInformationSet { get; private set; } = false;

/// <summary>
/// Default filter to use to filter tests
/// </summary>
public string TestCaseFilter { get; private set; }

/// Path to dotnet executable to be used to invoke testhost.dll. Specifying this will skip looking up testhost.exe and will force usage of the testhost.dll.
/// </summary>
public string DotnetHostPath { get; private set; }
Expand Down Expand Up @@ -576,6 +580,13 @@ public override XmlElement ToXml()
root.AppendChild(targetDevice);
}

if (!string.IsNullOrEmpty(this.TestCaseFilter))
{
XmlElement testCaseFilter = doc.CreateElement(nameof(TestCaseFilter));
testCaseFilter.InnerXml = this.TestCaseFilter;
root.AppendChild(testCaseFilter);
}

if (!string.IsNullOrEmpty(this.DotnetHostPath))
{
XmlElement dotnetHostPath = doc.CreateElement(nameof(DotnetHostPath));
Expand Down Expand Up @@ -879,6 +890,11 @@ public static RunConfiguration FromXml(XmlReader reader)
runConfiguration.TargetDevice = reader.ReadElementContentAsString();
break;

case "TestCaseFilter":
XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);
runConfiguration.TestCaseFilter = reader.ReadElementContentAsString();
break;

case "DotNetHostPath":
XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);
runConfiguration.DotnetHostPath = reader.ReadElementContentAsString();
Expand Down
6 changes: 6 additions & 0 deletions src/vstest.console/Processors/RunSettingsArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ public void Initialize(string argument)
this.commandLineOptions.InIsolation = true;
this.runSettingsManager.UpdateRunSettingsNode(InIsolationArgumentExecutor.RunSettingsPath, "true");
}

var testCaseFilter = this.runSettingsManager.QueryRunSettingsNode("RunConfiguration.TestCaseFilter");
if (testCaseFilter != null)
{
this.commandLineOptions.TestCaseFilterValue = testCaseFilter;
}
}
catch (XmlException exception)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors
Expand Down Expand Up @@ -119,7 +120,15 @@ public void Initialize(string argument)
{
if (string.IsNullOrWhiteSpace(argument))
{
if (!string.IsNullOrWhiteSpace(this.commandLineOptions.TestCaseFilterValue))
{
// if user did not specify the argument, and we have a previously set value (for example from settings)
// we keep the value and return, otherwise we throw because there was no default value, and the user did not provide any either
return;
}

throw new CommandLineException(string.Format(CultureInfo.CurrentUICulture, CommandLineResources.TestCaseFilterValueRequired));

}

this.commandLineOptions.TestCaseFilterValue = argument;
Expand Down
5 changes: 4 additions & 1 deletion src/vstest.console/TestPlatformHelpers/TestRequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public void DiscoverTests(DiscoveryRequestPayload discoveryPayload, ITestDiscove

var runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(runsettings);
var batchSize = runConfiguration.BatchSize;
var testCaseFilterFromRunsettings = runConfiguration.TestCaseFilter;

if (requestData.IsTelemetryOptedIn)
{
Expand All @@ -160,10 +161,12 @@ public void DiscoverTests(DiscoveryRequestPayload discoveryPayload, ITestDiscove
this.LogCommandsTelemetryPoints(requestData);
}



// create discovery request
var criteria = new DiscoveryCriteria(discoveryPayload.Sources, batchSize, this.commandLineOptions.TestStatsEventTimeout, runsettings)
{
TestCaseFilter = this.commandLineOptions.TestCaseFilterValue
TestCaseFilter = this.commandLineOptions.TestCaseFilterValue ?? testCaseFilterFromRunsettings
};

// Make sure to run the run request inside a lock as the below section is not thread-safe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,30 @@ public void InitializeShouldNotSetInIsolataionToTrueIfEnvironmentVariablesNotSpe
Assert.IsNull(this.settingsProvider.QueryRunSettingsNode(InIsolationArgumentExecutor.RunSettingsPath));
}

[TestMethod]
public void InitializeShouldUpdateTestCaseFilterIfProvided()
{
// Arrange.
var fileName = "C:\\temp\\r.runsettings";
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
var filter = "TestCategory=Included";
var settingsXml = $"<RunSettings><RunConfiguration><TestCaseFilter>{filter}</TestCaseFilter></RunConfiguration></RunSettings>";

var executor = new TestableRunSettingsArgumentExecutor(
CommandLineOptions.Instance,
this.settingsProvider,
settingsXml);

// Setup mocks.
var mockFileHelper = new Mock<IFileHelper>();
mockFileHelper.Setup(fh => fh.Exists(It.IsAny<string>())).Returns(true);
executor.FileHelper = mockFileHelper.Object;

// Act.
executor.Initialize(fileName);

// Assert.
Assert.AreEqual(filter, CommandLineOptions.Instance.TestCaseFilterValue);
}
#endregion

#region Testable Implementations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ public void ExecutorInitializeWithNullOrEmptyTestCaseFilterShouldThrowCommandLin
}
}

[TestMethod]
public void ExecutorInitializeWithNullOrEmptyTestCaseFilterShouldNotThrowWhenTestFilterWasSpecifiedByPreviousStep()
{
var options = CommandLineOptions.Instance;
options.TestCaseFilterValue = "Test=FilterFromPreviousStep";
TestCaseFilterArgumentExecutor executor = new TestCaseFilterArgumentExecutor(options);

executor.Initialize(null);
}

[TestMethod]
public void ExecutorInitializeWithTestCaseFilterShouldOverwriteTheValueProvidedByPreviousStep()
{
var options = CommandLineOptions.Instance;
var defaultValue = "Test=FilterFromPreviousStep";
options.TestCaseFilterValue = defaultValue;
Assert.AreEqual(defaultValue, options.TestCaseFilterValue);
TestCaseFilterArgumentExecutor executor = new TestCaseFilterArgumentExecutor(options);

var value = "Test=NewFilter";
executor.Initialize(value);

Assert.AreEqual(value, options.TestCaseFilterValue);
}

[TestMethod]
public void ExecutorInitializeWithValidTestCaseFilterShouldAddTestCaseFilterToCommandLineOptions()
{
Expand Down