Skip to content

Commit

Permalink
unit tests for RunSettingsService
Browse files Browse the repository at this point in the history
  • Loading branch information
csoltenborn committed Nov 15, 2015
1 parent 7581381 commit f9760dc
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 2 deletions.
7 changes: 5 additions & 2 deletions GoogleTestExtension/GoogleTestAdapter/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,23 @@
<Reference Include="Microsoft.VisualStudio.TestPlatform.ObjectModel">
<HintPath>..\lib\Microsoft.VisualStudio.TestPlatform.ObjectModel.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestWindow, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\lib\Microsoft.VisualStudio.TestWindow.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestWindow.Core">
<HintPath>..\lib\Microsoft.VisualStudio.TestWindow.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestWindow.Interfaces, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\lib\Microsoft.VisualStudio.TestWindow.Interfaces.dll</HintPath>
</Reference>
<Reference Include="Moq, Version=4.2.1507.118, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.2.1507.0118\lib\net40\Moq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.XML" />
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
Expand Down Expand Up @@ -97,6 +109,12 @@
<ItemGroup>
<None Include="Key.snk" />
<None Include="packages.config" />
<Content Include="Resources\TestData\RunSettingsServiceTests\Solution.gta.runsettings">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Resources\TestData\RunSettingsServiceTests\User.runsettings">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="Resources\TestData\_x64\CrashingGoogleTests\CrashingGoogleTests.exe.gta_testdurations" />
<None Include="Resources\TestData\_x64\ExternallyLinkedGoogleTests\ExternallyLinkedGoogleTests.exe.gta_testdurations" />
<None Include="Resources\TestData\_x64\StaticallyLinkedGoogleTests\StaticallyLinkedGoogleTests.exe.gta_testdurations" />
Expand Down
80 changes: 80 additions & 0 deletions GoogleTestExtension/GoogleTestAdapterTests/OptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<ILogger> mockLogger = new Mock<ILogger>();
Mock<IRunSettingsConfigurationInfo> mockRunSettingsConfigInfo = new Mock<IRunSettingsConfigurationInfo>();

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<MessageLevel>(ml => ml == MessageLevel.Warning), It.Is<string>(s => s.Contains("could not be parsed"))),
Times.Exactly(1));
}

[TestMethod]
public void RunSettingsService_CorrectOverridingHierarchy()
{
Mock<ILogger> mockLogger = new Mock<ILogger>();
Mock<IRunSettingsConfigurationInfo> mockRunSettingsConfigInfo = new Mock<IRunSettingsConfigurationInfo>();

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<ILogger> mockLogger)
{
Mock<IGlobalRunSettings> mockGlobalRunSettings = new Mock<IGlobalRunSettings>();
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);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<GoogleTestAdapter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<BatchForTestSetup>Solution</BatchForTestSetup>
<NrOfTestRepetitions>2</NrOfTestRepetitions>
<ShuffleTestsSeed>2</ShuffleTestsSeed>
<ReportWaitPeriod>2</ReportWaitPeriod>
</GoogleTestAdapter>
</RunSettings>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<GoogleTestAdapter>
<BatchForTestTeardown>User</BatchForTestTeardown>
<MaxNrOfThreads>3</MaxNrOfThreads>
<ShuffleTestsSeed>3</ShuffleTestsSeed>
<ReportWaitPeriod>3</ReportWaitPeriod>
</GoogleTestAdapter>
</RunSettings>

0 comments on commit f9760dc

Please sign in to comment.