Skip to content

Commit

Permalink
Fix for issue #559 and #561 Added option to switch between NUnit and …
Browse files Browse the repository at this point in the history
…MSTest category types. Defaulting to MSTest to ensure #506 is still working.
  • Loading branch information
OsirisTerje committed Nov 19, 2018
1 parent 275bb27 commit 6c3c48d
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 32 deletions.
1 change: 1 addition & 0 deletions NUnit3TestAdapter.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
NuGet.Config = NuGet.Config
Osiris.Extended.ruleset = Osiris.Extended.ruleset
README.md = README.md
Simple.runsettings = Simple.runsettings
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "nuget", "nuget", "{DE347D88-F6ED-4031-AFC2-318F63E39BC9}"
Expand Down
52 changes: 52 additions & 0 deletions Simple.runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<!-- 0 = As many processes as possible, limited by number of cores on machine, 1 = Sequential (1 process), 2-> Given number of processes up to limit by number of cores on machine-->
<MaxCpuCount>0</MaxCpuCount>
</RunConfiguration>

<!--
<TestRunParameters>
<Parameter name="webAppUrl" value="http://localhost" />
<Parameter name="webAppUserName" value="Admin" />
</TestRunParameters>
-->

<!-- Adapter Specific sections -->

<!-- MSTest adapter -->
<MSTest>
<MapInconclusiveToFailed>True</MapInconclusiveToFailed>
<CaptureTraceOutput>false</CaptureTraceOutput>
<DeleteDeploymentDirectoryAfterTestRunIsComplete>False</DeleteDeploymentDirectoryAfterTestRunIsComplete>
<DeploymentEnabled>False</DeploymentEnabled>
<!-- Uncomment and update path for assembly resolution -->
<!-- <AssemblyResolution>
<Directory path="D:\myfolder\bin\" includeSubDirectories="false"/>
</AssemblyResolution> -->
</MSTest>

<!-- NUnit3 adapter, uncomment to set as appropriate, numeric, booleans, enums have their default values below, except RandomSeed -->
<!--
<NUnit>
<BasePath>D:\Dev\NUnit\nunit3-vs-adapter\demo\NUnitTestDemo\bin\Release</BasePath>
<PrivateBinPath>extras;more.extras</PrivateBinPath>
<DefaultTimeout>0</DefaultTimeout>
<WorkDirectory>work</WorkDirectory>
<InternalTraceLevel>Off</InternalTraceLevel>
<RandomSeed>1234567</RandomSeed>
<NumberOfTestWorkers>-1</NumberOfTestWorkers>
<Verbosity>0</Verbosity>
<UseVsKeepEngineRunning>false</UseVsKeepEngineRunning>
<ShadowCopyFiles>false</ShadowCopyFiles>
<DefaultTestNamePattern>{m}{a}</DefaultTestNamePattern>
</NUnit>
-->

<NUnit>
<Verbosity>0</Verbosity>
</NUnit>


</RunSettings>

2 changes: 1 addition & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var configuration = Argument("configuration", "Release");
// SET PACKAGE VERSION
//////////////////////////////////////////////////////////////////////

var version = "3.11.0";
var version = "3.11.1";
var modifier = "";

var dbgSuffix = configuration == "Debug" ? "-dbg" : "";
Expand Down
4 changes: 3 additions & 1 deletion build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ Param(
)

$CakeVersion = "0.30.0"
$DotNetVersion = "2.1.201";
# $DotNetVersion = "2.1.201"; # This will not work with the tests
$DotNetVersion = "1.0.4";
$DotNetInstallerUri = "https://dot.net/v1/dotnet-install.ps1";
$NugetUrl = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"

Expand Down Expand Up @@ -80,6 +81,7 @@ if($FoundDotNetCliVersion -ne $DotNetVersion) {
if (!(Test-Path $InstallPath)) {
mkdir -Force $InstallPath | Out-Null;
}
[Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
(New-Object System.Net.WebClient).DownloadFile($DotNetInstallerUri, "$InstallPath\dotnet-install.ps1");
& $InstallPath\dotnet-install.ps1 -Version $DotNetVersion -InstallDir $InstallPath;

Expand Down
28 changes: 26 additions & 2 deletions src/NUnitTestAdapter/AdapterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,21 @@ public interface IAdapterSettings
/// </summary>
string DefaultTestNamePattern { get; }

VsTestCategoryType VsTestCategoryType { get; }

void Load(IDiscoveryContext context);
void Load(string settingsXml);
void SaveRandomSeed(string dirname);
void RestoreRandomSeed(string dirname);
}

public enum VsTestCategoryType
{
NUnit,
MsTest
}


public class AdapterSettings : IAdapterSettings
{
private const string RANDOM_SEED_FILE = "nunit_random_seed.tmp";
Expand Down Expand Up @@ -153,6 +162,8 @@ public AdapterSettings(TestLogger logger)
public string DomainUsage { get; private set; }


public VsTestCategoryType VsTestCategoryType { get; private set; } = VsTestCategoryType.MsTest;

public bool DumpXmlTestDiscovery { get; private set; }

public bool DumpXmlTestResults { get; private set; }
Expand Down Expand Up @@ -226,6 +237,20 @@ public void Load(string settingsXml)

DumpXmlTestDiscovery = GetInnerTextAsBool(nunitNode, nameof(DumpXmlTestDiscovery), false);
DumpXmlTestResults = GetInnerTextAsBool(nunitNode, nameof(DumpXmlTestResults), false);
var vsTestCategoryType = GetInnerText(nunitNode, nameof(VsTestCategoryType), Verbosity > 0);
if (vsTestCategoryType != null)
switch (vsTestCategoryType.ToLower())
{
case "nunit":
VsTestCategoryType = VsTestCategoryType.NUnit;
break;
case "mstest":
VsTestCategoryType = VsTestCategoryType.MsTest;
break;
default:
_logger.Warning($"Invalid value ({vsTestCategoryType}) for VsTestCategoryType, should be either NUnit or MsTest");
break;
}



Expand Down Expand Up @@ -348,8 +373,7 @@ private string GetInnerText(XmlNode startNode, string xpath, bool log, params st
if (string.Compare(valid, val, StringComparison.OrdinalIgnoreCase) == 0)
return valid;

throw new ArgumentException(string.Format(
"Invalid value {0} passed for element {1}.", val, xpath));
throw new ArgumentException($"Invalid value {val} passed for element {xpath}.");
}


Expand Down
17 changes: 10 additions & 7 deletions src/NUnitTestAdapter/CategoryList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
Expand All @@ -24,9 +23,11 @@ public class CategoryList

private readonly List<string> categorylist = new List<string>();
private readonly TestCase testCase;
private IAdapterSettings settings;

public CategoryList(TestCase testCase)
public CategoryList(TestCase testCase, IAdapterSettings adapterSettings)
{
settings = adapterSettings;
this.testCase = testCase;
}

Expand Down Expand Up @@ -60,7 +61,7 @@ public IEnumerable<string> ProcessTestCaseProperties(XmlNode testNode, bool addT
if (testNode.Attributes?["runstate"]?.Value == "Explicit")
{
// Add UI grouping “Explicit”
if (!testCase.Traits.Any(trait => trait.Name == ExplicitTraitName))
if (testCase.Traits.All(trait => trait.Name != ExplicitTraitName))
testCase.Traits.Add(new Trait(ExplicitTraitName, ExplicitTraitValue));

// Track whether the test is actually explicit since multiple things result in the same UI grouping
Expand Down Expand Up @@ -89,7 +90,7 @@ private static bool IsInternalProperty(string propertyName, string propertyValue
}

// Property names starting with '_' are for internal use only
return String.IsNullOrEmpty(propertyName) || propertyName[0] == '_' || String.IsNullOrEmpty(propertyValue);
return string.IsNullOrEmpty(propertyName) || propertyName[0] == '_' || string.IsNullOrEmpty(propertyValue);
}

private static void AddTraitsToCache(IDictionary<string, TraitsFeature.CachedTestCaseInfo> traitsCache, string key, string propertyName, string propertyValue)
Expand All @@ -111,8 +112,10 @@ public void UpdateCategoriesToVs()
{
if (categorylist.Any())
{
testCase.SetPropertyValue(NUnitTestCategoryProperty, categorylist.Distinct().ToArray());
testCase.SetPropertyValue(MsTestCategoryProperty, categorylist.Distinct().ToArray());
testCase.SetPropertyValue(
settings.VsTestCategoryType == VsTestCategoryType.NUnit
? NUnitTestCategoryProperty
: MsTestCategoryProperty, categorylist.Distinct().ToArray());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/NUnitTestAdapter/NUnit3TestDiscoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discove
if (topNode.GetAttribute("runstate") == "Runnable")
{
int cases;
using (var testConverter = new TestConverter(TestLog, sourceAssemblyPath, Settings.CollectSourceInformation))
using (var testConverter = new TestConverter(TestLog, sourceAssemblyPath, Settings))
{
cases = ProcessTestCases(topNode, discoverySink, testConverter);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NUnitTestAdapter/NUnit3TestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private void RunAssembly(string assemblyPath, TestFilter filter)
{
var nunitTestCases = loadResult.SelectNodes("//test-case");

using (var testConverter = new TestConverter(TestLog, assemblyPath, Settings.CollectSourceInformation))
using (var testConverter = new TestConverter(TestLog, assemblyPath, Settings))
{
var loadedTestCases = new List<TestCase>();

Expand Down
18 changes: 10 additions & 8 deletions src/NUnitTestAdapter/TestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ public sealed class TestConverter : IDisposable, ITestConverter
private readonly Dictionary<string, TestCase> _vsTestCaseMap;
private readonly string _sourceAssembly;
private readonly NavigationDataProvider _navigationDataProvider;
private readonly bool _collectSourceInformation;
private bool CollectSourceInformation => adapterSettings.CollectSourceInformation;
private readonly IAdapterSettings adapterSettings;

public TestConverter(ITestLogger logger, string sourceAssembly, bool collectSourceInformation)

public TestConverter(ITestLogger logger, string sourceAssembly, IAdapterSettings settings)
{
adapterSettings = settings;
_logger = logger;
_sourceAssembly = sourceAssembly;
_vsTestCaseMap = new Dictionary<string, TestCase>();
_collectSourceInformation = collectSourceInformation;
TraitsCache = new Dictionary<string, TraitsFeature.CachedTestCaseInfo>();

if (_collectSourceInformation)
if (CollectSourceInformation)
{
_navigationDataProvider = new NavigationDataProvider(sourceAssembly, logger);
}
Expand Down Expand Up @@ -99,7 +101,7 @@ public TestCase GetCachedTestCase(string id)
}

private static readonly string NL = Environment.NewLine;

public TestResultSet GetVSTestResults(XmlNode resultNode, ICollection<XmlNode> outputNodes)
{
var results = new List<VSTestResult>();
Expand Down Expand Up @@ -155,7 +157,7 @@ private TestCase MakeTestCaseFromXmlNode(XmlNode testNode)
LineNumber = 0
};

if (_collectSourceInformation && _navigationDataProvider != null)
if (CollectSourceInformation && _navigationDataProvider != null)
{
var className = testNode.GetAttribute("classname");
var methodName = testNode.GetAttribute("methodname");
Expand All @@ -167,7 +169,7 @@ private TestCase MakeTestCaseFromXmlNode(XmlNode testNode)
}
}

testCase.AddTraitsFromTestNode(testNode, TraitsCache,_logger);
testCase.AddTraitsFromTestNode(testNode, TraitsCache,_logger,adapterSettings);

return testCase;
}
Expand Down Expand Up @@ -224,7 +226,7 @@ private VSTestResult GetBasicResult(XmlNode resultNode, IEnumerable<XmlNode> out
FillResultFromOutputNodes(outputNodes, vsResult);

// Add stdOut messages from TestFinished element to vstest result
XmlNode outputNode = resultNode.SelectSingleNode("output");
var outputNode = resultNode.SelectSingleNode("output");
if (outputNode != null)
vsResult.Messages.Add(new TestResultMessage(TestResultMessage.StandardOutCategory, outputNode.InnerText));

Expand Down
4 changes: 2 additions & 2 deletions src/NUnitTestAdapter/TraitsFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ public sealed class CachedTestCaseInfo
}

public static void AddTraitsFromTestNode(this TestCase testCase, XmlNode testNode,
IDictionary<string, CachedTestCaseInfo> traitsCache, ITestLogger logger)
IDictionary<string, CachedTestCaseInfo> traitsCache, ITestLogger logger, IAdapterSettings adapterSettings)
{
var ancestor = testNode.ParentNode;
var key = ancestor.Attributes?["id"]?.Value;
var categorylist = new CategoryList(testCase);
var categorylist = new CategoryList(testCase,adapterSettings);
// Reading ancestor properties of a test-case node. And adding to the cache.
while (ancestor != null && key != null)
{
Expand Down
16 changes: 16 additions & 0 deletions src/NUnitTestAdapterTests/AdapterSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,22 @@ public void BasePathSetting()
Assert.That(_settings.BasePath, Is.EqualTo(".."));
}


[Test]
public void VsTestCategoryTypeSetting()
{
_settings.Load("<RunSettings><NUnit><VsTestCategoryType>nunit</VsTestCategoryType></NUnit></RunSettings>");
Assert.That(_settings.VsTestCategoryType, Is.EqualTo(VsTestCategoryType.NUnit));
}

[Test]
public void VsTestCategoryTypeSettingWithGarbage()
{
_settings.Load("<RunSettings><NUnit><VsTestCategoryType>garbage</VsTestCategoryType></NUnit></RunSettings>");
Assert.That(_settings.VsTestCategoryType, Is.EqualTo(VsTestCategoryType.MsTest));
}


[Test]
public void PrivateBinPathSetting()
{
Expand Down
9 changes: 6 additions & 3 deletions src/NUnitTestAdapterTests/NUnitEventListenerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public class NUnitEventListenerTests
public void SetUp()
{
testLog = new FakeFrameworkHandle();

using (var testConverter = new TestConverter(new TestLogger(new MessageLoggerStub()), FakeTestData.AssemblyPath, collectSourceInformation: true))
var settings = Substitute.For<IAdapterSettings>();
settings.CollectSourceInformation.Returns(true);
using (var testConverter = new TestConverter(new TestLogger(new MessageLoggerStub()), FakeTestData.AssemblyPath,settings))
{
fakeTestNode = FakeTestData.GetTestNode();

Expand Down Expand Up @@ -154,7 +155,9 @@ public void TestFinished_CallsRecordResultCorrectly()
public void Listener_LeaseLifetimeWillNotExpire()
{
testLog = new FakeFrameworkHandle();
using (var testConverter = new TestConverter(new TestLogger(new MessageLoggerStub()), FakeTestData.AssemblyPath, collectSourceInformation: true))
var settings = Substitute.For<IAdapterSettings>();
settings.CollectSourceInformation.Returns(true);
using (var testConverter = new TestConverter(new TestLogger(new MessageLoggerStub()), FakeTestData.AssemblyPath, settings))
{
var localInstance = (MarshalByRefObject)Activator.CreateInstance(typeof(NUnitEventListener), testLog, testConverter, null);

Expand Down
5 changes: 4 additions & 1 deletion src/NUnitTestAdapterTests/TestCaseUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using System;
using System.Collections.Generic;
using System.Xml;
using NSubstitute;
using NUnit.VisualStudio.TestAdapter.Tests.Fakes;

namespace NUnit.VisualStudio.TestAdapter.Tests
Expand Down Expand Up @@ -55,10 +56,12 @@ public static IReadOnlyList<TestCase> ConvertTestCases(this TestConverter testCo

public static IReadOnlyCollection<TestCase> ConvertTestCases(string xml)
{
var settings = Substitute.For<IAdapterSettings>();
settings.CollectSourceInformation.Returns(false);
using (var testConverter = new TestConverter(
new TestLogger(new MessageLoggerStub()),
FakeTestData.AssemblyPath,
collectSourceInformation: false))
settings))
{
return testConverter.ConvertTestCases(xml);
}
Expand Down
5 changes: 4 additions & 1 deletion src/NUnitTestAdapterTests/TestConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using System.Xml;

using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using NSubstitute;
using NUnit.Framework;

namespace NUnit.VisualStudio.TestAdapter.Tests
Expand All @@ -43,7 +44,9 @@ public class TestConverterTests
public void SetUp()
{
fakeTestNode = FakeTestData.GetTestNode();
testConverter = new TestConverter(new TestLogger(new MessageLoggerStub()), FakeTestData.AssemblyPath, collectSourceInformation: true);
var settings = Substitute.For<IAdapterSettings>();
settings.CollectSourceInformation.Returns(true);
testConverter = new TestConverter(new TestLogger(new MessageLoggerStub()), FakeTestData.AssemblyPath, settings);
}

[TearDown]
Expand Down
Loading

0 comments on commit 6c3c48d

Please sign in to comment.