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

Implemented functionality to return non-zero value when no tests available. #2610

Merged
merged 24 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4eb1f76
Test space added
Sanan07 May 26, 2020
fd6cbbf
Exception handler was added to catch AccessDeniedException while tryi…
Sanan07 Jun 3, 2020
026226a
Remove unnecessary space
Sanan07 Jun 3, 2020
104ffc0
Deleted unnecessary test. Changed console message and corrected folde…
Sanan07 Jun 3, 2020
2608e88
Remove unnecessary dot
Sanan07 Jun 3, 2020
f8d8aa1
Removed unnecessary lines and usings and coreccted exception message
Sanan07 Jun 3, 2020
0f6bf18
Removed unnecessary line
Sanan07 Jun 3, 2020
d1803c9
Updating resource files
Sanan07 Jun 4, 2020
bf0cacf
New exception handling was added
Sanan07 Jun 11, 2020
3ee2dc7
Merge branch 'master' into accessDeniedException
Sanan07 Jun 11, 2020
2582e75
Formatted exception message
Sanan07 Jun 11, 2020
030fda6
Merge branch 'accessDeniedException'
Sanan07 Jul 2, 2020
ea180c3
Merge branch 'master' of https://github.com/microsoft/vstest
Sanan07 Jul 16, 2020
7a896ff
Merge branch 'master' of https://github.com/microsoft/vstest
Sanan07 Jul 28, 2020
8ea1932
Merge branch 'master' of https://github.com/microsoft/vstest
Sanan07 Jul 31, 2020
740ae68
Merge branch 'master' of https://github.com/microsoft/vstest
Sanan07 Aug 6, 2020
f69fca2
Merge branch 'master' of https://github.com/Sanan07/vstest
Sanan07 Aug 14, 2020
35c0de0
Merge branch 'master' of https://github.com/microsoft/vstest into master
Sanan07 Sep 15, 2020
deffdd6
Merge branch 'master' of https://github.com/Sanan07/vstest
Sanan07 Oct 6, 2020
ba1febe
Merge branch 'master' of https://github.com/microsoft/vstest into master
Sanan07 Oct 8, 2020
a97cdd1
Initial implementaion
Sanan07 Oct 29, 2020
d55e7ea
Added acceptance tests. Removed unnecessary console output
Sanan07 Oct 30, 2020
d0f3773
Review changes
Sanan07 Nov 2, 2020
59c2327
Changing the name of the test
Sanan07 Nov 3, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,46 @@ public static int GetMaxCpuCount(RunConfiguration runConfiguration)

return cpuCount;
}
/// <summary>
/// Gets the value of FailWhenNoTestsFound parameter from runsettings file
/// </summary>
/// <param name="runSettings">Runsetting string value</param>
/// <returns>The value of FailWhenNoTestsFound</returns>
public static bool GetFailWhenNoTestsFound(string runSettings)
{
bool failWhenNoTestFound = false;

if (runSettings != null)
{
try
{
RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(runSettings);
failWhenNoTestFound = GetFailWhenNoTestsFound(runConfiguration);
}
catch (SettingsException se)
{
if (EqtTrace.IsErrorEnabled)
{
EqtTrace.Error("RunSpecificTestsArgumentProcessor.GetFailWhenNoTestsFound: Unable to get the value of FailWhenNoTestsFound from runsettings: Error {0}", se);
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

return failWhenNoTestFound;
}

private static bool GetFailWhenNoTestsFound(RunConfiguration runConfiguration)
{
bool failWhenNoTestsFound = false;

if (runConfiguration != null)
{
failWhenNoTestsFound = runConfiguration.FailWhenNoTestsFound;
}

return failWhenNoTestsFound;
}


/// <summary>
/// Gets the test adapters path from the run configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public class RunConfiguration : TestRunSettings
/// </summary>
private string targetDevice;

/// <summary>
/// Defines if Test Platform should return non-zero value if no tests found and executed
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
private bool failWhenNoTestsFound;

#endregion

#region Constructor
Expand Down Expand Up @@ -308,6 +313,18 @@ public Framework TargetFramework
}
}

public bool FailWhenNoTestsFound
{
get
{
return failWhenNoTestsFound;
}
set
{
this.failWhenNoTestsFound = value;
}
}

/// <summary>
/// Gets or sets the target Framework this run is targeting. Possible values are Framework3.5|Framework4.0|Framework4.5
/// </summary>
Expand Down Expand Up @@ -594,6 +611,13 @@ public override XmlElement ToXml()
root.AppendChild(dotnetHostPath);
}

if (this.FailWhenNoTestsFound)
{
XmlElement treatAsError = doc.CreateElement(nameof(FailWhenNoTestsFound));
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
treatAsError.InnerText = this.FailWhenNoTestsFound.ToString();
root.AppendChild(treatAsError);
}

return root;
}

Expand Down Expand Up @@ -754,7 +778,7 @@ public static RunConfiguration FromXml(XmlReader reader)
bool disableParallelizationCheck;
if (!bool.TryParse(disableParallelizationValueString, out disableParallelizationCheck))
{
throw new SettingsException(String.Format(CultureInfo.CurrentCulture,
throw new SettingsException(string.Format(CultureInfo.CurrentCulture,
Resources.Resources.InvalidSettingsIncorrectValue, Constants.RunConfigurationSettingsName, disableParallelizationValueString, elementName));
}
runConfiguration.DisableParallelization = disableParallelizationCheck;
Expand Down Expand Up @@ -899,6 +923,17 @@ public static RunConfiguration FromXml(XmlReader reader)
XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);
runConfiguration.DotnetHostPath = reader.ReadElementContentAsString();
break;
case "FailWhenNoTestsFound":
XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);
string failWhenNoTestsFoundValueString = reader.ReadElementContentAsString();
bool failWhenNoTestsFound;
if (!bool.TryParse(failWhenNoTestsFoundValueString, out failWhenNoTestsFound))
{
throw new SettingsException(string.Format(CultureInfo.CurrentCulture,
Resources.Resources.InvalidSettingsIncorrectValue, Constants.RunConfigurationSettingsName, failWhenNoTestsFoundValueString, elementName));
}
runConfiguration.FailWhenNoTestsFound = failWhenNoTestsFound;
break;

default:
// Ignore a runsettings element that we don't understand. It could occur in the case
Expand Down
16 changes: 13 additions & 3 deletions src/vstest.console/Processors/RunSpecificTestsArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors
using System.Globalization;
using System.Linq;

using Microsoft.VisualStudio.TestPlatform.Common;
using Microsoft.VisualStudio.TestPlatform.Common.Interfaces;
using Microsoft.VisualStudio.TestPlatform.Common.Utilities;
using Microsoft.VisualStudio.TestPlatform.Client.RequestHelper;
using Microsoft.VisualStudio.TestPlatform.CommandLine.Internal;
using Microsoft.VisualStudio.TestPlatform.CommandLine.TestPlatformHelpers;
using Microsoft.VisualStudio.TestPlatform.Common;
using Microsoft.VisualStudio.TestPlatform.Common.Interfaces;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources;
using CommandLineResources = Resources.Resources;

internal class RunSpecificTestsArgumentProcessor : IArgumentProcessor
{
Expand Down Expand Up @@ -218,6 +219,14 @@ public ArgumentProcessorResult Execute()
// Now that tests are discovered and filtered, we run only those selected tests.
this.ExecuteSelectedTests();

bool failWhenNoTestsFound = RunSettingsUtilities.GetFailWhenNoTestsFound(effectiveRunSettings);
nohwnd marked this conversation as resolved.
Show resolved Hide resolved

// If no tests found and FailWhenNoTestsFound parameter set to `true` then return fail
if (failWhenNoTestsFound && this.selectedTestCases.Count == 0)
{
return ArgumentProcessorResult.Fail;
}

return ArgumentProcessorResult.Success;
}

Expand Down Expand Up @@ -285,6 +294,7 @@ private void ExecuteSelectedTests()
}
}


/// <summary>
/// Filter discovered tests and find matching tests from given search strings.
/// Any name of the test that can match multiple strings will be added only once.
Expand Down
26 changes: 20 additions & 6 deletions src/vstest.console/Processors/RunTestsArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors
{
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
Expand All @@ -14,11 +13,12 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.Processors
using Microsoft.VisualStudio.TestPlatform.CommandLine.TestPlatformHelpers;
using Microsoft.VisualStudio.TestPlatform.Common;
using Microsoft.VisualStudio.TestPlatform.Common.Interfaces;
using Microsoft.VisualStudio.TestPlatform.Common.Utilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.Utilities;

using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources;
using CommandLineResources = Resources.Resources;

internal class RunTestsArgumentProcessor : IArgumentProcessor
{
Expand Down Expand Up @@ -110,6 +110,11 @@ internal class RunTestsArgumentExecutor : IArgumentExecutor
/// Registers and Unregisters for test run events before and after test run
/// </summary>
private ITestRunEventsRegistrar testRunEventsRegistrar;

/// <summary>
/// Shows the number of tests which were executed
/// </summary>
private static long numberOfExecutedTests;

#endregion

Expand Down Expand Up @@ -168,24 +173,32 @@ public ArgumentProcessorResult Execute()
this.output.Information(false, CommandLineResources.VstestDiagLogOutputPath, EqtTrace.LogFile);
}

var runSettings = this.runSettingsManager.ActiveRunSettings.SettingsXml;

if (this.commandLineOptions.Sources.Any())
{
this.RunTests(this.commandLineOptions.Sources);
this.RunTests(runSettings);
}

bool failWhenNoTestsFound = RunSettingsUtilities.GetFailWhenNoTestsFound(runSettings);

// If no tests found and FailWhenNoTestsFound parameter set to `true` then return fail
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
if (failWhenNoTestsFound && numberOfExecutedTests == 0)
{
return ArgumentProcessorResult.Fail;
}

return ArgumentProcessorResult.Success;
}

private void RunTests(IEnumerable<string> sources)
private void RunTests(string runSettings)
{
// create/start test run
if (EqtTrace.IsInfoEnabled)
{
EqtTrace.Info("RunTestsArgumentProcessor:Execute: Test run is starting.");
}

var runSettings = this.runSettingsManager.ActiveRunSettings.SettingsXml;

if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("RunTestsArgumentProcessor:Execute: Queuing Test run.");
Expand Down Expand Up @@ -241,6 +254,7 @@ private void TestRunRequest_OnRunCompletion(object sender, TestRunCompleteEventA
// we need to check if there are any tests executed - to try show some help info to user to check for installed vsix extensions
if (!e.IsAborted && !e.IsCanceled)
{
numberOfExecutedTests = e.TestRunStatistics.ExecutedTests;
var testsFoundInAnySource = (e.TestRunStatistics == null) ? false : (e.TestRunStatistics.ExecutedTests > 0);

// Indicate the user to use test adapter path command if there are no tests found
Expand Down
11 changes: 6 additions & 5 deletions src/vstest.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
namespace Microsoft.VisualStudio.TestPlatform.CommandLine
{
using System;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using Microsoft.VisualStudio.TestPlatform.Utilities;

/// <summary>
Expand All @@ -24,21 +26,20 @@ public static int Main(string[] args)
{
ConsoleOutput.Instance.WriteLine("Waiting for debugger attach...", OutputLevel.Information);

var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
var currentProcess = Process.GetCurrentProcess();
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
ConsoleOutput.Instance.WriteLine(
string.Format("Process Id: {0}, Name: {1}", currentProcess.Id, currentProcess.ProcessName),
OutputLevel.Information);

while (!System.Diagnostics.Debugger.IsAttached)
while (!Debugger.IsAttached)
{
System.Threading.Thread.Sleep(1000);
Thread.Sleep(1000);
}

System.Diagnostics.Debugger.Break();
Debugger.Break();
}

SetCultureSpecifiedByUser();

return new Executor(ConsoleOutput.Instance).Execute(args);
}

Expand Down
74 changes: 74 additions & 0 deletions test/Microsoft.TestPlatform.AcceptanceTests/ExecutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,79 @@ public void IncompatibleSourcesWarningShouldBeDisplayedInTheConsoleOnlyWhenRunni
this.StdOutputContains(expectedWarningContains);
}
}

[TestMethod]
[NetFullTargetFrameworkDataSource]
public void ExitCodeShouldReturnOneWhenFailWhenNoTestsFoundParameterSetToTrueAndNoTestAvailableWithFilter(RunnerInfo runnerInfo)
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
{
SetTestEnvironment(this.testEnvironment, runnerInfo);

var assemblyPaths = this.BuildMultipleAssemblyPath("SimpleTestProject2.dll").Trim('\"');

var arguments = PrepareArguments(assemblyPaths, this.GetTestAdapterPath(), string.Empty, this.FrameworkArgValue, runnerInfo.InIsolationValue);

// Setting /TestCaseFilter to random test name, which does not exists
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
arguments = string.Concat(arguments, " /TestCaseFilter:SomeRandomTestName");

// set FailWhenNoTestsFound to true
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
arguments = string.Concat(arguments, " -- RunConfiguration.FailWhenNoTestsFound=true");
this.InvokeVsTest(arguments);

this.ExitCodeEquals(1);
}

[TestMethod]
[NetFullTargetFrameworkDataSource]
public void ExitCodeShouldReturnZeroWhenFailWhenNoTestsFoundParameterSetToTrueAndNoTestAvailableWithFilter(RunnerInfo runnerInfo)
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
{
SetTestEnvironment(this.testEnvironment, runnerInfo);

var assemblyPaths = this.BuildMultipleAssemblyPath("SimpleTestProject2.dll").Trim('\"');

var arguments = PrepareArguments(assemblyPaths, this.GetTestAdapterPath(), string.Empty, this.FrameworkArgValue, runnerInfo.InIsolationValue);

// Setting /TestCaseFilter to random test name, which does not exists
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
arguments = string.Concat(arguments, " /TestCaseFilter:SomeRandomTestName");

// set FailWhenNoTestsFound to true
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
arguments = string.Concat(arguments, " -- RunConfiguration.FailWhenNoTestsFound=false");
this.InvokeVsTest(arguments);

this.ExitCodeEquals(0);
}

[TestMethod]
[NetFullTargetFrameworkDataSource]
public void ExitCodeShouldNotDependOnFailWhenNoTestsFoundTrueValueWhenTestsAreAvailable(RunnerInfo runnerInfo)
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
{
SetTestEnvironment(this.testEnvironment, runnerInfo);

var assemblyPaths = this.BuildMultipleAssemblyPath("SimpleTestProject2.dll").Trim('\"');

var arguments = PrepareArguments(assemblyPaths, this.GetTestAdapterPath(), string.Empty, this.FrameworkArgValue, runnerInfo.InIsolationValue);

// set FailWhenNoTestsFound to true
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
arguments = string.Concat(arguments, " -- RunConfiguration.FailWhenNoTestsFound=true");
this.InvokeVsTest(arguments);

this.ExitCodeEquals(1);
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
}

[TestMethod]
[NetFullTargetFrameworkDataSource]
public void ExitCodeShouldNotDependOnFailWhenNoTestsFoundFalseValueWhenTestsAreAvailable(RunnerInfo runnerInfo)
{
SetTestEnvironment(this.testEnvironment, runnerInfo);

var assemblyPaths = this.BuildMultipleAssemblyPath("SimpleTestProject2.dll").Trim('\"');

var arguments = PrepareArguments(assemblyPaths, this.GetTestAdapterPath(), string.Empty, this.FrameworkArgValue, runnerInfo.InIsolationValue);

// set FailWhenNoTestsFound to true
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
arguments = string.Concat(arguments, " -- RunConfiguration.FailWhenNoTestsFound=true");
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
this.InvokeVsTest(arguments);

this.ExitCodeEquals(1);
}
}
}