Skip to content

Commit

Permalink
Enable engine tests to run in the same AppDomain as the adapter's engine
Browse files Browse the repository at this point in the history
  • Loading branch information
jnm2 committed Feb 11, 2018
1 parent 2709fba commit bf648f5
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/NUnitTestAdapter/NUnit.TestAdapter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<ItemGroup Condition="'$(TargetFramework)' == 'net35'">
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="11.0.0" PrivateAssets="All" />
<PackageReference Include="Mono.Cecil" Version="0.9.6.4" />
<PackageReference Include="nunit.engine" Version="3.7.0" PrivateAssets="All" />
<PackageReference Include="nunit.engine" Version="3.7.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp1.0'">
Expand Down
6 changes: 5 additions & 1 deletion src/NUnitTestAdapter/NUnitTestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ protected NUnitTestAdapter()

#endregion

internal event Action<TestEngineClass> InternalEngineCreated;

#region Properties

public AdapterSettings Settings { get; private set; }
Expand Down Expand Up @@ -117,7 +119,9 @@ public static bool IsRunningUnderIDE
// Discover or Execute method must call this method.
protected void Initialize(IDiscoveryContext context, IMessageLogger messageLogger)
{
TestEngine = new TestEngineClass();
var engine = new TestEngineClass();
InternalEngineCreated?.Invoke(engine);
TestEngine = engine;
TestLog = new TestLogger(messageLogger);
Settings = new AdapterSettings(TestLog);
TestLog.InitSettings(Settings);
Expand Down
66 changes: 66 additions & 0 deletions src/NUnitTestAdapterTests/TestAdapterUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// ***********************************************************************
// Copyright (c) 2018 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.Engine.Services;
using NUnit.Framework;

namespace NUnit.VisualStudio.TestAdapter.Tests
{
internal static class TestAdapterUtils
{
public static ITestDiscoverer CreateDiscoverer()
{
var discoverer = new NUnit3TestDiscoverer();
InitializeForTesting(discoverer);
return discoverer;
}

public static ITestExecutor CreateExecutor()
{
var executor = new NUnit3TestExecutor();
InitializeForTesting(executor);
return executor;
}

private static void InitializeForTesting(NUnitTestAdapter adapter)
{
#if NET45
adapter.InternalEngineCreated += engine =>
{
engine.Services.Add(new SettingsService(true));
engine.Services.Add(new DomainManager());
engine.Services.Add(new ExtensionService());
engine.Services.Add(new DriverService());
engine.Services.Add(new RecentFilesService());
engine.Services.Add(new ProjectService());
engine.Services.Add(new RuntimeFrameworkService());
engine.Services.Add(new DefaultTestRunnerFactory());
engine.Services.Add(new TestAgency("TestAgency for " + TestContext.CurrentContext.Test.Name, 0));
engine.Services.Add(new ResultService());
engine.Services.Add(new TestFilterService());
};
#endif
}
}
}
18 changes: 6 additions & 12 deletions src/NUnitTestAdapterTests/TestDiscoveryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public void LoadMockassembly()

// Load the NUnit mock-assembly.dll once for this test, saving
// the list of test cases sent to the discovery sink
nunittestDiscoverer = ((ITestDiscoverer)new NUnit3TestDiscoverer());
nunittestDiscoverer.DiscoverTests(
TestAdapterUtils.CreateDiscoverer().DiscoverTests(
new[] { MockAssemblyPath },
_context,
new MessageLoggerStub(),
Expand Down Expand Up @@ -125,27 +124,24 @@ public class EmptyAssemblyDiscoveryTests : ITestCaseDiscoverySink
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(
TestAdapterUtils.CreateDiscoverer().DiscoverTests(
new[] { EmptyAssemblyPath },
context,
new MessageLoggerStub(),
this);
}

#region ITestCaseDiscoverySink Methods
#region ITestCaseDiscoverySink Methods

void ITestCaseDiscoverySink.SendTestCase(TestCase discoveredTest)
{
}

#endregion
#endregion
}

[Category("TestDiscovery")]
Expand All @@ -164,10 +160,9 @@ public void Setup()
public void WhenAssemblyDontExist()
{
int noOfMessagesFound = 3; // Start + end, + info
var nunittestDiscoverer = new NUnit3TestDiscoverer();
var context = new FakeDiscoveryContext(null);
var messageLoggerStub = new MessageLoggerStub();
nunittestDiscoverer.DiscoverTests(
TestAdapterUtils.CreateDiscoverer().DiscoverTests(
new[] { "FileThatDoesntExist.dll" },
context,
messageLoggerStub,
Expand All @@ -182,12 +177,11 @@ public void WhenAssemblyDontExist()
[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(
TestAdapterUtils.CreateDiscoverer().DiscoverTests(
new[] { path },
context,
messageLoggerStub,
Expand Down
4 changes: 1 addition & 3 deletions src/NUnitTestAdapterTests/TestExecutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class TestExecutionTests
static readonly IRunContext Context = new FakeRunContext();

private FakeFrameworkHandle testLog;
private static ITestExecutor executor;
ResultSummary Summary { get; set; }


Expand All @@ -59,8 +58,7 @@ public void LoadMockassembly()

// Load the NUnit mock-assembly.dll once for this test, saving
// the list of test cases sent to the discovery sink
executor = new NUnit3TestExecutor();
executor.RunTests(new[] { MockAssemblyPath }, Context, testLog);
TestAdapterUtils.CreateExecutor().RunTests(new[] { MockAssemblyPath }, Context, testLog);

var testResults = testLog.Events
.Where(e => e.EventType == FakeFrameworkHandle.EventType.RecordResult)
Expand Down

0 comments on commit bf648f5

Please sign in to comment.