diff --git a/src/vstest.console/Processors/ListFullyQualifiedTestsArgumentProcessor.cs b/src/vstest.console/Processors/ListFullyQualifiedTestsArgumentProcessor.cs index a8fa98d386..7f2128651e 100644 --- a/src/vstest.console/Processors/ListFullyQualifiedTestsArgumentProcessor.cs +++ b/src/vstest.console/Processors/ListFullyQualifiedTestsArgumentProcessor.cs @@ -281,6 +281,7 @@ private class TestCaseFilter { private static TestCaseFilterExpression filterExpression; private const string TestCategory = "TestCategory"; + private const string Category = "Category"; private const string Traits = "Traits"; public TestCaseFilter() @@ -436,6 +437,13 @@ private static Dictionary> GetTraitsInTraitDictionary(Dicti } } + //This is hack for NUnit,Xunit to understand test category -> This method is called only for NUnit/Xunit + if (!traitDictionary.ContainsKey(TestCategory) && traitDictionary.ContainsKey(Category)) + { + traitDictionary.TryGetValue(Category, out var categoryValue); + traitDictionary.Add(TestCategory, categoryValue); + } + return traitDictionary; } diff --git a/test/vstest.console.UnitTests/Processors/ListFullyQualifiedTestsArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/ListFullyQualifiedTestsArgumentProcessorTests.cs index deca36914f..114a6481ab 100644 --- a/test/vstest.console.UnitTests/Processors/ListFullyQualifiedTestsArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/ListFullyQualifiedTestsArgumentProcessorTests.cs @@ -235,6 +235,22 @@ public void ExecutorExecuteShouldOutputDiscoveredTestsAndReturnSuccess() Assert.IsTrue(fileOutput.Contains("Test2")); } + [TestMethod] + public void DiscoveryShouldFilterCategoryTestsAndReturnSuccess() + { + var mockDiscoveryRequest = new Mock(); + var mockConsoleOutput = new Mock(); + + this.RunListFullyQualifiedTestArgumentProcessorWithTraits(mockDiscoveryRequest, mockConsoleOutput); + + mockDiscoveryRequest.Verify(dr => dr.DiscoverAsync(), Times.Once); + + var fileOutput = File.ReadAllLines(this.dummyFilePath); + Assert.IsTrue(fileOutput.Length == 1); + Assert.IsTrue(fileOutput.Contains("Test1")); + Assert.IsTrue(!fileOutput.Contains("Test2")); + } + [ExpectedException(typeof(CommandLineException))] [TestMethod] public void ExecutorExecuteShouldThrowWhenListFullyQualifiedTestsTargetPathIsEmpty() @@ -268,6 +284,31 @@ public void ListFullyQualifiedTestsArgumentProcessorExecuteShouldInstrumentDisco } #endregion + private void RunListFullyQualifiedTestArgumentProcessorWithTraits(Mock mockDiscoveryRequest, Mock mockConsoleOutput, bool legitPath = true) + { + var mockTestPlatform = new Mock(); + var list = new List(); + + var t1 = new TestCase("Test1", new Uri("http://FooTestUri1"), "Source1"); + t1.Traits.Add(new Trait("Category", "MyCat")); + list.Add(t1); + + var t2 = new TestCase("Test2", new Uri("http://FooTestUri2"), "Source2"); + t2.Traits.Add(new Trait("Category", "MyBat")); + list.Add(t2); + + mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Raises(dr => dr.OnDiscoveredTests += null, new DiscoveredTestsEventArgs(list)); + mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny(), It.IsAny())).Returns(mockDiscoveryRequest.Object); + + this.ResetAndAddSourceToCommandLineOptions(legitPath); + var cmdOptions = CommandLineOptions.Instance; + cmdOptions.TestCaseFilterValue = "TestCategory=MyCat"; + + var testRequestManager = new TestRequestManager(cmdOptions, mockTestPlatform.Object, TestLoggerManager.Instance, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask); + + GetExecutor(testRequestManager, mockConsoleOutput.Object).Execute(); + } + private void RunListFullyQualifiedTestArgumentProcessorExecuteWithMockSetup(Mock mockDiscoveryRequest, Mock mockConsoleOutput, bool legitPath = true) { var mockTestPlatform = new Mock();