From f9760dc405519d376ec273556f1a9abd7a4eb647 Mon Sep 17 00:00:00 2001 From: Christian Soltenborn Date: Sun, 15 Nov 2015 17:40:53 +0100 Subject: [PATCH] unit tests for RunSettingsService --- .../GoogleTestAdapter/Options.cs | 7 +- .../AbstractGoogleTestExtensionTests.cs | 3 + .../GoogleTestAdapterTests.csproj | 18 +++++ .../GoogleTestAdapterTests/OptionsTests.cs | 80 +++++++++++++++++++ .../Solution.gta.runsettings | 9 +++ .../RunSettingsServiceTests/User.runsettings | 9 +++ 6 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 GoogleTestExtension/GoogleTestAdapterTests/Resources/TestData/RunSettingsServiceTests/Solution.gta.runsettings create mode 100644 GoogleTestExtension/GoogleTestAdapterTests/Resources/TestData/RunSettingsServiceTests/User.runsettings diff --git a/GoogleTestExtension/GoogleTestAdapter/Options.cs b/GoogleTestExtension/GoogleTestAdapter/Options.cs index 213fa00a8..d85052fd6 100644 --- a/GoogleTestExtension/GoogleTestAdapter/Options.cs +++ b/GoogleTestExtension/GoogleTestAdapter/Options.cs @@ -25,10 +25,12 @@ public class RunSettingsService : IRunSettingsService { public string Name { get { return GoogleTestConstants.SettingsName; } } + public string SolutionSettingsFile_ForTesting { get; set; } = null; + private IGlobalRunSettings globalRunSettings; [ImportingConstructor] - RunSettingsService([Import(typeof(IGlobalRunSettings))] IGlobalRunSettings globalRunSettings) + public RunSettingsService([Import(typeof(IGlobalRunSettings))] IGlobalRunSettings globalRunSettings) { this.globalRunSettings = globalRunSettings; } @@ -49,7 +51,8 @@ public IXPathNavigable AddRunSettings(IXPathNavigable userRunSettingDocument, IR userRunSettingsNavigator.DeleteSelf(); // this node is to be replaced by the final run settings } - string solutionRunSettingsFile = GetSolutionSettingsXmlFile(); + // FIXME test code + string solutionRunSettingsFile = SolutionSettingsFile_ForTesting ?? GetSolutionSettingsXmlFile(); try { if (File.Exists(solutionRunSettingsFile)) diff --git a/GoogleTestExtension/GoogleTestAdapterTests/AbstractGoogleTestExtensionTests.cs b/GoogleTestExtension/GoogleTestAdapterTests/AbstractGoogleTestExtensionTests.cs index 748d954f0..b128dbefa 100644 --- a/GoogleTestExtension/GoogleTestAdapterTests/AbstractGoogleTestExtensionTests.cs +++ b/GoogleTestExtension/GoogleTestAdapterTests/AbstractGoogleTestExtensionTests.cs @@ -36,6 +36,9 @@ public abstract class AbstractGoogleTestExtensionTests protected const string XmlFile2 = TestdataDir + @"SampleResult2.xml"; protected const string XmlFileBroken = TestdataDir + @"SampleResult1_Broken.xml"; + protected const string SolutionTestSettings = TestdataDir + @"RunSettingsServiceTests\Solution" + GoogleTestConstants.SettingsExtension; + protected const string UserTestSettings = TestdataDir + @"RunSettingsServiceTests\User.runsettings"; + protected const string DummyExecutable = "ff.exe"; diff --git a/GoogleTestExtension/GoogleTestAdapterTests/GoogleTestAdapterTests.csproj b/GoogleTestExtension/GoogleTestAdapterTests/GoogleTestAdapterTests.csproj index 7b4ea8292..e9cd7eb39 100644 --- a/GoogleTestExtension/GoogleTestAdapterTests/GoogleTestAdapterTests.csproj +++ b/GoogleTestExtension/GoogleTestAdapterTests/GoogleTestAdapterTests.csproj @@ -47,11 +47,23 @@ ..\lib\Microsoft.VisualStudio.TestPlatform.ObjectModel.dll + + False + ..\lib\Microsoft.VisualStudio.TestWindow.dll + + + ..\lib\Microsoft.VisualStudio.TestWindow.Core.dll + + + False + ..\lib\Microsoft.VisualStudio.TestWindow.Interfaces.dll + ..\packages\Moq.4.2.1507.0118\lib\net40\Moq.dll True + @@ -97,6 +109,12 @@ + + PreserveNewest + + + PreserveNewest + diff --git a/GoogleTestExtension/GoogleTestAdapterTests/OptionsTests.cs b/GoogleTestExtension/GoogleTestAdapterTests/OptionsTests.cs index bdd606e7e..c1e4d3fd3 100644 --- a/GoogleTestExtension/GoogleTestAdapterTests/OptionsTests.cs +++ b/GoogleTestExtension/GoogleTestAdapterTests/OptionsTests.cs @@ -2,6 +2,9 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using GoogleTestAdapter.Helpers; +using System.Xml.XPath; +using Microsoft.VisualStudio.TestWindow.Extensibility; +using System.Xml; namespace GoogleTestAdapter { @@ -106,6 +109,83 @@ public void AdditionalTestParameter_PlaceholdersAreTreatedCorrectly() Assert.AreEqual("mydir, 4711", result); } + [TestMethod] + public void RunSettingsService_Instantiation_HasCorrectName() + { + Assert.AreEqual(GoogleTestConstants.SettingsName, new RunSettingsService(null).Name); + } + + [TestMethod] + public void RunSettingsService_GentlyHandlesBrokenSolutionSettings() + { + Mock mockLogger = new Mock(); + Mock mockRunSettingsConfigInfo = new Mock(); + + RunSettingsService service = SetupRunSettingsService(mockLogger); + service.SolutionSettingsFile_ForTesting = XmlFileBroken; + + XmlDocument xml = new XmlDocument(); + xml.Load(UserTestSettings); + + service.AddRunSettings(xml, mockRunSettingsConfigInfo.Object, mockLogger.Object); + + // 1: from global, 2: from solution, 3: from user test settings + AssertContainsSetting(xml, "AdditionalTestExecutionParam", "Global"); + AssertContainsSetting(xml, "BatchForTestTeardown", "User"); + AssertContainsSetting(xml, "NrOfTestRepetitions", "1"); + AssertContainsSetting(xml, "MaxNrOfThreads", "3"); + AssertContainsSetting(xml, "ShuffleTestsSeed", "3"); + AssertContainsSetting(xml, "ReportWaitPeriod", "3"); + + mockLogger.Verify(l => l.Log(It.Is(ml => ml == MessageLevel.Warning), It.Is(s => s.Contains("could not be parsed"))), + Times.Exactly(1)); + } + + [TestMethod] + public void RunSettingsService_CorrectOverridingHierarchy() + { + Mock mockLogger = new Mock(); + Mock mockRunSettingsConfigInfo = new Mock(); + + RunSettingsService service = SetupRunSettingsService(mockLogger); + service.SolutionSettingsFile_ForTesting = SolutionTestSettings; + + XmlDocument xml = new XmlDocument(); + xml.Load(UserTestSettings); + + service.AddRunSettings(xml, mockRunSettingsConfigInfo.Object, mockLogger.Object); + + // 1: from global, 2: from solution, 3: from user test settings + AssertContainsSetting(xml, "AdditionalTestExecutionParam", "Global"); + AssertContainsSetting(xml, "BatchForTestSetup", "Solution"); + AssertContainsSetting(xml, "BatchForTestTeardown", "User"); + AssertContainsSetting(xml, "NrOfTestRepetitions", "2"); + AssertContainsSetting(xml, "MaxNrOfThreads", "3"); + AssertContainsSetting(xml, "ShuffleTestsSeed", "3"); + AssertContainsSetting(xml, "ReportWaitPeriod", "3"); + } + + private RunSettingsService SetupRunSettingsService(Mock mockLogger) + { + Mock mockGlobalRunSettings = new Mock(); + RunSettings globalRunSettings = new RunSettings(); + globalRunSettings.AdditionalTestExecutionParam = "Global"; + globalRunSettings.NrOfTestRepetitions = 1; + globalRunSettings.MaxNrOfThreads = 1; + globalRunSettings.ReportWaitPeriod = 1; + mockGlobalRunSettings.Setup(grs => grs.RunSettings).Returns(globalRunSettings); + + return new RunSettingsService(mockGlobalRunSettings.Object); + } + + private void AssertContainsSetting(XmlDocument xml, string nodeName, string value) + { + XmlNodeList list = xml.GetElementsByTagName(nodeName); + Assert.IsTrue(list.Count == 1); + XmlNode node = list.Item(0); + Assert.AreEqual(value, node.InnerText); + } + } } \ No newline at end of file diff --git a/GoogleTestExtension/GoogleTestAdapterTests/Resources/TestData/RunSettingsServiceTests/Solution.gta.runsettings b/GoogleTestExtension/GoogleTestAdapterTests/Resources/TestData/RunSettingsServiceTests/Solution.gta.runsettings new file mode 100644 index 000000000..8d27c8f65 --- /dev/null +++ b/GoogleTestExtension/GoogleTestAdapterTests/Resources/TestData/RunSettingsServiceTests/Solution.gta.runsettings @@ -0,0 +1,9 @@ + + + + Solution + 2 + 2 + 2 + + \ No newline at end of file diff --git a/GoogleTestExtension/GoogleTestAdapterTests/Resources/TestData/RunSettingsServiceTests/User.runsettings b/GoogleTestExtension/GoogleTestAdapterTests/Resources/TestData/RunSettingsServiceTests/User.runsettings new file mode 100644 index 000000000..48272c4ce --- /dev/null +++ b/GoogleTestExtension/GoogleTestAdapterTests/Resources/TestData/RunSettingsServiceTests/User.runsettings @@ -0,0 +1,9 @@ + + + + User + 3 + 3 + 3 + + \ No newline at end of file