Skip to content

Commit 29fc1d1

Browse files
TestRunRequestEventsRegistrar null in run specific tests (#2051)
* Registrar Null Fixed * Adding RunSpecific UTs
1 parent beea79e commit 29fc1d1

4 files changed

Lines changed: 138 additions & 2 deletions

File tree

‎src/vstest.console/Processors/RunSpecificTestsArgumentProcessor.cs‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ internal class RunSpecificTestsArgumentExecutor : IArgumentExecutor
137137
/// </summary>
138138
private ITestDiscoveryEventsRegistrar discoveryEventsRegistrar;
139139

140+
/// <summary>
141+
/// Registers and Unregisters for test run events before and after test run
142+
/// </summary>
143+
private ITestRunEventsRegistrar testRunEventsRegistrar;
144+
140145
#endregion
141146

142147
#region Constructor
@@ -159,6 +164,7 @@ public RunSpecificTestsArgumentExecutor(
159164
this.runSettingsManager = runSettingsProvider;
160165
this.output = output;
161166
this.discoveryEventsRegistrar = new DiscoveryEventsRegistrar(this.discoveryRequest_OnDiscoveredTests);
167+
this.testRunEventsRegistrar = new TestRunRequestEventsRegistrar(this.output, this.commandLineOptions);
162168
}
163169

164170
#endregion
@@ -259,7 +265,7 @@ private void ExecuteSelectedTests()
259265

260266
EqtTrace.Verbose("RunSpecificTestsArgumentProcessor:Execute: Test run is queued.");
261267
var runRequestPayload = new TestRunRequestPayload() { TestCases = this.selectedTestCases.ToList(), RunSettings = this.effectiveRunSettings, KeepAlive = keepAlive, TestPlatformOptions = new TestPlatformOptions() { TestCaseFilter = this.commandLineOptions.TestCaseFilterValue }};
262-
this.testRequestManager.RunTests(runRequestPayload, null, null, Constants.DefaultProtocolConfig);
268+
this.testRequestManager.RunTests(runRequestPayload, null, this.testRunEventsRegistrar, Constants.DefaultProtocolConfig);
263269
}
264270
else
265271
{
@@ -335,5 +341,53 @@ public void UnregisterDiscoveryEvents(IDiscoveryRequest discoveryRequest)
335341
discoveryRequest.OnDiscoveredTests -= this.discoveredTestsHandler;
336342
}
337343
}
344+
345+
private class TestRunRequestEventsRegistrar : ITestRunEventsRegistrar
346+
{
347+
private IOutput output;
348+
private CommandLineOptions commandLineOptions;
349+
350+
public TestRunRequestEventsRegistrar(IOutput output, CommandLineOptions commandLineOptions)
351+
{
352+
this.output = output;
353+
this.commandLineOptions = commandLineOptions;
354+
}
355+
356+
public void LogWarning(string message)
357+
{
358+
ConsoleLogger.RaiseTestRunWarning(message);
359+
}
360+
361+
public void RegisterTestRunEvents(ITestRunRequest testRunRequest)
362+
{
363+
testRunRequest.OnRunCompletion += TestRunRequest_OnRunCompletion;
364+
}
365+
366+
public void UnregisterTestRunEvents(ITestRunRequest testRunRequest)
367+
{
368+
testRunRequest.OnRunCompletion -= TestRunRequest_OnRunCompletion;
369+
}
370+
371+
/// <summary>
372+
/// Handles the TestRunRequest complete event
373+
/// </summary>
374+
/// <param name="sender"></param>
375+
/// <param name="e">RunCompletion args</param>
376+
private void TestRunRequest_OnRunCompletion(object sender, TestRunCompleteEventArgs e)
377+
{
378+
// If run is not aborted/cancelled then check the count of executed tests.
379+
// we need to check if there are any tests executed - to try show some help info to user to check for installed vsix extensions
380+
if (!e.IsAborted && !e.IsCanceled)
381+
{
382+
var testsFoundInAnySource = (e.TestRunStatistics == null) ? false : (e.TestRunStatistics.ExecutedTests > 0);
383+
384+
// Indicate the user to use testadapterpath command if there are no tests found
385+
if (!testsFoundInAnySource && string.IsNullOrEmpty(CommandLineOptions.Instance.TestAdapterPath) && this.commandLineOptions.TestCaseFilterValue == null)
386+
{
387+
this.output.Warning(false, CommandLineResources.SuggestTestAdapterPathIfNoTestsIsFound);
388+
}
389+
}
390+
}
391+
}
338392
}
339393
}

‎src/vstest.console/TestPlatformHelpers/TestRequestManager.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ private void CheckSourcesForCompatibility(Framework chosenFramework, Architectur
435435
if (!string.IsNullOrEmpty(incompatibleSettingWarning))
436436
{
437437
EqtTrace.Warning(incompatibleSettingWarning);
438-
registrar.LogWarning(incompatibleSettingWarning);
438+
registrar?.LogWarning(incompatibleSettingWarning);
439439
}
440440

441441
// Log compatible sources

‎test/Microsoft.TestPlatform.AcceptanceTests/FrameworkTests.cs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,29 @@ public void OnWrongFrameworkPassedTestRunShouldNotRun(RunnerInfo runnerInfo)
5050
this.StdErrorContains("Test Run Aborted.");
5151
}
5252
}
53+
54+
[TestMethod]
55+
[NetFullTargetFrameworkDataSource]
56+
[NetCoreTargetFrameworkDataSource]
57+
public void RunSpecificTestsShouldWorkWithFrameworkInCompatibleWarning(RunnerInfo runnerInfo)
58+
{
59+
AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerInfo);
60+
61+
var arguments = PrepareArguments(GetSampleTestAssembly(), string.Empty, string.Empty, this.FrameworkArgValue);
62+
arguments = string.Concat(arguments, " ", "/tests:PassingTest");
63+
arguments = string.Concat(arguments, " ", "/Framework:Framework40");
64+
65+
this.InvokeVsTest(arguments);
66+
67+
if (runnerInfo.TargetFramework.Contains("netcore"))
68+
{
69+
this.StdOutputContains("No test is available");
70+
}
71+
else
72+
{
73+
this.StdOutputContains("Following DLL(s) do not match framework/platform settings. ");
74+
this.ValidateSummaryStatus(1, 0, 0);
75+
}
76+
}
5377
}
5478
}

‎test/vstest.console.UnitTests/Processors/RunSpecificTestsArgumentProcessorTests.cs‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,64 @@ public void ExecutorShouldRunTestsWhenTestsAreCommaSeparatedWithEscape()
482482
Assert.AreEqual(ArgumentProcessorResult.Success, argumentProcessorResult);
483483
}
484484

485+
[TestMethod]
486+
public void ExecutorShouldDisplayWarningIfNoTestsAreExecuted()
487+
{
488+
var mockTestPlatform = new Mock<ITestPlatform>();
489+
var mockTestRunRequest = new Mock<ITestRunRequest>();
490+
var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();
491+
var mockTestRunStats = new Mock<ITestRunStatistics>();
492+
493+
List<TestCase> list = new List<TestCase>();
494+
list.Add(new TestCase("Test1", new Uri("http://FooTestUri1"), "Source1"));
495+
list.Add(new TestCase("Test2", new Uri("http://FooTestUri2"), "Source2"));
496+
mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Raises(dr => dr.OnDiscoveredTests += null, new DiscoveredTestsEventArgs(list));
497+
498+
mockTestRunRequest.Setup(tr => tr.ExecuteAsync()).Returns(1).Raises(tr => tr.OnRunCompletion += null,
499+
new TestRunCompleteEventArgs(mockTestRunStats.Object, false, false, null, null, new TimeSpan()));
500+
501+
mockTestPlatform.Setup(tp => tp.CreateTestRunRequest(It.IsAny<IRequestData>(), It.IsAny<TestRunCriteria>(), It.IsAny<TestPlatformOptions>())).Returns(mockTestRunRequest.Object);
502+
mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>(), It.IsAny<TestPlatformOptions>())).Returns(mockDiscoveryRequest.Object);
503+
504+
this.ResetAndAddSourceToCommandLineOptions();
505+
var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask);
506+
var executor = GetExecutor(testRequestManager);
507+
508+
executor.Initialize("Test1");
509+
executor.Execute();
510+
511+
this.mockOutput.Verify(op => op.WriteLine(It.Is<string>(st => st.Contains("Additionally, path to test adapters can be specified using /TestAdapterPath command.")), OutputLevel.Warning), Times.Once);
512+
}
513+
514+
[TestMethod]
515+
public void ExecutorShouldNotDisplayWarningIfTestsAreExecuted()
516+
{
517+
var mockTestPlatform = new Mock<ITestPlatform>();
518+
var mockTestRunRequest = new Mock<ITestRunRequest>();
519+
var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();
520+
var testRunStats = new TestRunStatistics(1, new Dictionary<TestOutcome, long> { { TestOutcome.Passed, 1 } });
521+
522+
List<TestCase> list = new List<TestCase>();
523+
list.Add(new TestCase("Test1", new Uri("http://FooTestUri1"), "Source1"));
524+
list.Add(new TestCase("Test2", new Uri("http://FooTestUri2"), "Source2"));
525+
mockDiscoveryRequest.Setup(dr => dr.DiscoverAsync()).Raises(dr => dr.OnDiscoveredTests += null, new DiscoveredTestsEventArgs(list));
526+
527+
mockTestRunRequest.Setup(tr => tr.ExecuteAsync()).Returns(1).Raises(tr => tr.OnRunCompletion += null,
528+
new TestRunCompleteEventArgs(testRunStats, false, false, null, null, new TimeSpan()));
529+
530+
mockTestPlatform.Setup(tp => tp.CreateTestRunRequest(It.IsAny<IRequestData>(), It.IsAny<TestRunCriteria>(), It.IsAny<TestPlatformOptions>())).Returns(mockTestRunRequest.Object);
531+
mockTestPlatform.Setup(tp => tp.CreateDiscoveryRequest(It.IsAny<IRequestData>(), It.IsAny<DiscoveryCriteria>(), It.IsAny<TestPlatformOptions>())).Returns(mockDiscoveryRequest.Object);
532+
533+
this.ResetAndAddSourceToCommandLineOptions();
534+
var testRequestManager = new TestRequestManager(CommandLineOptions.Instance, mockTestPlatform.Object, TestRunResultAggregator.Instance, this.mockTestPlatformEventSource.Object, this.inferHelper, this.mockMetricsPublisherTask);
535+
var executor = GetExecutor(testRequestManager);
536+
537+
executor.Initialize("Test1");
538+
executor.Execute();
539+
540+
this.mockOutput.Verify(op => op.WriteLine(It.Is<string>(st => st.Contains("Additionally, path to test adapters can be specified using /TestAdapterPath command.")), OutputLevel.Warning), Times.Never);
541+
}
542+
485543
#endregion
486544

487545
private void ResetAndAddSourceToCommandLineOptions()

0 commit comments

Comments
 (0)