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

Moving discovery output to console logger #1111

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/vstest.console/Internal/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> parameters)
Expand Down Expand Up @@ -474,6 +478,21 @@ private void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e)
}
}
}

/// <summary>
/// Called when discovered tests are received.
/// </summary>
private void DiscoveredTestsHandler(object sender, DiscoveredTestsEventArgs e)
{
ValidateArg.NotNull<object>(sender, "sender");
ValidateArg.NotNull<DiscoveredTestsEventArgs>(e, "e");

foreach (TestCase test in e.DiscoveredTestCases)
{
String output = String.Format(CultureInfo.CurrentUICulture, CommandLineResources.AvailableTestsFormat, test.DisplayName);
Output.WriteLine(output, OutputLevel.Information);
}
}
#endregion
}
}
40 changes: 1 addition & 39 deletions src/vstest.console/Processors/ListTestsArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@ internal class ListTestsArgumentExecutor : IArgumentExecutor
/// </summary>
private IRunSettingsProvider runSettingsManager;

/// <summary>
/// Registers for discovery events during discovery
/// </summary>
private ITestDiscoveryEventsRegistrar discoveryEventsRegistrar;

#endregion

#region Constructor
Expand Down Expand Up @@ -172,7 +167,6 @@ internal ListTestsArgumentExecutor(
this.testRequestManager = testRequestManager;

this.runSettingsManager = runSettingsProvider;
this.discoveryEventsRegistrar = new DiscoveryEventsRegistrar(output);
}

#endregion
Expand Down Expand Up @@ -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);
}
}
}
}
}
56 changes: 55 additions & 1 deletion test/vstest.console.UnitTests/Internal/ConsoleLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.Internal
public class ConsoleLoggerTests
{
private Mock<ITestRunRequest> testRunRequest;
private Mock<IDiscoveryRequest> discoveryRequest;
private Mock<TestLoggerEvents> events;
private Mock<IOutput> mockOutput;
private TestLoggerManager testLoggerManager;
Expand Down Expand Up @@ -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());
}

/// <summary>
/// Exception should be thrown if event args is null.
/// </summary>
[TestMethod]
public void DiscoveredTestsHandlerShouldThowExceptionIfEventArgsIsNull()
{
// Raise an event on mock object
Assert.ThrowsException<ArgumentNullException>(() =>
{
this.discoveryRequest.Raise(m => m.OnDiscoveredTests += null, default(DiscoveredTestsEventArgs));
});
}

/// <summary>
/// Console output should be written in case tests are discovered.
/// </summary>
[TestMethod]
public void DiscoveredTestsHandlerShouldWriteToConsoleIfTestsPresent()
{
List<TestCase> testCases = new List<TestCase>();
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());
}

/// <summary>
/// Console output should not be written in case there are no tests discovered.
/// </summary>
[TestMethod]
public void DiscoveredTestsHandlerShouldNotWriteToConsoleIfNoTestsPresent()
{
List<TestCase> testCases = new List<TestCase>();
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<string>(), It.IsAny<OutputLevel>()), Times.Never());
}

/// <summary>
/// Setup Mocks and other dependencies
/// </summary>
private void Setup()
{
// mock for ITestRunRequest
// mock for ITestRunRequest and IDiscoveryRequest
this.testRunRequest = new Mock<ITestRunRequest>();
this.discoveryRequest = new Mock<IDiscoveryRequest>();

this.events = new Mock<TestLoggerEvents>();
this.mockOutput = new Mock<IOutput>();

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,6 @@ public void ExecutorExecuteShouldThrowOtherExceptions()
Assert.ThrowsException<Exception>(() => executor.Execute());
}

[TestMethod]
public void ExecutorExecuteShouldOutputDiscoveredTestsAndReturnSuccess()
{
var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();
var mockConsoleOutput = new Mock<IOutput>();

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()
{
Expand Down