diff --git a/src/vstest.console/Internal/ConsoleLogger.cs b/src/vstest.console/Internal/ConsoleLogger.cs index fc35353097..eac87fb630 100644 --- a/src/vstest.console/Internal/ConsoleLogger.cs +++ b/src/vstest.console/Internal/ConsoleLogger.cs @@ -141,10 +141,14 @@ public void Initialize(TestLoggerEvents events, string testRunDirectory) ConsoleLogger.Output = ConsoleOutput.Instance; } - // Register for the events. + // Register for the test run events. events.TestRunMessage += this.TestMessageHandler; events.TestResult += this.TestResultHandler; events.TestRunComplete += this.TestRunCompleteHandler; + + // Register for the discovery events. + events.DiscoveryMessage += this.TestMessageHandler; + events.DiscoveredTests += DiscoveredTestsHandler; } public void Initialize(TestLoggerEvents events, Dictionary parameters) @@ -474,6 +478,21 @@ private void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e) } } } + + /// + /// Called when discovered tests are received. + /// + private void DiscoveredTestsHandler(object sender, DiscoveredTestsEventArgs e) + { + ValidateArg.NotNull(sender, "sender"); + ValidateArg.NotNull(e, "e"); + + foreach (TestCase test in e.DiscoveredTestCases) + { + String output = String.Format(CultureInfo.CurrentUICulture, CommandLineResources.AvailableTestsFormat, test.DisplayName); + Output.WriteLine(output, OutputLevel.Information); + } + } #endregion } } diff --git a/src/vstest.console/Processors/ListTestsArgumentProcessor.cs b/src/vstest.console/Processors/ListTestsArgumentProcessor.cs index 4ad11cfbfb..dcaee2744b 100644 --- a/src/vstest.console/Processors/ListTestsArgumentProcessor.cs +++ b/src/vstest.console/Processors/ListTestsArgumentProcessor.cs @@ -130,11 +130,6 @@ internal class ListTestsArgumentExecutor : IArgumentExecutor /// private IRunSettingsProvider runSettingsManager; - /// - /// Registers for discovery events during discovery - /// - private ITestDiscoveryEventsRegistrar discoveryEventsRegistrar; - #endregion #region Constructor @@ -172,7 +167,6 @@ internal ListTestsArgumentExecutor( this.testRequestManager = testRequestManager; this.runSettingsManager = runSettingsProvider; - this.discoveryEventsRegistrar = new DiscoveryEventsRegistrar(output); } #endregion @@ -216,43 +210,11 @@ public ArgumentProcessorResult Execute() var success = this.testRequestManager.DiscoverTests( new DiscoveryRequestPayload() { Sources = this.commandLineOptions.Sources, RunSettings = runSettings }, - this.discoveryEventsRegistrar, Constants.DefaultProtocolConfig); + null, Constants.DefaultProtocolConfig); return success ? ArgumentProcessorResult.Success : ArgumentProcessorResult.Fail; } #endregion - - private class DiscoveryEventsRegistrar : ITestDiscoveryEventsRegistrar - { - private IOutput output; - - public DiscoveryEventsRegistrar(IOutput output) - { - this.output = output; - } - - public void RegisterDiscoveryEvents(IDiscoveryRequest discoveryRequest) - { - discoveryRequest.OnDiscoveredTests += this.discoveryRequest_OnDiscoveredTests; - } - - public void UnregisterDiscoveryEvents(IDiscoveryRequest discoveryRequest) - { - discoveryRequest.OnDiscoveredTests -= this.discoveryRequest_OnDiscoveredTests; - } - - private void discoveryRequest_OnDiscoveredTests(Object sender, DiscoveredTestsEventArgs args) - { - // List out each of the tests. - foreach (var test in args.DiscoveredTestCases) - { - this.output.WriteLine(String.Format(CultureInfo.CurrentUICulture, - CommandLineResources.AvailableTestsFormat, - test.DisplayName), - OutputLevel.Information); - } - } - } } } \ No newline at end of file diff --git a/test/vstest.console.UnitTests/Internal/ConsoleLoggerTests.cs b/test/vstest.console.UnitTests/Internal/ConsoleLoggerTests.cs index e607bec3c9..518ae2703e 100644 --- a/test/vstest.console.UnitTests/Internal/ConsoleLoggerTests.cs +++ b/test/vstest.console.UnitTests/Internal/ConsoleLoggerTests.cs @@ -25,6 +25,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Internal public class ConsoleLoggerTests { private Mock testRunRequest; + private Mock discoveryRequest; private Mock events; private Mock mockOutput; private TestLoggerManager testLoggerManager; @@ -622,13 +623,63 @@ public void AttachmentInformationShouldBeWrittenToConsoleIfAttachmentsArePresent this.mockOutput.Verify(o => o.WriteLine(string.Format(CultureInfo.CurrentCulture, CommandLineResources.AttachmentOutputFormat, uriDataAttachment1.Uri.LocalPath), OutputLevel.Information), Times.Once()); } + /// + /// Exception should be thrown if event args is null. + /// + [TestMethod] + public void DiscoveredTestsHandlerShouldThowExceptionIfEventArgsIsNull() + { + // Raise an event on mock object + Assert.ThrowsException(() => + { + this.discoveryRequest.Raise(m => m.OnDiscoveredTests += null, default(DiscoveredTestsEventArgs)); + }); + } + + /// + /// Console output should be written in case tests are discovered. + /// + [TestMethod] + public void DiscoveredTestsHandlerShouldWriteToConsoleIfTestsPresent() + { + List testCases = new List(); + testCases.Add(new TestCase("Test1", new Uri("http://FooTestUri1"), "Source1")); + testCases.Add(new TestCase("Test2", new Uri("http://FooTestUri2"), "Source2")); + DiscoveredTestsEventArgs discoveredTestsEventArgs = new DiscoveredTestsEventArgs(testCases); + + // Raise an event on mock object + this.discoveryRequest.Raise(m => m.OnDiscoveredTests += null, discoveredTestsEventArgs); + + // Verify + this.mockOutput.Verify(o => o.WriteLine(String.Format(CultureInfo.CurrentUICulture, CommandLineResources.AvailableTestsFormat, "Test1"), OutputLevel.Information), Times.Once()); + this.mockOutput.Verify(o => o.WriteLine(String.Format(CultureInfo.CurrentUICulture, CommandLineResources.AvailableTestsFormat, "Test2"), OutputLevel.Information), Times.Once()); + } + + /// + /// Console output should not be written in case there are no tests discovered. + /// + [TestMethod] + public void DiscoveredTestsHandlerShouldNotWriteToConsoleIfNoTestsPresent() + { + List testCases = new List(); + DiscoveredTestsEventArgs discoveredTestsEventArgs = new DiscoveredTestsEventArgs(testCases); + + // Raise an event on mock object + this.discoveryRequest.Raise(m => m.OnDiscoveredTests += null, discoveredTestsEventArgs); + + // Verify + this.mockOutput.Verify(o => o.WriteLine(It.IsAny(), It.IsAny()), Times.Never()); + } + /// /// Setup Mocks and other dependencies /// private void Setup() { - // mock for ITestRunRequest + // mock for ITestRunRequest and IDiscoveryRequest this.testRunRequest = new Mock(); + this.discoveryRequest = new Mock(); + this.events = new Mock(); this.mockOutput = new Mock(); @@ -643,6 +694,9 @@ private void Setup() // Register TestRunRequest object this.testLoggerManager.RegisterTestRunEvents(this.testRunRequest.Object); + + // Register DiscoveryRequest object + this.testLoggerManager.RegisterDiscoveryEvents(this.discoveryRequest.Object); } private void FlushLoggerMessages() diff --git a/test/vstest.console.UnitTests/Processors/ListTestsArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/ListTestsArgumentProcessorTests.cs index d5a466ecfe..e0922bb486 100644 --- a/test/vstest.console.UnitTests/Processors/ListTestsArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/ListTestsArgumentProcessorTests.cs @@ -218,21 +218,6 @@ public void ExecutorExecuteShouldThrowOtherExceptions() Assert.ThrowsException(() => executor.Execute()); } - [TestMethod] - public void ExecutorExecuteShouldOutputDiscoveredTestsAndReturnSuccess() - { - var mockDiscoveryRequest = new Mock(); - var mockConsoleOutput = new Mock(); - - this.RunListTestArgumentProcessorExecuteWithMockSetup(mockDiscoveryRequest, mockConsoleOutput); - - // Assert - mockDiscoveryRequest.Verify(dr => dr.DiscoverAsync(), Times.Once); - - mockConsoleOutput.Verify((IOutput co) => co.WriteLine(" Test1", OutputLevel.Information)); - mockConsoleOutput.Verify((IOutput co) => co.WriteLine(" Test2", OutputLevel.Information)); - } - [TestMethod] public void ListTestArgumentProcessorExecuteShouldInstrumentDiscoveryRequestStart() {