Skip to content

Commit

Permalink
Added unit test for NUnitEventListener #516
Browse files Browse the repository at this point in the history
  • Loading branch information
OsirisTerje committed Jun 25, 2018
1 parent 835a9f7 commit 1360cb2
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/NUnitTestAdapter/Internal/Extensions.cs
Expand Up @@ -3,7 +3,6 @@
namespace NUnit.VisualStudio.TestAdapter.Internal
{
#if NET35

public static class TypeExtensions
{
public static Type GetTypeInfo(this Type type) => type;
Expand Down
1 change: 1 addition & 0 deletions src/NUnitTestAdapter/NUnit.TestAdapter.csproj
Expand Up @@ -4,6 +4,7 @@
<PropertyGroup>
<AssemblyName>NUnit3.TestAdapter</AssemblyName>
<RootNamespace>NUnit.VisualStudio.TestAdapter</RootNamespace>
<!--<TargetFramework>net35</TargetFramework>--><!-- For testing and debugging-->
<TargetFrameworks>net35;netcoreapp1.0</TargetFrameworks>
<CodeAnalysisRuleSet>..\..\Osiris.Extended.ruleset</CodeAnalysisRuleSet>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
Expand Down
6 changes: 3 additions & 3 deletions src/NUnitTestAdapter/NUnitEventListener.cs
Expand Up @@ -46,7 +46,7 @@ public class NUnitEventListener :
ITestEventListener, IDisposable // Public for testing
{
private readonly ITestExecutionRecorder _recorder;
private readonly TestConverter _testConverter;
private readonly ITestConverter _testConverter;

#if !NETCOREAPP1_0
public override object InitializeLifetimeService()
Expand All @@ -59,7 +59,7 @@ public override object InitializeLifetimeService()
}
#endif

public NUnitEventListener(ITestExecutionRecorder recorder, TestConverter testConverter, IDumpXml dumpXml)
public NUnitEventListener(ITestExecutionRecorder recorder, ITestConverter testConverter, IDumpXml dumpXml)
{
this.dumpXml = dumpXml;
_recorder = recorder;
Expand Down Expand Up @@ -156,7 +156,7 @@ public void SuiteFinished(XmlNode resultNode)
{
var result = resultNode.GetAttribute("result");
var site = resultNode.GetAttribute("site");

if (result == "Failed")
{
if (site == "SetUp" || site == "TearDown")
Expand Down
8 changes: 7 additions & 1 deletion src/NUnitTestAdapter/TestConverter.cs
Expand Up @@ -32,7 +32,13 @@

namespace NUnit.VisualStudio.TestAdapter
{
public sealed class TestConverter : IDisposable
public interface ITestConverter
{
TestCase GetCachedTestCase(string id);
TestConverter.TestResultSet GetVSTestResults(XmlNode resultNode);
}

public sealed class TestConverter : IDisposable, ITestConverter
{
private readonly ITestLogger _logger;
private readonly Dictionary<string, TestCase> _vsTestCaseMap;
Expand Down
7 changes: 2 additions & 5 deletions src/NUnitTestAdapterTests/ExtensionsTests.cs
Expand Up @@ -21,11 +21,6 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using NUnit.VisualStudio.TestAdapter.Internal;

Expand All @@ -38,6 +33,8 @@ public class ExtensionsTests
[TestCase(" \t")]
[TestCase(" ")]
[TestCase("")]
[TestCase(null)]
[TestCase("\r\n")]
public void ThatIsNullOrWhiteSpaceHandlesTabs(string value)
{
var res = StringExtensions.IsNullOrWhiteSpace(value);
Expand Down
3 changes: 2 additions & 1 deletion src/NUnitTestAdapterTests/NUnit.TestAdapter.Tests.csproj
Expand Up @@ -4,7 +4,8 @@
<PropertyGroup>
<RootNamespace>NUnit.VisualStudio.TestAdapter.Tests</RootNamespace>
<AssemblyName>NUnit.VisualStudio.TestAdapter.Tests</AssemblyName>
<TargetFrameworks>net46;netcoreapp1.0</TargetFrameworks>
<TargetFrameworks>net35;netcoreapp1.0</TargetFrameworks>
<!--<TargetFrameworks>net46</TargetFrameworks>--> <!-- For testing and debugging-->
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
Expand Down
52 changes: 49 additions & 3 deletions src/NUnitTestAdapterTests/NUnitEventListenerTests.cs
Expand Up @@ -29,7 +29,11 @@
#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;
using NUnit.VisualStudio.TestAdapter.Tests.Fakes;

using VSTestResult = Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult;
Expand Down Expand Up @@ -111,7 +115,7 @@ public void TestFinished_CallsRecordResultCorrectly()
Assume.That(
testLog.Events[1].EventType,
Is.EqualTo(FakeFrameworkHandle.EventType.RecordResult));

VerifyTestResult(testLog.Events[1].TestResult);
}

Expand Down Expand Up @@ -150,11 +154,11 @@ public void Listener_LeaseLifetimeWillNotExpire()
testLog = new FakeFrameworkHandle();
using (var testConverter = new TestConverter(new TestLogger(new MessageLoggerStub()), FakeTestData.AssemblyPath, collectSourceInformation: true))
{
var localInstance = (MarshalByRefObject) Activator.CreateInstance(typeof(NUnitEventListener), testLog, testConverter, null);
var localInstance = (MarshalByRefObject)Activator.CreateInstance(typeof(NUnitEventListener), testLog, testConverter, null);

RemotingServices.Marshal(localInstance);

var lifetime = ((MarshalByRefObject) localInstance).GetLifetimeService();
var lifetime = ((MarshalByRefObject)localInstance).GetLifetimeService();

// A null lifetime (as opposed to an ILease) means the object has an infinite lifetime
Assert.IsNull(lifetime);
Expand Down Expand Up @@ -191,4 +195,46 @@ private void VerifyTestResult(VSTestResult ourResult)

#endregion
}

public class NUnitEventListenerOutputTests
{
private ITestExecutionRecorder recorder;
private ITestConverter converter;
private IDumpXml dumpxml;

private const string Testoutput =
@"<test-output stream='Progress' testid='0-1001' testname='Something.TestClass.Whatever'><![CDATA[Whatever
]]></test-output>";

private const string BlankTestoutput =
@"<test-output stream='Progress' testid='0-1001' testname='Something.TestClass.Whatever'><![CDATA[ ]]></test-output>";

[SetUp]
public void Setup()
{
recorder = Substitute.For<ITestExecutionRecorder>();
converter = Substitute.For<ITestConverter>();
dumpxml = Substitute.For<IDumpXml>();
}

[Test]
public void ThatNormalTestOutputIsOutput()
{
var sut = new NUnitEventListener(recorder, converter, dumpxml);

sut.OnTestEvent(Testoutput);

recorder.Received().SendMessage(Arg.Any<TestMessageLevel>(), "Whatever");
}

[Test]
public void ThatTestOutputWithWhiteSpaceIsNotOutput()
{
var sut = new NUnitEventListener(recorder, converter, dumpxml);

sut.OnTestEvent(BlankTestoutput);

recorder.DidNotReceive().SendMessage(Arg.Any<TestMessageLevel>(), Arg.Any<string>());
}
}
}

0 comments on commit 1360cb2

Please sign in to comment.