From 4d40ace3028f7747f8e485b4db90af566779d297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Terje=20Sandstr=C3=B8m?= Date: Mon, 19 Oct 2020 21:42:09 +0200 Subject: [PATCH 1/4] Removed duplicated code --- .gitignore | 1 + src/NUnitTestAdapter/Execution.cs | 38 +++++------ src/NUnitTestAdapter/TraitsFeature.cs | 68 ++++++++----------- .../NUnitEngineTests/NUnitDiscoveryTests.cs | 2 - src/NUnitTestAdapterTests/TraitsTests.cs | 11 ++- 5 files changed, 52 insertions(+), 68 deletions(-) diff --git a/.gitignore b/.gitignore index 167fcb68..2ba44ec0 100644 --- a/.gitignore +++ b/.gitignore @@ -411,3 +411,4 @@ healthchecksdb MigrationBackup/ # Ionide (cross platform F# VS Code tools) working folder .ionide/ +/dupl.xml diff --git a/src/NUnitTestAdapter/Execution.cs b/src/NUnitTestAdapter/Execution.cs index 78cc325b..881ecad2 100644 --- a/src/NUnitTestAdapter/Execution.cs +++ b/src/NUnitTestAdapter/Execution.cs @@ -69,6 +69,22 @@ public virtual bool Run(TestFilter filter, DiscoveryConverter discovery, NUnit3T protected NUnitTestFilterBuilder CreateTestFilterBuilder() => new NUnitTestFilterBuilder(NUnitEngineAdapter.GetService(), Settings); protected ITestConverterCommon CreateConverter(DiscoveryConverter discovery) => Settings.DiscoveryMethod == DiscoveryMethod.Current ? discovery.TestConverter : discovery.TestConverterForXml; + + protected TestFilter CheckFilter(IDiscoveryConverter discovery) + { + TestFilter filter; + if (discovery.NoOfLoadedTestCasesAboveLimit) + { + TestLog.Debug("Setting filter to empty due to number of testcases"); + filter = TestFilter.Empty; + } + else + { + var filterBuilder = CreateTestFilterBuilder(); + filter = filterBuilder.FilterByList(discovery.LoadedTestCases); + } + return filter; + } } public class IdeExecution : Execution @@ -87,16 +103,7 @@ public override TestFilter CheckFilterInCurrentMode(TestFilter filter, IDiscover return filter; if (filter.IsEmpty()) return filter; - if (discovery.NoOfLoadedTestCasesAboveLimit) - { - TestLog.Debug("Setting filter to empty due to number of testcases"); - filter = TestFilter.Empty; - } - else - { - var filterBuilder = CreateTestFilterBuilder(); - filter = filterBuilder.FilterByList(discovery.LoadedTestCases); - } + filter = CheckFilter(discovery); return filter; } } @@ -151,16 +158,7 @@ public override TestFilter CheckFilterInCurrentMode(TestFilter filter, IDiscover return filter; if ((VsTestFilter == null || VsTestFilter.IsEmpty) && filter != TestFilter.Empty) { - if (discovery.NoOfLoadedTestCasesAboveLimit) - { - TestLog.Debug("Setting filter to empty due to number of testcases"); - filter = TestFilter.Empty; - } - else - { - var filterBuilder = CreateTestFilterBuilder(); - filter = filterBuilder.FilterByList(discovery.LoadedTestCases); - } + filter = CheckFilter(discovery); } else if (VsTestFilter != null && !VsTestFilter.IsEmpty && !Settings.UseNUnitFilter) { diff --git a/src/NUnitTestAdapter/TraitsFeature.cs b/src/NUnitTestAdapter/TraitsFeature.cs index dd3c99eb..730d5087 100644 --- a/src/NUnitTestAdapter/TraitsFeature.cs +++ b/src/NUnitTestAdapter/TraitsFeature.cs @@ -21,6 +21,7 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // *********************************************************************** +using System; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestPlatform.ObjectModel; @@ -68,26 +69,7 @@ public static void AddTraitsFromTestNode(this TestCase testCase, NUnitDiscoveryT // Reading ancestor properties of a test-case node. And adding to the cache. while (ancestor != null && !string.IsNullOrEmpty(key)) { - if (traitsCache.ContainsKey(key)) - { - categoryList.AddRange(traitsCache[key].Traits.Where(o => o.Name == NunitTestCategoryLabel).Select(prop => prop.Value).ToList()); - - if (traitsCache[key].Explicit) - testCase.SetPropertyValue(CategoryList.NUnitExplicitProperty, true); - - var traitsList = traitsCache[key].Traits.Where(o => o.Name != NunitTestCategoryLabel).ToList(); - if (traitsList.Count > 0) - testCase.Traits.AddRange(traitsList); - } - else - { - categoryList.ProcessTestCaseProperties(ancestor, true, key, traitsCache); - // Adding entry to dictionary, so that we will not make SelectNodes call again. - if (categoryList.LastNodeListCount == 0 && !traitsCache.ContainsKey(key)) - { - traitsCache[key] = new CachedTestCaseInfo(); - } - } + AddingToCache(testCase, traitsCache, key, categoryList, ancestor, categoryList.ProcessTestCaseProperties); ancestor = ancestor.Parent; key = ancestor?.Id; } @@ -97,6 +79,31 @@ public static void AddTraitsFromTestNode(this TestCase testCase, NUnitDiscoveryT categoryList.UpdateCategoriesToVs(); } + private static void AddingToCache(TestCase testCase, IDictionary traitsCache, string key, CategoryList categoryList, T ancestor, Func, IEnumerable> processTestCaseProperties) + { + if (traitsCache.ContainsKey(key)) + { + categoryList.AddRange(traitsCache[key].Traits.Where(o => o.Name == NunitTestCategoryLabel) + .Select(prop => prop.Value).ToList()); + + if (traitsCache[key].Explicit) + testCase.SetPropertyValue(CategoryList.NUnitExplicitProperty, true); + + var traitsList = traitsCache[key].Traits.Where(o => o.Name != NunitTestCategoryLabel).ToList(); + if (traitsList.Count > 0) + testCase.Traits.AddRange(traitsList); + } + else + { + processTestCaseProperties(ancestor, true, key, traitsCache); + // Adding entry to dictionary, so that we will not make SelectNodes call again. + if (categoryList.LastNodeListCount == 0 && !traitsCache.ContainsKey(key)) + { + traitsCache[key] = new CachedTestCaseInfo(); + } + } + } + public static void AddTraitsFromXmlTestNode(this TestCase testCase, NUnitEventTestCase testNCase, IDictionary traitsCache, ITestLogger logger, IAdapterSettings adapterSettings) { @@ -106,26 +113,7 @@ public static void AddTraitsFromXmlTestNode(this TestCase testCase, NUnitEventTe // Reading ancestor properties of a test-case node. And adding to the cache. while (ancestor != null && key != null) { - if (traitsCache.ContainsKey(key)) - { - categoryList.AddRange(traitsCache[key].Traits.Where(o => o.Name == NunitTestCategoryLabel).Select(prop => prop.Value).ToList()); - - if (traitsCache[key].Explicit) - testCase.SetPropertyValue(CategoryList.NUnitExplicitProperty, true); - - var traitsList = traitsCache[key].Traits.Where(o => o.Name != NunitTestCategoryLabel).ToList(); - if (traitsList.Count > 0) - testCase.Traits.AddRange(traitsList); - } - else - { - categoryList.ProcessTestCaseProperties(ancestor, true, key, traitsCache); - // Adding entry to dictionary, so that we will not make SelectNodes call again. - if (categoryList.LastNodeListCount == 0 && !traitsCache.ContainsKey(key)) - { - traitsCache[key] = new CachedTestCaseInfo(); - } - } + AddingToCache(testCase, traitsCache, key, categoryList, ancestor, categoryList.ProcessTestCaseProperties); ancestor = ancestor.Parent; key = ancestor?.Id; } diff --git a/src/NUnitTestAdapterTests/NUnitEngineTests/NUnitDiscoveryTests.cs b/src/NUnitTestAdapterTests/NUnitEngineTests/NUnitDiscoveryTests.cs index 499b0b53..792c5256 100644 --- a/src/NUnitTestAdapterTests/NUnitEngineTests/NUnitDiscoveryTests.cs +++ b/src/NUnitTestAdapterTests/NUnitEngineTests/NUnitDiscoveryTests.cs @@ -1081,8 +1081,6 @@ public void ThatSetUpFixtureWorks2() var ndr = sut.ConvertXml( new NUnitResults(XmlHelper.CreateXmlNode(SetupFixtureIssue770))); Assert.That(ndr, Is.Not.Null); - } - } } diff --git a/src/NUnitTestAdapterTests/TraitsTests.cs b/src/NUnitTestAdapterTests/TraitsTests.cs index 075f746f..55da2e1b 100644 --- a/src/NUnitTestAdapterTests/TraitsTests.cs +++ b/src/NUnitTestAdapterTests/TraitsTests.cs @@ -7,6 +7,7 @@ using NUnit.Framework; using NUnit.VisualStudio.TestAdapter.NUnitEngine; using NUnit.VisualStudio.TestAdapter.Tests.Fakes; +// ReSharper disable StringLiteralTypo namespace NUnit.VisualStudio.TestAdapter.Tests { @@ -516,7 +517,7 @@ public void ThatTestCaseHasTraits() Assert.That(testcaselist.Count, Is.EqualTo(3), "Wrong number of testcases found"); var testcasesWithCategories = testcaselist.Where(o => o.GetCategories()?.FirstOrDefault() != null).ToList(); Assert.That(testcasesWithCategories, Is.Not.Null, "Didn't find the testcases"); - Assert.That(testcasesWithCategories.Count(), Is.EqualTo(1), "Wrong number of testcases with categories, should be only 1"); + Assert.That(testcasesWithCategories.Count, Is.EqualTo(1), "Wrong number of testcases with categories, should be only 1"); var tc = testcasesWithCategories.FirstOrDefault(); VerifyCategoriesOnly(tc, 1, "simple"); Assert.That(tc.GetCategories().First(), Is.EqualTo("Single")); @@ -537,13 +538,11 @@ private static IReadOnlyList GetTestCases(string xml) { var settings = Substitute.For(); settings.CollectSourceInformation.Returns(false); - using (var converter = new TestConverterForXml( + using var converter = new TestConverterForXml( new TestLogger(new MessageLoggerStub()), "unused", - settings)) - { - return converter.ConvertTestCases(xml); - } + settings); + return converter.ConvertTestCases(xml); } [Test] From 43c846579ba2b83f447eaecef907d8df485ef628 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Terje=20Sandstr=C3=B8m?= Date: Mon, 19 Oct 2020 22:01:36 +0200 Subject: [PATCH 2/4] Fixes --- .../NUnitEventListenerOutputTests.cs | 85 +++++++++++++++++++ .../NUnitEventListenerTests.cs | 78 ----------------- 2 files changed, 85 insertions(+), 78 deletions(-) create mode 100644 src/NUnitTestAdapterTests/NUnitEventListenerOutputTests.cs diff --git a/src/NUnitTestAdapterTests/NUnitEventListenerOutputTests.cs b/src/NUnitTestAdapterTests/NUnitEventListenerOutputTests.cs new file mode 100644 index 00000000..3bf3d528 --- /dev/null +++ b/src/NUnitTestAdapterTests/NUnitEventListenerOutputTests.cs @@ -0,0 +1,85 @@ +using System.Collections.Generic; +using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; +using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; +using NSubstitute; +using NUnit.Framework; +using NUnit.VisualStudio.TestAdapter.Dump; +using NUnit.VisualStudio.TestAdapter.NUnitEngine; + +namespace NUnit.VisualStudio.TestAdapter.Tests +{ + /// + /// These tests ensure correct console output, which is what we send to the "recorder". + /// + public class NUnitEventListenerOutputTests + { + private ITestExecutionRecorder recorder; + private ITestConverterCommon converter; + private IDumpXml dumpxml; + private IAdapterSettings settings; + private INUnit3TestExecutor executor; + + + private const string TestOutputProgress = + @""; + + private const string TestOutputOut = + @""; + + private const string TestOutputError = + @""; + + private const string BlankTestOutput = + @""; + + private const string TestFinish = + @""; + + [SetUp] + public void Setup() + { + recorder = Substitute.For(); + converter = Substitute.For(); + dumpxml = Substitute.For(); + settings = Substitute.For(); + executor = Substitute.For(); + executor.Settings.Returns(settings); + executor.FrameworkHandle.Returns(recorder); + } + + [Test] + public void ThatNormalTestOutputIsOutput() + { + var sut = new NUnitEventListener(converter, executor); + sut.OnTestEvent(TestOutputProgress); + sut.OnTestEvent(TestFinish); + + recorder.Received().SendMessage(Arg.Any(), Arg.Is(x => x.StartsWith("Whatever"))); + converter.Received().GetVsTestResults(Arg.Any(), Arg.Is>(x => x.Count == 1)); + } + + [Test] + public void ThatNormalTestOutputIsError() + { + var sut = new NUnitEventListener(converter, executor); + sut.OnTestEvent(TestOutputError); + sut.OnTestEvent(TestFinish); + + recorder.Received().SendMessage(Arg.Any(), Arg.Is(x => x.StartsWith("Whatever"))); + converter.Received().GetVsTestResults(Arg.Any(), Arg.Is>(x => x.Count == 1)); + } + + [Test] + public void ThatTestOutputWithOnlyWhiteSpaceIsNotOutput() + { + var sut = new NUnitEventListener(converter, executor); + + sut.OnTestEvent(BlankTestOutput); + + recorder.DidNotReceive().SendMessage(Arg.Any(), Arg.Any()); + } + } +} \ No newline at end of file diff --git a/src/NUnitTestAdapterTests/NUnitEventListenerTests.cs b/src/NUnitTestAdapterTests/NUnitEventListenerTests.cs index 4f58802a..c3435b11 100644 --- a/src/NUnitTestAdapterTests/NUnitEventListenerTests.cs +++ b/src/NUnitTestAdapterTests/NUnitEventListenerTests.cs @@ -22,14 +22,11 @@ // *********************************************************************** using System; -using System.Collections.Generic; #if NET35 using System.Runtime.Remoting; #endif using System.Xml; using Microsoft.VisualStudio.TestPlatform.ObjectModel; -using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; -using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; using NSubstitute; using NUnit.Framework; using NUnit.VisualStudio.TestAdapter.Dump; @@ -194,79 +191,4 @@ private void VerifyTestResult(VSTestResult ourResult) #endregion } - - /// - /// These tests ensure correct console output, which is what we send to the "recorder". - /// - public class NUnitEventListenerOutputTests - { - private ITestExecutionRecorder recorder; - private ITestConverterCommon converter; - private IDumpXml dumpxml; - private IAdapterSettings settings; - private INUnit3TestExecutor executor; - - - private const string TestOutputProgress = - @""; - - private const string TestOutputOut = - @""; - - private const string TestOutputError = - @""; - - private const string BlankTestOutput = - @""; - - private const string TestFinish = - @""; - - [SetUp] - public void Setup() - { - recorder = Substitute.For(); - converter = Substitute.For(); - dumpxml = Substitute.For(); - settings = Substitute.For(); - executor = Substitute.For(); - executor.Settings.Returns(settings); - executor.FrameworkHandle.Returns(recorder); - } - - [Test] - public void ThatNormalTestOutputIsOutput() - { - var sut = new NUnitEventListener(converter, executor); - sut.OnTestEvent(TestOutputProgress); - sut.OnTestEvent(TestFinish); - - recorder.Received().SendMessage(Arg.Any(), Arg.Is(x => x.StartsWith("Whatever"))); - converter.Received().GetVsTestResults(Arg.Any(), Arg.Is>(x => x.Count == 1)); - } - - [Test] - public void ThatNormalTestOutputIsError() - { - var sut = new NUnitEventListener(converter, executor); - sut.OnTestEvent(TestOutputError); - sut.OnTestEvent(TestFinish); - - recorder.Received().SendMessage(Arg.Any(), Arg.Is(x => x.StartsWith("Whatever"))); - converter.Received().GetVsTestResults(Arg.Any(), Arg.Is>(x => x.Count == 1)); - } - - [Test] - public void ThatTestOutputWithOnlyWhiteSpaceIsNotOutput() - { - var sut = new NUnitEventListener(converter, executor); - - sut.OnTestEvent(BlankTestOutput); - - recorder.DidNotReceive().SendMessage(Arg.Any(), Arg.Any()); - } - } } From 30073024aa1865cdf5d3cceafdbc8307a73efbde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Terje=20Sandstr=C3=B8m?= Date: Tue, 20 Oct 2020 21:23:22 +0200 Subject: [PATCH 3/4] Added azure pieplines yml --- NUnit3TestAdapter.sln | 1 + 1 file changed, 1 insertion(+) diff --git a/NUnit3TestAdapter.sln b/NUnit3TestAdapter.sln index d3b14f05..a7131d56 100644 --- a/NUnit3TestAdapter.sln +++ b/NUnit3TestAdapter.sln @@ -8,6 +8,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution .gitignore = .gitignore .runsettings = .runsettings appveyor.yml = appveyor.yml + azure-pipelines.yml = azure-pipelines.yml build.cake = build.cake build.cmd = build.cmd build.ps1 = build.ps1 From 3543ab0be449fd640f101bc78245526af8c8e440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Terje=20Sandstr=C3=B8m?= Date: Fri, 23 Oct 2020 21:04:05 +0200 Subject: [PATCH 4/4] Fix compile error --- .../NUnitEventListenerTests.cs | 76 +------------------ 1 file changed, 3 insertions(+), 73 deletions(-) diff --git a/src/NUnitTestAdapterTests/NUnitEventListenerTests.cs b/src/NUnitTestAdapterTests/NUnitEventListenerTests.cs index ce35a0fb..0ca1887a 100644 --- a/src/NUnitTestAdapterTests/NUnitEventListenerTests.cs +++ b/src/NUnitTestAdapterTests/NUnitEventListenerTests.cs @@ -22,11 +22,14 @@ // *********************************************************************** using System; +using System.Collections.Generic; #if NET35 using System.Runtime.Remoting; #endif using System.Xml; using Microsoft.VisualStudio.TestPlatform.ObjectModel; +using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; +using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; using NSubstitute; using NUnit.Framework; using NUnit.VisualStudio.TestAdapter.Dump; @@ -191,77 +194,4 @@ private void VerifyTestResult(VSTestResult ourResult) #endregion } - - /// - /// These tests ensure correct console output, which is what we send to the "recorder". - /// - public class NUnitEventListenerOutputTests - { - private ITestExecutionRecorder recorder; - private ITestConverterCommon converter; - private IAdapterSettings settings; - private INUnit3TestExecutor executor; - - - private const string TestOutputProgress = - @""; - - private const string TestOutputOut = - @""; - - private const string TestOutputError = - @""; - - private const string BlankTestOutput = - @""; - - private const string TestFinish = - @""; - - [SetUp] - public void Setup() - { - recorder = Substitute.For(); - converter = Substitute.For(); - settings = Substitute.For(); - executor = Substitute.For(); - executor.Settings.Returns(settings); - executor.FrameworkHandle.Returns(recorder); - } - - [Test] - public void ThatNormalTestOutputIsOutput() - { - var sut = new NUnitEventListener(converter, executor); - sut.OnTestEvent(TestOutputProgress); - sut.OnTestEvent(TestFinish); - - recorder.Received().SendMessage(Arg.Any(), Arg.Is(x => x.StartsWith("Whatever"))); - converter.Received().GetVsTestResults(Arg.Any(), Arg.Is>(x => x.Count == 1)); - } - - [Test] - public void ThatNormalTestOutputIsError() - { - var sut = new NUnitEventListener(converter, executor); - sut.OnTestEvent(TestOutputError); - sut.OnTestEvent(TestFinish); - - recorder.Received().SendMessage(Arg.Any(), Arg.Is(x => x.StartsWith("Whatever"))); - converter.Received().GetVsTestResults(Arg.Any(), Arg.Is>(x => x.Count == 1)); - } - - [Test] - public void ThatTestOutputWithOnlyWhiteSpaceIsNotOutput() - { - var sut = new NUnitEventListener(converter, executor); - - sut.OnTestEvent(BlankTestOutput); - - recorder.DidNotReceive().SendMessage(Arg.Any(), Arg.Any()); - } - } }