From 7482d965cb89459dc98ea4b9573a217c3156c7b8 Mon Sep 17 00:00:00 2001 From: Terje Sandstrom Date: Sun, 15 Oct 2017 17:24:05 +0200 Subject: [PATCH] Added more tests, code fixups. --- Osiris.Extended.ruleset | 7 +- src/NUnitTestAdapter/DumpXml.cs | 6 +- src/NUnitTestAdapter/NUnitEventListener.cs | 3 +- src/NUnitTestAdapter/TestConverter.cs | 2 +- .../AdapterSettingsTests.cs | 32 +++- src/NUnitTestAdapterTests/AsyncTests.cs | 24 ++- src/NUnitTestAdapterTests/DumpXmlTests.cs | 24 ++- .../NUnitEventListenerTests.cs | 4 +- src/NUnitTestAdapterTests/ProjectTests.cs | 24 ++- .../TestExecutionTests.cs | 6 +- src/NUnitTestAdapterTests/TestTFSBuild.cs | 24 ++- src/NUnitTestAdapterTests/TraitsTests.cs | 165 +++++++++++++++++- 12 files changed, 294 insertions(+), 27 deletions(-) diff --git a/Osiris.Extended.ruleset b/Osiris.Extended.ruleset index 1f5b160..68367be 100644 --- a/Osiris.Extended.ruleset +++ b/Osiris.Extended.ruleset @@ -1,10 +1,15 @@  - + + + + + + diff --git a/src/NUnitTestAdapter/DumpXml.cs b/src/NUnitTestAdapter/DumpXml.cs index c5f38bd..2f3a35b 100644 --- a/src/NUnitTestAdapter/DumpXml.cs +++ b/src/NUnitTestAdapter/DumpXml.cs @@ -98,9 +98,9 @@ namespace NUnit.VisualStudio.TestAdapter.Dump public void AddTestEvent(string text) { - txt.Append(""); + txt.Append("\n"); txt.Append(text); - txt.Append(""); + txt.Append("\n\n"); } @@ -126,7 +126,7 @@ namespace NUnit.VisualStudio.TestAdapter.Dump twriter.Formatting = System.Xml.Formatting.Indented; twriter.Indentation = 3; twriter.QuoteChar = '\''; - node.WriteContentTo(twriter); + node.WriteTo(twriter); } return swriter.ToString(); } diff --git a/src/NUnitTestAdapter/NUnitEventListener.cs b/src/NUnitTestAdapter/NUnitEventListener.cs index 4cec2e7..8ab2a04 100644 --- a/src/NUnitTestAdapter/NUnitEventListener.cs +++ b/src/NUnitTestAdapter/NUnitEventListener.cs @@ -98,8 +98,7 @@ namespace NUnit.VisualStudio.TestAdapter } catch(Exception ex) { - _recorder.SendMessage(TestMessageLevel.Warning, - string.Format("Error processing {0} event for {1}", node.Name, node.GetAttribute("fullname"))); + _recorder.SendMessage(TestMessageLevel.Warning,$"Error processing {node.Name} event for {node.GetAttribute("fullname")}"); _recorder.SendMessage(TestMessageLevel.Warning, ex.ToString()); } } diff --git a/src/NUnitTestAdapter/TestConverter.cs b/src/NUnitTestAdapter/TestConverter.cs index 1a7ea1a..6a3d198 100644 --- a/src/NUnitTestAdapter/TestConverter.cs +++ b/src/NUnitTestAdapter/TestConverter.cs @@ -77,7 +77,7 @@ namespace NUnit.VisualStudio.TestAdapter public TestCase ConvertTestCase(XmlNode testNode) { if (testNode == null || testNode.Name != "test-case") - throw new ArgumentException("The argument must be a test case", "test"); + throw new ArgumentException("The argument must be a test case", nameof(testNode)); // Return cached value if we have one string id = testNode.GetAttribute("id"); diff --git a/src/NUnitTestAdapterTests/AdapterSettingsTests.cs b/src/NUnitTestAdapterTests/AdapterSettingsTests.cs index 3277b82..6ba8821 100644 --- a/src/NUnitTestAdapterTests/AdapterSettingsTests.cs +++ b/src/NUnitTestAdapterTests/AdapterSettingsTests.cs @@ -1,8 +1,26 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +// *********************************************************************** +// Copyright (c) 2011-2017 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 +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// *********************************************************************** + using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; using NUnit.Framework; using NUnit.VisualStudio.TestAdapter.Tests.Fakes; @@ -16,7 +34,9 @@ namespace NUnit.VisualStudio.TestAdapter.Tests [SetUp] public void SetUp() { - _settings = new AdapterSettings(new TestLogger(new MessageLoggerStub(), 0)); + var testlogger = new TestLogger(new MessageLoggerStub()); + _settings = new AdapterSettings(testlogger); + testlogger.InitSettings(_settings); } [Test] diff --git a/src/NUnitTestAdapterTests/AsyncTests.cs b/src/NUnitTestAdapterTests/AsyncTests.cs index 5149f67..21c1a68 100644 --- a/src/NUnitTestAdapterTests/AsyncTests.cs +++ b/src/NUnitTestAdapterTests/AsyncTests.cs @@ -1,4 +1,26 @@ -using System; +// *********************************************************************** +// Copyright (c) 2011-2015 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 +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// *********************************************************************** +using System; using System.Threading.Tasks; using Microsoft.VisualStudio.TestPlatform.ObjectModel; using NUnit.Framework; diff --git a/src/NUnitTestAdapterTests/DumpXmlTests.cs b/src/NUnitTestAdapterTests/DumpXmlTests.cs index dc60c87..b1ea60f 100644 --- a/src/NUnitTestAdapterTests/DumpXmlTests.cs +++ b/src/NUnitTestAdapterTests/DumpXmlTests.cs @@ -1,4 +1,26 @@ -using System.Text.RegularExpressions; +// *********************************************************************** +// Copyright (c) 2017 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 +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// *********************************************************************** +using System.Text.RegularExpressions; using NSubstitute; using NUnit.Framework; using NUnit.VisualStudio.TestAdapter.Dump; diff --git a/src/NUnitTestAdapterTests/NUnitEventListenerTests.cs b/src/NUnitTestAdapterTests/NUnitEventListenerTests.cs index 3274908..b9cddcb 100644 --- a/src/NUnitTestAdapterTests/NUnitEventListenerTests.cs +++ b/src/NUnitTestAdapterTests/NUnitEventListenerTests.cs @@ -1,5 +1,5 @@ // *********************************************************************** -// Copyright (c) 2012-2015 Charlie Poole, Terje Sandstrom +// Copyright (c) 2012-2017 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 @@ -148,7 +148,7 @@ namespace NUnit.VisualStudio.TestAdapter.Tests { testLog = new FakeFrameworkHandle(); testConverter = new TestConverter(new TestLogger(new MessageLoggerStub()), FakeTestData.AssemblyPath, collectSourceInformation: true); - MarshalByRefObject localInstance = (MarshalByRefObject)Activator.CreateInstance(typeof(NUnitEventListener), testLog, testConverter); + var localInstance = (MarshalByRefObject)Activator.CreateInstance(typeof(NUnitEventListener), testLog, testConverter); RemotingServices.Marshal(localInstance); diff --git a/src/NUnitTestAdapterTests/ProjectTests.cs b/src/NUnitTestAdapterTests/ProjectTests.cs index caf0fc7..e425d18 100644 --- a/src/NUnitTestAdapterTests/ProjectTests.cs +++ b/src/NUnitTestAdapterTests/ProjectTests.cs @@ -1,4 +1,26 @@ -using System.IO; +// *********************************************************************** +// Copyright (c) 2011-2017 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 +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// *********************************************************************** +using System.IO; using System.Linq; using System.Reflection; using System.Xml.Linq; diff --git a/src/NUnitTestAdapterTests/TestExecutionTests.cs b/src/NUnitTestAdapterTests/TestExecutionTests.cs index a374ef3..da5fd82 100644 --- a/src/NUnitTestAdapterTests/TestExecutionTests.cs +++ b/src/NUnitTestAdapterTests/TestExecutionTests.cs @@ -1,5 +1,5 @@ // *********************************************************************** -// Copyright (c) 2012-2015 Charlie Poole, Terje Sandstrom +// Copyright (c) 2012-2017 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 @@ -143,7 +143,7 @@ namespace NUnit.VisualStudio.TestAdapter.Tests [TestCase("NotRunnableTest", TestOutcome.Failed, "No arguments were provided", false)] public void TestResultIsReportedCorrectly(string name, TestOutcome outcome, string message, bool hasStackTrace) { - TestResult testResult = GetTestResult(name); + var testResult = GetTestResult(name); Assert.NotNull(testResult, "Unable to find result for method: " + name); Assert.That(testResult.Outcome, Is.EqualTo(outcome)); @@ -155,7 +155,7 @@ namespace NUnit.VisualStudio.TestAdapter.Tests [Test] public void AttachmentsShowSupportMultipleFiles() { - TestResult test = GetTestResult(nameof(FixtureWithAttachment.AttachmentTest)); + var test = GetTestResult(nameof(FixtureWithAttachment.AttachmentTest)); Assert.That(test, Is.Not.Null, "Could not find test result"); Assert.That(test.Attachments.Count, Is.EqualTo(1)); diff --git a/src/NUnitTestAdapterTests/TestTFSBuild.cs b/src/NUnitTestAdapterTests/TestTFSBuild.cs index 186cb58..1b7c5c1 100644 --- a/src/NUnitTestAdapterTests/TestTFSBuild.cs +++ b/src/NUnitTestAdapterTests/TestTFSBuild.cs @@ -1,4 +1,26 @@ -using System; +// *********************************************************************** +// Copyright (c) 2011-2017 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 +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// *********************************************************************** +using System; using Microsoft.VisualStudio.TestPlatform.ObjectModel; using NUnit.Framework; diff --git a/src/NUnitTestAdapterTests/TraitsTests.cs b/src/NUnitTestAdapterTests/TraitsTests.cs index b25a6b3..9729332 100644 --- a/src/NUnitTestAdapterTests/TraitsTests.cs +++ b/src/NUnitTestAdapterTests/TraitsTests.cs @@ -9,7 +9,7 @@ using NUnit.Framework; namespace NUnit.VisualStudio.TestAdapter.Tests { - + public class TestDataForTraits { #region TestXml Data @@ -249,6 +249,107 @@ namespace NUnit.VisualStudio.TestAdapter.Tests "; + ///[Category("BaseClass")] + ///public class TestBase + ///{ + /// [Category("BaseMethod")] + /// [Test] + /// public void TestItBase() + /// { + /// Assert.That(true); + /// } + ///} + ///[Category("DerivedClass")] + ///public class Derived : TestBase + ///{ + /// [Category("DerivedMethod")] + /// [Test] + /// public void TestItDerived() + /// { + /// Assert.That(true); + /// } + ///} + private const string TestCaseWithInheritedTestsInSameAssembly = + @" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + "; + + ///[Category("BaseClass")] + ///public abstract class TestBase + ///{ + /// [Category("BaseMethod")] + /// [Test] + /// public void TestItBase() + /// { + /// Assert.That(true); + /// } + ///} + ///[Category("DerivedClass")] + ///public class Derived : TestBase + ///{ + /// [Category("DerivedMethod")] + /// [Test] + /// public void TestItDerived() + /// { + /// Assert.That(true); + /// } + ///} + private const string TestCaseWithAbstractInheritedTestsInSameAssembly = + @" + + + + + + + + + + + + + + + + + + + + + + +"; #endregion @@ -259,6 +360,10 @@ namespace NUnit.VisualStudio.TestAdapter.Tests public XmlNode XmlForStandardTest => XmlHelper.CreateXmlNode(TestXmlStandardClass); public XmlNode XmlForTestCaseWithCategory => XmlHelper.CreateXmlNode(TestCaseWithCategory); + + public XmlNode XmlForTestCaseWithInheritedTestsInSameAssembly => XmlHelper.CreateXmlNode(TestCaseWithInheritedTestsInSameAssembly); + public XmlNode XmlForTestCaseWithAbstractInheritedTestsInSameAssembly => XmlHelper.CreateXmlNode(TestCaseWithAbstractInheritedTestsInSameAssembly); + } #if !NETCOREAPP1_0 @@ -276,7 +381,10 @@ namespace NUnit.VisualStudio.TestAdapter.Tests { testDataForTraits = new TestDataForTraits(); var messagelogger = Substitute.For(); - var testlogger = new TestLogger(messagelogger, 5); + var adaptersettings = Substitute.For(); + adaptersettings.Verbosity.Returns(5); + var testlogger = new TestLogger(messagelogger); + testlogger.InitSettings(adaptersettings); testconverter = new TestConverter(testlogger, "whatever", false); testcaselist = new List(); @@ -326,6 +434,53 @@ namespace NUnit.VisualStudio.TestAdapter.Tests Assert.That(testcase1.Traits.Count(), Is.EqualTo(2), "Wrong number of categories for derived test case"); } + + [Test] + public void ThatInheritedConcreteClassesHaveTraits() + { + var xml = testDataForTraits.XmlForTestCaseWithInheritedTestsInSameAssembly; + ProcessXml2TestCase(xml); + Assert.That(testcaselist.Count, Is.EqualTo(3), "Wrong number of testcases found"); + var uniqueTraits = UniqueTraits(); + Assert.That(uniqueTraits.Count(),Is.EqualTo(4),"Wrong number of traits"); + string searchTrait = "BaseClass"; + var tcWithTrait = TcWithTrait(searchTrait); + Assert.That(tcWithTrait.Count(),Is.EqualTo(3),$"Wrong number of testcases found for trait={searchTrait}"); + + } + + private IEnumerable TcWithTrait(string searchTrait) + { + return testcaselist.Where(o => o.Traits.Select(t => t.Value).Contains(searchTrait)); + } + + private IEnumerable UniqueTraits() + { + var traits = new List(); + foreach (var tc in testcaselist) + { + traits.AddRange(tc.Traits.Select(o => o.Value)); + } + var uniqueTraits = traits.Distinct(); + return uniqueTraits; + } + + + [Test] + public void ThatInheritedAbstractClassesHaveTraits() + { + var xml = testDataForTraits.XmlForTestCaseWithAbstractInheritedTestsInSameAssembly; + ProcessXml2TestCase(xml); + Assert.That(testcaselist.Count, Is.EqualTo(2), "Wrong number of testcases found"); + var uniqueTraits = UniqueTraits(); + Assert.That(uniqueTraits.Count(), Is.EqualTo(4), "Wrong number of traits"); + string searchTrait = "BaseClass"; + var tcWithTrait = TcWithTrait(searchTrait); + Assert.That(tcWithTrait.Count(), Is.EqualTo(2), $"Wrong number of testcases found for trait={searchTrait}"); + + } + + private void ProcessXml2TestCase(XmlNode xml) { foreach (XmlNode node in xml.SelectNodes("//test-case")) @@ -358,12 +513,12 @@ namespace NUnit.VisualStudio.TestAdapter.Tests ProcessXml2TestCase(xml); Assert.That(testcaselist.Count, Is.EqualTo(3), "Wrong number of testcases found"); - var testcasesWithCategories = testcaselist.Where(o => o.Traits?.FirstOrDefault(p=>p.Name=="Category")!= null); + var testcasesWithCategories = testcaselist.Where(o => o.Traits?.FirstOrDefault(p => p.Name == "Category") != null); 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(); Assert.That(tc.Traits.Count(), Is.EqualTo(1), "Wrong number of categories for test case"); - Assert.That(tc.Traits.First().Value,Is.EqualTo("Single")); + Assert.That(tc.Traits.First().Value, Is.EqualTo("Single")); }