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 23 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 GetTreatNoTestsAsError(string runSettings)
{
bool failWhenNoTestFound = false;

if (runSettings != null)
{
try
{
RunConfiguration runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(runSettings);
failWhenNoTestFound = GetTreatNoTestsAsError(runConfiguration);
}
catch (SettingsException se)
{
if (EqtTrace.IsErrorEnabled)
{
EqtTrace.Error("RunSettingsUtilities.GetTreatNoTestsAsError: Unable to get the value of TreatNoTestsAsError from runsettings: Error {0}", se);
}
}
}

return failWhenNoTestFound;
}

private static bool GetTreatNoTestsAsError(RunConfiguration runConfiguration)
{
bool treatNoTestsAsError = false;

if (runConfiguration != null)
{
treatNoTestsAsError = runConfiguration.TreatNoTestsAsError;
}

return treatNoTestsAsError;
}


/// <summary>
/// Gets the test adapters path from the run configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ public Framework TargetFramework
this.TargetFrameworkSet = true;
}
}
/// <summary>
/// Gets or sets value indicating exit code when no tests are discovered or executed
/// </summary>
public bool TreatNoTestsAsError
{
get;
set;
}

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

if (this.TreatNoTestsAsError)
{
XmlElement treatAsError = doc.CreateElement(nameof(TreatNoTestsAsError));
treatAsError.InnerText = this.TreatNoTestsAsError.ToString();
root.AppendChild(treatAsError);
}

return root;
}

Expand Down Expand Up @@ -754,7 +769,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 +914,17 @@ public static RunConfiguration FromXml(XmlReader reader)
XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);
runConfiguration.DotnetHostPath = reader.ReadElementContentAsString();
break;
case "TreatNoTestsAsError":
XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);
string treatNoTestsAsErrorValueString = reader.ReadElementContentAsString();
bool treatNoTestsAsError;
if (!bool.TryParse(treatNoTestsAsErrorValueString, out treatNoTestsAsError))
{
throw new SettingsException(string.Format(CultureInfo.CurrentCulture,
Resources.Resources.InvalidSettingsIncorrectValue, Constants.RunConfigurationSettingsName, treatNoTestsAsErrorValueString, elementName));
}
runConfiguration.TreatNoTestsAsError = treatNoTestsAsError;
break;

default:
// Ignore a runsettings element that we don't understand. It could occur in the case
Expand Down
15 changes: 12 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,13 @@ public ArgumentProcessorResult Execute()
// Now that tests are discovered and filtered, we run only those selected tests.
this.ExecuteSelectedTests();

bool treatNoTestsAsError = RunSettingsUtilities.GetTreatNoTestsAsError(effectiveRunSettings);

if (treatNoTestsAsError && this.selectedTestCases.Count == 0)
{
return ArgumentProcessorResult.Fail;
}

return ArgumentProcessorResult.Success;
}

Expand Down Expand Up @@ -285,6 +293,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
25 changes: 19 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,31 @@ 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 treatNoTestsAsError = RunSettingsUtilities.GetTreatNoTestsAsError(runSettings);

if (treatNoTestsAsError && 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 +253,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
72 changes: 72 additions & 0 deletions test/Microsoft.TestPlatform.AcceptanceTests/ExecutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,77 @@ public void IncompatibleSourcesWarningShouldBeDisplayedInTheConsoleOnlyWhenRunni
this.StdOutputContains(expectedWarningContains);
}
}

[TestMethod]
[NetFullTargetFrameworkDataSource]
public void ExitCodeShouldReturnOneWhenTreatNoTestsAsErrorParameterSetToTrueAndNoTestMatchesFilter(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);

// Setting /TestCaseFilter to the test name, which does not exists in the assembly, so we will have 0 tests executed
arguments = string.Concat(arguments, " /TestCaseFilter:TestNameThatMatchesNoTestInTheAssembly");

arguments = string.Concat(arguments, " -- RunConfiguration.TreatNoTestsAsError=true");
this.InvokeVsTest(arguments);

this.ExitCodeEquals(1);
}

[TestMethod]
[NetFullTargetFrameworkDataSource]
public void ExitCodeShouldReturnZeroWhenTreatNoTestsAsErrorParameterSetToFalseAndNoTestMatchesFilter(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);

// Setting /TestCaseFilter to the test name, which does not exists in the assembly, so we will have 0 tests executed
arguments = string.Concat(arguments, " /TestCaseFilter:TestNameThatMatchesNoTestInTheAssembly");

arguments = string.Concat(arguments, " -- RunConfiguration.TreatNoTestsAsError=false");
this.InvokeVsTest(arguments);

this.ExitCodeEquals(0);
}

[TestMethod]
[NetFullTargetFrameworkDataSource]
public void ExitCodeShouldNotDependOnTreatNoTestsAsErrorTrueValueWhenThereAreAnyTestsToRun(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);

arguments = string.Concat(arguments, " -- RunConfiguration.TreatNoTestsAsError=true");
this.InvokeVsTest(arguments);

// Returning 1 because of failing test in test assembly (SimpleTestProject2.dll)
this.ExitCodeEquals(1);
Sanan07 marked this conversation as resolved.
Show resolved Hide resolved
}

[TestMethod]
[NetFullTargetFrameworkDataSource]
public void ExitCodeShouldNotDependOnFailTreatNoTestsAsErrorFalseValueWhenTestsAreAvailable(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);

arguments = string.Concat(arguments, " -- RunConfiguration.TreatNoTestsAsError=false");
this.InvokeVsTest(arguments);

// Returning 1 because of failing test in test assembly (SimpleTestProject2.dll)
this.ExitCodeEquals(1);
}
}
}