Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OsirisTerje committed Jul 9, 2020
1 parent f4c973d commit f973ae3
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .runsettings
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<Verbosity>5</Verbosity>
<DumpXmlTestResults>true</DumpXmlTestResults>
<DumpXmlTestDiscovery>true</DumpXmlTestDiscovery>
<DiscoveryMethod>Modern</DiscoveryMethod>
<DiscoveryMethod>Current</DiscoveryMethod>
</NUnit>


Expand Down
2 changes: 1 addition & 1 deletion DisableAppDomain.runsettings
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<Verbosity>5</Verbosity>
<DumpXmlTestResults>true</DumpXmlTestResults>
<DumpXmlTestDiscovery>true</DumpXmlTestDiscovery>
<DiscoveryMethod>Modern</DiscoveryMethod>
<DiscoveryMethod>Current</DiscoveryMethod>
</NUnit>
</RunSettings>
2 changes: 1 addition & 1 deletion src/NUnitTestAdapter/NUnit3TestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public void InitializeForExecution(IRunContext runContext, IFrameworkHandle fram

RunContext = runContext;
FrameworkHandle = frameworkHandle;
VsTestFilter = new VsTestFilter(runContext);
VsTestFilter = VsTestFilterFactory.CreateVsTestFilter(Settings, runContext);

CleanUpRegisteredChannels();

Expand Down
4 changes: 2 additions & 2 deletions src/NUnitTestAdapter/NUnitEngine/DiscoveryConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public IList<TestCase> Convert(NUnitResults discoveryResults, string assemblyPat
foreach (XmlNode testNode in nunitTestCases)
loadedTestCases.Add(converterForXml.ConvertTestCase(new NUnitEventTestCase(testNode)));
TestLog.Info(
$" NUnit3TestExecutor discovered {loadedTestCases.Count} of {nunitTestCases.Count} NUnit test cases using Classic mode");
$" NUnit3TestExecutor discovered {loadedTestCases.Count} of {nunitTestCases.Count} NUnit test cases using Legacy discovery mode");
}
else
{
Expand All @@ -155,7 +155,7 @@ public IList<TestCase> Convert(NUnitResults discoveryResults, string assemblyPat
loadedTestCases.Add(converter.ConvertTestCase(testNode));
var msg = isExplicit ? "Explicit run" : "Non-Explicit run";
TestLog.Info(
$" NUnit3TestExecutor discovered {loadedTestCases.Count} of {nunitTestCases.Count} NUnit test cases using Modern mode, {msg}");
$" NUnit3TestExecutor discovered {loadedTestCases.Count} of {nunitTestCases.Count} NUnit test cases using Current Discovery mode, {msg}");
}

timing.LogTime("Converting test cases ");
Expand Down
75 changes: 65 additions & 10 deletions src/NUnitTestAdapter/VsTestFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace NUnit.VisualStudio.TestAdapter
using System.Collections;
// ReSharper disable once RedundantUsingDirective
using System.Reflection; // Needed for .net core 2.1
using NUnit.VisualStudio.TestAdapter.Internal;
using NUnit.VisualStudio.TestAdapter.Internal; // Needed for reflection

public interface IVsTestFilter
{
Expand All @@ -43,7 +43,7 @@ public interface IVsTestFilter
IEnumerable<TestCase> CheckFilter(IEnumerable<TestCase> tests);
}

public class VsTestFilter : IVsTestFilter
public abstract class VsTestFilter : IVsTestFilter
{
/// <summary>
/// Supported properties for filtering.
Expand All @@ -68,7 +68,9 @@ static VsTestFilter()
var categoryTrait = new NTrait("Category", "");
SupportedTraitCache = new Dictionary<string, NTrait>(StringComparer.OrdinalIgnoreCase)
{
["Priority"] = priorityTrait, ["TestCategory"] = categoryTrait, ["Category"] = categoryTrait
["Priority"] = priorityTrait,
["TestCategory"] = categoryTrait,
["Category"] = categoryTrait
};
// Initialize the trait property map, since TFS doesnt know about traits
TraitPropertyMap = new Dictionary<NTrait, TestProperty>(new NTraitNameComparer());
Expand All @@ -85,7 +87,8 @@ static VsTestFilter()
}

private readonly IRunContext runContext;
public VsTestFilter(IRunContext runContext)

protected VsTestFilter(IRunContext runContext)
{
this.runContext = runContext;
}
Expand All @@ -102,12 +105,7 @@ public IEnumerable<TestCase> CheckFilter(IEnumerable<TestCase> tests)
return tests.Where(CheckFilter).ToList();
}

private bool CheckFilter(TestCase testCase)
{
// var isExplicit = testCase.GetPropertyValue(CategoryList.NUnitExplicitProperty, false);

return /*!isExplicit &&*/ TfsTestCaseFilterExpression?.MatchTestCase(testCase, p => PropertyValueProvider(testCase, p)) != false;
}
protected abstract bool CheckFilter(TestCase testCase);

/// <summary>
/// Provides value of TestProperty corresponding to property name 'propertyName' as used in filter.
Expand Down Expand Up @@ -187,6 +185,63 @@ public static NTrait TraitProvider(string traitName)
}
}

public static class VsTestFilterFactory
{
public static VsTestFilter CreateVsTestFilter(IAdapterSettings settings, IRunContext context)
{
if (settings.DiscoveryMethod == DiscoveryMethod.Legacy)
return new VsTestFilterLegacy(context);
if (settings.DesignMode)
return new VsTestFilterIde(context);
return new VsTestFilterNonIde(context);
}
}

public class VsTestFilterLegacy : VsTestFilter
{
public VsTestFilterLegacy(IRunContext runContext) : base(runContext)
{
}

protected override bool CheckFilter(TestCase testCase)
{
var isExplicit = testCase.GetPropertyValue(CategoryList.NUnitExplicitProperty, false);

return !isExplicit && TfsTestCaseFilterExpression?.MatchTestCase(testCase, p => PropertyValueProvider(testCase, p)) != false;
}
}

public class VsTestFilterIde : VsTestFilter
{
public VsTestFilterIde(IRunContext runContext) : base(runContext)
{
}

protected override bool CheckFilter(TestCase testCase)
{
var isExplicit = testCase.GetPropertyValue(CategoryList.NUnitExplicitProperty, false);

return !isExplicit && TfsTestCaseFilterExpression?.MatchTestCase(testCase, p => PropertyValueProvider(testCase, p)) != false;
}
}

public class VsTestFilterNonIde : VsTestFilter
{
public VsTestFilterNonIde(IRunContext runContext) : base(runContext)
{
}

protected override bool CheckFilter(TestCase testCase)
{
// var isExplicit = testCase.GetPropertyValue(CategoryList.NUnitExplicitProperty, false);

return /*!isExplicit &&*/ TfsTestCaseFilterExpression?.MatchTestCase(testCase, p => PropertyValueProvider(testCase, p)) != false;
}
}




public class NTraitNameComparer : IEqualityComparer<NTrait>
{
public bool Equals(NTrait n, NTrait y)
Expand Down
6 changes: 3 additions & 3 deletions src/NUnitTestAdapterTests/AdapterSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,9 @@ public void TestDefaults()
});
}

[TestCase("garbage", DiscoveryMethod.Legacy, DiscoveryMethod.Legacy)]
[TestCase("Old", DiscoveryMethod.Legacy, DiscoveryMethod.Legacy)]
[TestCase("Modern", DiscoveryMethod.Legacy, DiscoveryMethod.Current)]
[TestCase("garbage", DiscoveryMethod.Current, DiscoveryMethod.Current)]
[TestCase("Legacy", DiscoveryMethod.Legacy, DiscoveryMethod.Legacy)]
[TestCase("Current", DiscoveryMethod.Legacy, DiscoveryMethod.Current)]
public void TestMapEnum<T>(string setting, T defaultValue, T expected)
where T : struct, Enum
{
Expand Down
7 changes: 7 additions & 0 deletions src/NUnitTestAdapterTests/ExecutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void Setup()
filter = new TestFilter("<filter><or>A<or>B</or></or></filter>");
}

[Explicit("Need to mock out the engine, it crashes on [command line] build due to multiple instances that can't be handled")]
[Test]
public void ThatCheckFilterInCurrentModeWorks()
{
Expand All @@ -48,5 +49,11 @@ public void ThatCheckFilterInCurrentModeWorks()
Assert.That(result.Text, Is.Not.EqualTo(filter.Text));
Assert.That(result.Text, Is.EqualTo("<filter><test>A</test></filter>"));
}

[TearDown]
public void TearDown()
{
ctx.EngineAdapter.CloseRunner();
}
}
}
4 changes: 2 additions & 2 deletions src/NUnitTestAdapterTests/Fakes/FakeDiscoveryContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ***********************************************************************
// Copyright (c) 2012 Charlie Poole, Terje Sandstrom
// Copyright (c) 2012-2020 Charlie Poole, Terje Sandstrom
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -34,7 +34,7 @@ public FakeDiscoveryContext(IRunSettings runSettings)

#region IDiscoveryContextMembers

public IRunSettings RunSettings { get; private set; }
public IRunSettings RunSettings { get; }

#endregion
}
Expand Down
4 changes: 3 additions & 1 deletion src/NUnitTestAdapterTests/Filtering/FilteringTestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public static VsTestFilter CreateTestFilter(ITestCaseFilterExpression filterExpr
{
var context = Substitute.For<IRunContext>();
context.GetTestCaseFilter(null, null).ReturnsForAnyArgs(filterExpression);
return new VsTestFilter(context);
var settings = Substitute.For<IAdapterSettings>();
settings.DiscoveryMethod.Returns(DiscoveryMethod.Legacy);
return VsTestFilterFactory.CreateVsTestFilter(settings, context);
}

public static void AssertExpectedResult(ITestCaseFilterExpression filterExpression, IReadOnlyCollection<TestCase> testCases, IReadOnlyCollection<string> expectedMatchingTestNames)
Expand Down
33 changes: 22 additions & 11 deletions src/NUnitTestAdapterTests/Filtering/TestFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using NSubstitute;
using NUnit.Framework;
using NUnit.VisualStudio.TestAdapter.Tests.Fakes;

Expand All @@ -32,10 +33,20 @@ namespace NUnit.VisualStudio.TestAdapter.Tests.Filtering
[TestFixture, Category("TFS")]
public class TestFilterTests
{
IAdapterSettings settings;

[OneTimeSetUp]
public void Setup()
{
settings = Substitute.For<IAdapterSettings>();
settings.DiscoveryMethod.Returns(DiscoveryMethod.Legacy);
}


[Test]
public void PropertyProvider()
{
var testfilter = new VsTestFilter(null);
var testfilter = VsTestFilterFactory.CreateVsTestFilter(settings, null);
var prop = VsTestFilter.PropertyProvider("Priority");
Assert.NotNull(prop);
prop = VsTestFilter.PropertyProvider("TestCategory");
Expand All @@ -47,7 +58,7 @@ public void PropertyProvider()
[Test]
public void TraitProvider()
{
var testFilter = new VsTestFilter(null);
var testFilter = VsTestFilterFactory.CreateVsTestFilter(settings, null);
var trait = VsTestFilter.TraitProvider("TestCategory");
Assert.NotNull(trait);
trait = VsTestFilter.TraitProvider("Category");
Expand All @@ -57,7 +68,7 @@ public void TraitProvider()
[Test]
public void TraitProviderWithNoCategory()
{
var testFilter = new VsTestFilter(null);
var testFilter = VsTestFilterFactory.CreateVsTestFilter(settings, null);
var trait = VsTestFilter.TraitProvider("JustKidding");
Assert.Null(trait);
}
Expand All @@ -66,7 +77,7 @@ public void TraitProviderWithNoCategory()
public void PropertyValueProviderFqn()
{
var tc = new TestCase("Test1", new Uri("executor://NUnitTestExecutor"), "NUnit.VSIX");
var testFilter = new VsTestFilter(null);
var testFilter = VsTestFilterFactory.CreateVsTestFilter(settings, null);
var obj = VsTestFilter.PropertyValueProvider(tc, "FullyQualifiedName");
Assert.AreSame("Test1", obj);
}
Expand All @@ -76,7 +87,7 @@ public void PropertyValueProviderWithOneCategoryAndTestCategoryAsFilter()
{
var tc = new TestCase("Test1", new Uri("executor://NUnitTestExecutor"), "NUnit.VSIX");
tc.AddTrait("Category", "CI");
var testFilter = new VsTestFilter(null);
var testFilter = VsTestFilterFactory.CreateVsTestFilter(settings, null);
var obj = VsTestFilter.PropertyValueProvider(tc, "TestCategory");
Assert.AreSame("CI", obj);
}
Expand All @@ -86,7 +97,7 @@ public void PropertyValueProviderWithOneCategoryAndCategoryAsFilter()
{
var tc = new TestCase("Test1", new Uri("executor://NUnitTestExecutor"), "NUnit.VSIX");
tc.AddTrait("Category", "CI");
var testFilter = new VsTestFilter(null);
var testFilter = VsTestFilterFactory.CreateVsTestFilter(settings, null);
var obj = VsTestFilter.PropertyValueProvider(tc, "Category");
Assert.AreSame("CI", obj);
}
Expand All @@ -95,7 +106,7 @@ public void PropertyValueProviderWithOneCategoryAndCategoryAsFilter()
public void PropertyValueProviderWithNoTraitsAndTestCategoryAsFilter()
{
var tc = new TestCase("Test1", new Uri("executor://NUnitTestExecutor"), "NUnit.VSIX");
var testFilter = new VsTestFilter(null);
var testFilter = VsTestFilterFactory.CreateVsTestFilter(settings, null);
var obj = VsTestFilter.PropertyValueProvider(tc, "TestCategory");
Assert.IsNull(obj);
}
Expand All @@ -104,7 +115,7 @@ public void PropertyValueProviderWithNoTraitsAndTestCategoryAsFilter()
public void PropertyValueProviderWithNoTraitsAndCategoryAsFilter()
{
var tc = new TestCase("Test1", new Uri("executor://NUnitTestExecutor"), "NUnit.VSIX");
var testFilter = new VsTestFilter(null);
var testFilter = VsTestFilterFactory.CreateVsTestFilter(settings, null);
var obj = VsTestFilter.PropertyValueProvider(tc, "Category");
Assert.IsNull(obj);
}
Expand All @@ -115,7 +126,7 @@ public void PropertyValueProviderWithMultipleCategoriesAndTestCategoryAsFilter()
var tc = new TestCase("Test1", new Uri("executor://NUnitTestExecutor"), "NUnit.VSIX");
tc.AddTrait("Category", "CI");
tc.AddTrait("Category", "MyOwn");
var testFilter = new VsTestFilter(null);
var testFilter = VsTestFilterFactory.CreateVsTestFilter(settings, null);
var obj = VsTestFilter.PropertyValueProvider(tc, "TestCategory") as string[];
Assert.IsNotNull(obj);
Assert.AreEqual(obj.Length, 2);
Expand All @@ -129,7 +140,7 @@ public void PropertyValueProviderWithMultipleCategoriesAndCategoryAsFilter()
var tc = new TestCase("Test1", new Uri("executor://NUnitTestExecutor"), "NUnit.VSIX");
tc.AddTrait("Category", "CI");
tc.AddTrait("Category", "MyOwn");
var testFilter = new VsTestFilter(null);
var testFilter = VsTestFilterFactory.CreateVsTestFilter(settings, null);
var obj = VsTestFilter.PropertyValueProvider(tc, "Category") as string[];
Assert.IsNotNull(obj);
Assert.AreEqual(obj.Length, 2);
Expand All @@ -142,7 +153,7 @@ public void PropertyValueProviderCategoryFail()
{
var tc = new TestCase("Test1", new Uri("executor://NUnitTestExecutor"), "NUnit.VSIX");
tc.AddTrait("Category", "CI");
var testFilter = new VsTestFilter(null);
var testFilter = VsTestFilterFactory.CreateVsTestFilter(settings, null);
var obj = VsTestFilter.PropertyValueProvider(tc, "Garbage");
Assert.Null(obj);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

namespace NUnit.VisualStudio.TestAdapter.Tests.Filtering
{
public static class VSTestFilterStringTests
public static class VSTestFilterStringTestsLegacy
{
[TestCase(null, new[] { "NonExplicitParent.NonExplicitTest" })]
[TestCase("", new[] { "NonExplicitParent.NonExplicitTest" })]
Expand Down
8 changes: 3 additions & 5 deletions src/NUnitTestAdapterTests/TestDiscoveryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
Expand All @@ -45,7 +44,6 @@ public static IEnumerable<IDiscoveryContext> TestDiscoveryData()
}

[Category("TestDiscovery")]
[TestFixtureSource(typeof(TestDiscoveryDataProvider), nameof(TestDiscoveryDataProvider.TestDiscoveryData))]
public class TestDiscoveryTests : ITestCaseDiscoverySink
{
static readonly string MockAssemblyPath =
Expand All @@ -63,16 +61,16 @@ public TestDiscoveryTests()
[OneTimeSetUp]
public void LoadMockassembly()
{
// Sanity check to be sure we have the correct version of mock-assembly.dll
// Sanity check to be sure we have the correct version of mock-assembly.dll.
Assert.That(NUnit.Tests.Assemblies.MockAssembly.TestsAtRuntime, Is.EqualTo(NUnit.Tests.Assemblies.MockAssembly.Tests),
"The reference to mock-assembly.dll appears to be the wrong version");
Assert.That(File.Exists(MockAssemblyPath), $"Can't locate mock-assembly.dll at {MockAssemblyPath}");
var runsettings = "<RunSettings><NUnit><UseParentFQNForParametrizedTests>True</UseParentFQNForParametrizedTests><SkipNonTestAssemblies>false</SkipNonTestAssemblies></NUnit></RunSettings>";
var runsettings = "<RunSettings><NUnit><UseParentFQNForParametrizedTests>True</UseParentFQNForParametrizedTests><SkipNonTestAssemblies>false</SkipNonTestAssemblies><DiscoveryMethod>Legacy</DiscoveryMethod></NUnit></RunSettings>";
var rs = Substitute.For<IRunSettings>();
rs.SettingsXml.Returns(runsettings);
_context.RunSettings.Returns(rs);
// Load the NUnit mock-assembly.dll once for this test, saving
// the list of test cases sent to the discovery sink
// the list of test cases sent to the discovery sink.
var discoverer = TestAdapterUtils.CreateDiscoverer();
discoverer.DiscoverTests(
new[] { MockAssemblyPath },
Expand Down

0 comments on commit f973ae3

Please sign in to comment.