Skip to content

Commit

Permalink
Added tests for failure in discovery #302
Browse files Browse the repository at this point in the history
  • Loading branch information
OsirisTerje committed Jul 30, 2017
1 parent 37cc9f2 commit 164d948
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 15 deletions.
Binary file modified NUnit3TestAdapter.sln
Binary file not shown.
2 changes: 1 addition & 1 deletion src/NUnitTestAdapter/NUnit3TestDiscoverer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ***********************************************************************
// Copyright (c) 2011-2015 Charlie Poole, Terje Sandstrom
// 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
Expand Down
17 changes: 15 additions & 2 deletions src/NUnitTestAdapterTests/Fakes/MessageLoggerStub.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ***********************************************************************
// Copyright (c) 2015 Charlie Poole, Terje Sandstrom
// Copyright (c) 2015-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
Expand All @@ -21,15 +21,28 @@
// 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.Logging;

namespace NUnit.VisualStudio.TestAdapter.Tests.Fakes
{
public class MessageLoggerStub : IMessageLogger
{
private readonly List<Tuple<TestMessageLevel, string>> messages = new List<Tuple<TestMessageLevel, string>>();
public void SendMessage(TestMessageLevel testMessageLevel, string message)
{
// Do nothing
messages.Add(new Tuple<TestMessageLevel, string>(testMessageLevel, message));
}

public TestMessageLevel LatestTestMessageLevel => messages.Last().Item1;
public string LatestMessage => messages.Last().Item2;

public int Count => messages.Count;

public IEnumerable<Tuple<TestMessageLevel, string>> Messages => messages;
public IEnumerable<Tuple<TestMessageLevel, string>> WarningMessages => messages.Where(o => o.Item1 == TestMessageLevel.Warning);
public IEnumerable<Tuple<TestMessageLevel, string>> ErrorMessages => messages.Where(o => o.Item1 == TestMessageLevel.Error);
}
}
11 changes: 11 additions & 0 deletions src/NUnitTestAdapterTests/NUnit.TestAdapter.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
<OutputType>exe</OutputType>
<OutputTypeEx>exe</OutputTypeEx>
</PropertyGroup>
<ItemGroup>
<Content Include="..\native-assembly\NativeTests.dll" Link="NativeTests.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.7.1" />
Expand All @@ -32,4 +37,10 @@
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

<ItemGroup>
<Compile Update="NavigationDataTests.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
</ItemGroup>
</Project>
88 changes: 76 additions & 12 deletions src/NUnitTestAdapterTests/TestDiscoveryTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ***********************************************************************
// Copyright (c) 2011-2015 Charlie Poole, Terje Sandstrom
// 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
Expand All @@ -24,10 +24,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using NUnit.Framework;
using Enumerable = System.Linq.Enumerable;

namespace NUnit.VisualStudio.TestAdapter.Tests
{
Expand All @@ -47,7 +49,7 @@ public static IEnumerable<IDiscoveryContext> TestDiscoveryData()
[TestFixtureSource(typeof(TestDiscoveryDataProvider), nameof(TestDiscoveryDataProvider.TestDiscoveryData))]
public class TestDiscoveryTests : ITestCaseDiscoverySink
{
static readonly string MockAssemblyPath =
static readonly string MockAssemblyPath =
Path.Combine(TestContext.CurrentContext.TestDirectory, "mock-assembly.dll");

List<TestCase> TestCases;
Expand All @@ -74,9 +76,9 @@ public void LoadMockassembly()
// the list of test cases sent to the discovery sink
nunittestDiscoverer = ((ITestDiscoverer)new NUnit3TestDiscoverer());
nunittestDiscoverer.DiscoverTests(
new[] { MockAssemblyPath},
_context,
new MessageLoggerStub(),
new[] { MockAssemblyPath },
_context,
new MessageLoggerStub(),
this);
}

Expand All @@ -102,7 +104,7 @@ public void VerifyTestCaseIsFound(string name, string fullName)
public void VerifyNestedTestCaseSourceIsAvailable(string name)
{
var testCase = TestCases.Find(tc => tc.DisplayName == name);

Assert.That(!string.IsNullOrEmpty(testCase.Source));
Assert.Greater(testCase.LineNumber, 0);
}
Expand All @@ -120,29 +122,91 @@ void ITestCaseDiscoverySink.SendTestCase(TestCase discoveredTest)
[Category("TestDiscovery")]
public class EmptyAssemblyDiscoveryTests : ITestCaseDiscoverySink
{
static readonly string EmptyAssemblyPath =
static readonly string EmptyAssemblyPath =
Path.Combine(TestContext.CurrentContext.TestDirectory, "empty-assembly.dll");

private static ITestDiscoverer nunittestDiscoverer;

[TestCaseSource(typeof(TestDiscoveryDataProvider), nameof(TestDiscoveryDataProvider.TestDiscoveryData))]
public void VerifyLoading(IDiscoveryContext context)
{
// Load the NUnit empty-assembly.dll once for this test
nunittestDiscoverer = ((ITestDiscoverer)new NUnit3TestDiscoverer());
nunittestDiscoverer.DiscoverTests(
new[] { EmptyAssemblyPath},
context,
new MessageLoggerStub(),
new[] { EmptyAssemblyPath },
context,
new MessageLoggerStub(),
this);
}

#region ITestCaseDiscoverySink Methods
#region ITestCaseDiscoverySink Methods

void ITestCaseDiscoverySink.SendTestCase(TestCase discoveredTest)
{
}

#endregion
}

[Category("TestDiscovery")]
public class FailuresInDiscovery : ITestCaseDiscoverySink
{
bool testcaseWasSent;


[SetUp]
public void Setup()
{
testcaseWasSent = false;
}

[Test]
public void WhenAssemblyDontExist()
{

#if DEBUG
int noOfMessagesFound = 4; // Start + end , one debug + info
#else
int noOfMessagesFound = 3; // Start + end, + info
#endif
var nunittestDiscoverer = new NUnit3TestDiscoverer();
var context = new FakeDiscoveryContext(null);
var messageLoggerStub = new MessageLoggerStub();
nunittestDiscoverer.DiscoverTests(
new[] { "FileThatDoesntExist.dll" },
context,
messageLoggerStub,
this);
Assert.That(messageLoggerStub.Count, Is.EqualTo(noOfMessagesFound));
Assert.That(messageLoggerStub.LatestTestMessageLevel, Is.EqualTo(TestMessageLevel.Informational));
Assert.That(testcaseWasSent, Is.False);
Assert.That(!messageLoggerStub.ErrorMessages.Any());
}

[Test]
public void WhenAssemblyIsNative()
{
var nunittestDiscoverer = new NUnit3TestDiscoverer();
var context = new FakeDiscoveryContext(null);
var messageLoggerStub = new MessageLoggerStub();
var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "NativeTests.dll");
Assert.That(File.Exists(path));
nunittestDiscoverer.DiscoverTests(
new[] { path },
context,
messageLoggerStub,
this);
Assert.That(testcaseWasSent, Is.False);
Assert.That(messageLoggerStub.WarningMessages.Count(), Is.EqualTo(1));
Assert.That(!messageLoggerStub.ErrorMessages.Any());
var warningmsg = messageLoggerStub.WarningMessages.Select(o => o.Item2).Single();
Assert.That(warningmsg, Does.Contain("Assembly not supported"));
}

public void SendTestCase(TestCase discoveredTest)
{
testcaseWasSent = true;
}
}

}
Binary file added src/native-assembly/NativeTests.dll
Binary file not shown.

0 comments on commit 164d948

Please sign in to comment.