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

Make test functions static when possible #3830

Merged
merged 1 commit into from
Jun 29, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected string GetTargetFramworkForRunsettings()
/// Empty runsettings, just with the RunSettings tag that we require.
/// </summary>
/// <returns></returns>
public string GetEmptyRunsettings()
public static string GetEmptyRunsettings()
{
return "<RunSettings></RunSettings>";
}
Expand All @@ -133,7 +133,7 @@ public string GetDefaultRunSettings()
/// Use the overload without any parameters to get the target framework from the currently set test environment.
/// </summary>
/// <returns></returns>
public string GetRunSettingsWithTargetFramework(string targetFramework)
public static string GetRunSettingsWithTargetFramework(string targetFramework)
{
string runSettingsXml =
$@"<?xml version=""1.0"" encoding=""utf-8""?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ private void VaildateOutput(TempDirectory tempDirectory, string testName, bool v
Assert.IsTrue(isValid, "Sequence attachment is not valid.");
}

private bool IsValidXml(string xmlFilePath)
private static bool IsValidXml(string xmlFilePath)
{
var file = File.OpenRead(xmlFilePath);
var reader = XmlReader.Create(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ public class CodeCoverageAcceptanceTestBase : AcceptanceTestBase
*/
protected const double ExpectedMinimalModuleCoverage = 30.0;

protected string GetNetStandardAdapterPath()
protected static string GetNetStandardAdapterPath()
{
return Path.Combine(IntegrationTestEnvironment.TestPlatformRootDirectory, "artifacts", IntegrationTestEnvironment.BuildConfiguration, "Microsoft.CodeCoverage");
}

protected string GetNetFrameworkAdapterPath()
protected static string GetNetFrameworkAdapterPath()
{
return Path.Combine(IntegrationTestEnvironment.TestPlatformRootDirectory, "artifacts", IntegrationTestEnvironment.BuildConfiguration, "net451", "win7-x64", "Extensions");
}

protected string GetCodeCoverageExePath()
protected static string GetCodeCoverageExePath()
{
return Path.Combine(GetNetStandardAdapterPath(), "CodeCoverage", "CodeCoverage.exe");
}

protected XmlNode? GetModuleNode(XmlNode node, string name)
protected static XmlNode? GetModuleNode(XmlNode node, string name)
{
var moduleNode = GetNode(node, "module", name);

Expand All @@ -52,12 +52,12 @@ protected string GetCodeCoverageExePath()
return moduleNode;
}

protected XmlNode? GetNode(XmlNode node, string type, string name)
protected static XmlNode? GetNode(XmlNode node, string type, string name)
{
return node.SelectSingleNode($"//{type}[@name='{name}']") ?? node.SelectSingleNode($"//{type}[@name='{name.ToLower()}']");
}

protected XmlDocument GetXmlCoverage(string coverageResult, TempDirectory tempDirectory)
protected static XmlDocument GetXmlCoverage(string coverageResult, TempDirectory tempDirectory)
{
var coverage = new XmlDocument();

Expand Down Expand Up @@ -94,7 +94,7 @@ protected XmlDocument GetXmlCoverage(string coverageResult, TempDirectory tempDi
return coverage;
}

protected void AssertCoverage(XmlNode node, double expectedCoverage)
protected static void AssertCoverage(XmlNode node, double expectedCoverage)
{
var coverage = node.Attributes!["block_coverage"] != null
? double.Parse(node.Attributes!["block_coverage"]!.Value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private string CreateArguments(
return arguments;
}

private void AssertSkippedMethod(XmlDocument document)
private static void AssertSkippedMethod(XmlDocument document)
{
var module = GetModuleNode(document.DocumentElement!, "codecoveragetest.dll");
Assert.IsNotNull(module);
Expand All @@ -321,7 +321,7 @@ private void AssertSkippedMethod(XmlDocument document)
Assert.IsNotNull(testAbsFunction);
}

private void ValidateCoverageData(XmlDocument document, string moduleName, bool validateSourceFileNames)
private static void ValidateCoverageData(XmlDocument document, string moduleName, bool validateSourceFileNames)
{
var module = GetModuleNode(document.DocumentElement!, moduleName.ToLower());

Expand All @@ -340,7 +340,7 @@ private void ValidateCoverageData(XmlDocument document, string moduleName, bool
}
}

private void AssertSourceFileName(XmlNode module)
private static void AssertSourceFileName(XmlNode module)
{
const string expectedFileName = "UnitTest1.cs";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private void VaildateDataCollectorOutput(string resultsDir)
Assert.AreEqual(3, diaglogsFileCount);
}

private string GetRunsettingsFilePath(string resultsDir)
private static string GetRunsettingsFilePath(string resultsDir)
{
var runsettingsPath = Path.Combine(resultsDir, "test_" + Guid.NewGuid() + ".runsettings");
var dataCollectionAttributes = new Dictionary<string, string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private void RunTests(RunnerInfo runnerInfo, string testAssembly, int passedTest
ValidateSummaryStatus(passedTestCount, 0, 0);
}

private string GetRunsettingsFilePath(TempDirectory tempDirectory, Dictionary<string, string> runConfigurationDictionary)
private static string GetRunsettingsFilePath(TempDirectory tempDirectory, Dictionary<string, string> runConfigurationDictionary)
{
var runsettingsPath = Path.Combine(tempDirectory.Path, "test_" + Guid.NewGuid() + ".runsettings");
CreateRunSettingsFile(runsettingsPath, runConfigurationDictionary);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void TestMethod1()
Assert.AreEqual(0, exitCode, stdOut);
}

private string GetLatestSdkVersion(string dotnetPath)
private static string GetLatestSdkVersion(string dotnetPath)
=> Path.GetFileName(Directory.GetDirectories(Path.Combine(Path.GetDirectoryName(dotnetPath)!, @"shared/Microsoft.NETCore.App")).OrderByDescending(x => x).First());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void EventLogDataCollectorShoudCreateLogFileWithoutEventsIfEventsAreNotLo
StdErrorDoesNotContains("Unable to read event log");
}

private string GetRunsettingsFilePath(TempDirectory tempDirectory)
private static string GetRunsettingsFilePath(TempDirectory tempDirectory)
{
var runsettingsPath = Path.Combine(tempDirectory.Path, "test_" + Guid.NewGuid() + ".runsettings");

Expand Down Expand Up @@ -115,7 +115,7 @@ private void VaildateDataCollectorOutput(TempDirectory tempDirectory)
Assert.IsTrue(VerifyOrder(fileContent4, new[] { "110", "111", "112", "220", "221", "222", "223", "330", "331", "332" }), string.Format("Event log file content: {0}", fileContent4));
}

private bool VerifyOrder2(string content, Dictionary<string[], bool> eventIdsDics)
private static bool VerifyOrder2(string content, Dictionary<string[], bool> eventIdsDics)
{
foreach (var eventIds in eventIdsDics)
{
Expand All @@ -131,7 +131,7 @@ private bool VerifyOrder2(string content, Dictionary<string[], bool> eventIdsDic
return false;
}

private bool VerifyOrder(string content, string[] eventIds)
private static bool VerifyOrder(string content, string[] eventIds)
{
for (int i = 0; i < eventIds.Length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ private DebugInfo GetDebugInfo()
};
}

private RunnerInfo GetRunnerInfo(string batch, string runnerFramework, string hostFramework, bool inIsolation)
private static RunnerInfo GetRunnerInfo(string batch, string runnerFramework, string hostFramework, bool inIsolation)
{
return new RunnerInfo
{
Expand All @@ -321,7 +321,7 @@ private RunnerInfo GetRunnerInfo(string batch, string runnerFramework, string ho
};
}

private DllInfo GetMSTestInfo(string msTestVersion)
private static DllInfo GetMSTestInfo(string msTestVersion)
{
var depsXml = GetDependenciesXml();

Expand Down
2 changes: 1 addition & 1 deletion test/Microsoft.TestPlatform.AcceptanceTests/LoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void TrxLoggerResultSummaryOutcomeValueShouldNotChangeIfNoTestsExecutedAn
Assert.AreEqual("Completed", outcomeValue);
}

private bool IsValidXml(string xmlFilePath)
private static bool IsValidXml(string xmlFilePath)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private void DiscoverTests(RunnerInfo runnerInfo)
ValidateOutput("Discovery", TempDirectory);
}

private void ValidateOutput(string command, TempDirectory tempDirectory)
private static void ValidateOutput(string command, TempDirectory tempDirectory)
{
if (!Directory.Exists(tempDirectory.Path))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ private IList<string> GetTestAssemblies()
return GetProjects().Select(p => GetAssetFullPath(p)).ToList();
}

private IList<string> GetProjects()
private static IList<string> GetProjects()
{
return new List<string> { "SimpleTestProject.dll", "SimpleTestProject2.dll" };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void IdGeneration_ExtremelyLarge_TestVectors_10M_abc()
);
}

private void IdGeneration_TestVector(string testName, string expected)
private static void IdGeneration_TestVector(string testName, string expected)
{
// Arrange
expected = expected.Replace(" ", "").ToLowerInvariant();
Expand All @@ -118,7 +118,7 @@ private void IdGeneration_TestVector(string testName, string expected)
Assert.AreEqual(expected, actual, $"Test Id for '{testName}' is invalid!");
}

private void IdGeneration_TestRepetitionVector(string input, int repetition, string expected)
private static void IdGeneration_TestRepetitionVector(string input, int repetition, string expected)
{
// Arrange
var idProvider = new AdapterUtilities.TestIdProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private static T Deserialize<T>(string json, int version = 1)
return JsonDataSerializer.Instance.Deserialize<T>(json, version);
}

private void VerifyDummyPropertyIsRegistered()
private static void VerifyDummyPropertyIsRegistered()
{
var dummyProperty = TestProperty.Find("DummyProperty");
Assert.IsNotNull(dummyProperty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ public void StartTestRunShouldAttemptToTakeProxyFromPoolIfProxyIsNull()
}
}

private void SignalEvent(ManualResetEvent manualResetEvent)
private static void SignalEvent(ManualResetEvent manualResetEvent)
{
// Wait for the 100 ms.
Task.Delay(200).Wait();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,5 @@ public string[] GetPerfAssetFullPath(string name, string framework = "net6.0")
/// <returns></returns>

// DONT make this just <RunSettings></RunSettings> it makes Translation layer hang... https://github.com/microsoft/vstest/issues/3519
public string GetDefaultRunSettings() => "<RunSettings><RunConfiguration></RunConfiguration></RunSettings>";
public static string GetDefaultRunSettings() => "<RunSettings><RunConfiguration></RunConfiguration></RunSettings>";
}
10 changes: 5 additions & 5 deletions test/Microsoft.TestPlatform.TestUtilities/IntegrationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public void InvokeVsTestForDiscovery(string testAssembly, string testAdapterPath
/// <param name="runnerFramework">Runner Framework</param>
/// <param name="framework">Framework for which Tests are not supported</param>
/// <param name="message">Message to be shown</param>
public void ExecuteNotSupportedRunnerFrameworkTests(string runnerFramework, string framework, string message)
public static void ExecuteNotSupportedRunnerFrameworkTests(string runnerFramework, string framework, string message)
{
if (runnerFramework.StartsWith(framework))
{
Expand Down Expand Up @@ -578,19 +578,19 @@ protected string GetTestAdapterPath(UnitTestFramework testFramework = UnitTestFr

if (testFramework == UnitTestFramework.MSTest)
{
adapterRelativePath = string.Format(_testAdapterRelativePath, _testEnvironment.DependencyVersions["MSTestAdapterVersion"]);
adapterRelativePath = string.Format(_testAdapterRelativePath, IntegrationTestEnvironment.DependencyVersions["MSTestAdapterVersion"]);
}
else if (testFramework == UnitTestFramework.NUnit)
{
adapterRelativePath = string.Format(_nUnitTestAdapterRelativePath, _testEnvironment.DependencyVersions["NUnit3AdapterVersion"]);
adapterRelativePath = string.Format(_nUnitTestAdapterRelativePath, IntegrationTestEnvironment.DependencyVersions["NUnit3AdapterVersion"]);
}
else if (testFramework == UnitTestFramework.XUnit)
{
adapterRelativePath = string.Format(_xUnitTestAdapterRelativePath, _testEnvironment.DependencyVersions["XUnitAdapterVersion"]);
adapterRelativePath = string.Format(_xUnitTestAdapterRelativePath, IntegrationTestEnvironment.DependencyVersions["XUnitAdapterVersion"]);
}
else if (testFramework == UnitTestFramework.Chutzpah)
{
adapterRelativePath = string.Format(_chutzpahTestAdapterRelativePath, _testEnvironment.DependencyVersions["ChutzpahAdapterVersion"]);
adapterRelativePath = string.Format(_chutzpahTestAdapterRelativePath, IntegrationTestEnvironment.DependencyVersions["ChutzpahAdapterVersion"]);
}

return _testEnvironment.GetNugetPackage(adapterRelativePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static string BuildConfiguration
}
}

public Dictionary<string, string> DependencyVersions
public static Dictionary<string, string> DependencyVersions
=> s_dependencyVersions ??= GetDependencies(TestPlatformRootDirectory);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public void RemoveChildNodeShouldRemoveXmlIfExist()

#region Private Methods

private XmlDocument GetXmlDocument(string settingsXml)
private static XmlDocument GetXmlDocument(string settingsXml)
{
var doc = new XmlDocument();
doc.LoadXml(settingsXml);
Expand Down
8 changes: 4 additions & 4 deletions test/vstest.console.UnitTests/Internal/ConsoleLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1232,12 +1232,12 @@ public void ResultsInHeirarchichalOrderShouldReportCorrectCount()
_mockOutput.Verify(o => o.WriteLine(string.Format(CultureInfo.CurrentCulture, CommandLineResources.TestRunSummaryTotalTests, 2), OutputLevel.Information), Times.Once());
}

private TestCase CreateTestCase(string testCaseName)
private static TestCase CreateTestCase(string testCaseName)
{
return new TestCase(testCaseName, new Uri("some://uri"), "DummySourceFileName");
}

private List<ObjectModel.TestResult> GetTestResultsObject()
private static List<ObjectModel.TestResult> GetTestResultsObject()
{
var testcase = new TestCase("DymmyNamespace.DummyClass.TestName", new Uri("some://uri"), "TestSource")
{
Expand Down Expand Up @@ -1282,7 +1282,7 @@ private TestCase CreateTestCase(string testCaseName)
return testresultList;
}

private List<ObjectModel.TestResult> GetPassedTestResultsObject()
private static List<ObjectModel.TestResult> GetPassedTestResultsObject()
{
var testcase = new TestCase("DymmyNamespace.DummyClass.TestName", new Uri("some://uri"), "TestSourcePassed")
{
Expand All @@ -1309,7 +1309,7 @@ private TestCase CreateTestCase(string testCaseName)
}


private List<ObjectModel.TestResult> GetTestResultObject(TestOutcome outcome)
private static List<ObjectModel.TestResult> GetTestResultObject(TestOutcome outcome)
{
var testcase = new TestCase("TestName", new Uri("some://uri"), "TestSource");
var testresult = new ObjectModel.TestResult(testcase)
Expand Down
Loading