Skip to content

Commit

Permalink
Test both APIs under .NET Framework; switch APIs automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
CharliePoole committed Jan 16, 2025
1 parent b9ce49c commit 9543ea0
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 18 deletions.
31 changes: 31 additions & 0 deletions NUnitConsole.sln
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WpfTest", "src\TestData\wpf
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WpfApp", "src\TestData\WpfApp\WpfApp.csproj", "{93D182B7-2F63-4661-BB32-94F1716CF03F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "OlderNUnit", "OlderNUnit", "{D31607D2-D689-488B-9F9F-92F47AC1D7F6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NUnit3.0", "src\TestData\NUnit3.0\NUnit3.0.csproj", "{2448CE80-59AA-41B7-83A0-768BE5520F7A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NUnit3.0.1", "src\TestData\NUnit3.0.1\NUnit3.0.1.csproj", "{56416AD9-8E7B-4F72-B0E1-E75A024431F8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NUnit3.2", "src\TestData\NUnit3.2\NUnit3.2.csproj", "{8AFBB856-700A-4CCC-AE0E-9B622178E1E0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NUnit3.10", "src\TestData\NUnit3.10\NUnit3.10.csproj", "{0555B97D-E918-455B-951C-74EFCDA8790A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -241,6 +251,22 @@ Global
{93D182B7-2F63-4661-BB32-94F1716CF03F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{93D182B7-2F63-4661-BB32-94F1716CF03F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{93D182B7-2F63-4661-BB32-94F1716CF03F}.Release|Any CPU.Build.0 = Release|Any CPU
{2448CE80-59AA-41B7-83A0-768BE5520F7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2448CE80-59AA-41B7-83A0-768BE5520F7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2448CE80-59AA-41B7-83A0-768BE5520F7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2448CE80-59AA-41B7-83A0-768BE5520F7A}.Release|Any CPU.Build.0 = Release|Any CPU
{56416AD9-8E7B-4F72-B0E1-E75A024431F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{56416AD9-8E7B-4F72-B0E1-E75A024431F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{56416AD9-8E7B-4F72-B0E1-E75A024431F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{56416AD9-8E7B-4F72-B0E1-E75A024431F8}.Release|Any CPU.Build.0 = Release|Any CPU
{8AFBB856-700A-4CCC-AE0E-9B622178E1E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8AFBB856-700A-4CCC-AE0E-9B622178E1E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8AFBB856-700A-4CCC-AE0E-9B622178E1E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8AFBB856-700A-4CCC-AE0E-9B622178E1E0}.Release|Any CPU.Build.0 = Release|Any CPU
{0555B97D-E918-455B-951C-74EFCDA8790A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0555B97D-E918-455B-951C-74EFCDA8790A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0555B97D-E918-455B-951C-74EFCDA8790A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0555B97D-E918-455B-951C-74EFCDA8790A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -280,6 +306,11 @@ Global
{48DF1E40-93BA-436A-B460-5D1130316ADA} = {37D508B2-91E0-4B32-869B-DFF9E68EA213}
{2F9D8932-2186-464F-BED6-7D7979C8FFA6} = {37D508B2-91E0-4B32-869B-DFF9E68EA213}
{93D182B7-2F63-4661-BB32-94F1716CF03F} = {37D508B2-91E0-4B32-869B-DFF9E68EA213}
{D31607D2-D689-488B-9F9F-92F47AC1D7F6} = {37D508B2-91E0-4B32-869B-DFF9E68EA213}
{2448CE80-59AA-41B7-83A0-768BE5520F7A} = {D31607D2-D689-488B-9F9F-92F47AC1D7F6}
{56416AD9-8E7B-4F72-B0E1-E75A024431F8} = {D31607D2-D689-488B-9F9F-92F47AC1D7F6}
{8AFBB856-700A-4CCC-AE0E-9B622178E1E0} = {D31607D2-D689-488B-9F9F-92F47AC1D7F6}
{0555B97D-E918-455B-951C-74EFCDA8790A} = {D31607D2-D689-488B-9F9F-92F47AC1D7F6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D8E4FC26-5422-4C51-8BBC-D1AC0A578711}
Expand Down
40 changes: 40 additions & 0 deletions package-tests.cake
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,46 @@ StandardRunnerTests.Add(new PackageTest(
"testdata/net462/mock-assembly.dll testdata/net6.0/mock-assembly.dll",
new MockAssemblyExpectedResult("net-4.6.2", "netcore-6.0")));

//////////////////////////////////////////////////////////////////////
// TEST OLDER VERSIONS OF NUNIT SWITCHING API IF NEEDED
//////////////////////////////////////////////////////////////////////

StandardRunnerTests.Add(new PackageTest(
1, "NUnit30Test",
"Run a test under NUnit 3.0 using 2009 API",
"testdata/NUnit3.0/net462/NUnit3.0.dll",
new ExpectedResult("Passed")
{
Assemblies = new[] { new ExpectedAssemblyResult("NUnit3.0.dll", "net462") }
}));

StandardRunnerTests.Add(new PackageTest(
1, "NUnit301Test",
"Run a test under NUnit 3.0.1 using 2009 API",
"testdata/NUnit3.0/net462/NUnit3.0.1.dll",
new ExpectedResult("Passed")
{
Assemblies = new[] { new ExpectedAssemblyResult("NUnit3.0.1.dll", "net462") }
}));

StandardRunnerTests.Add(new PackageTest(
1, "NUnit32Test",
"Run a test under NUnit 3.2 using 20018 API",
"testdata/NUnit3.0/net462/NUnit3.2.dll",
new ExpectedResult("Passed")
{
Assemblies = new[] { new ExpectedAssemblyResult("NUnit3.2.dll", "net462") }
}));

StandardRunnerTests.Add(new PackageTest(
1, "NUnit310Test",
"Run a test under NUnit 3.10 using 2018 API",
"testdata/NUnit3.0/net462/NUnit3.10.dll",
new ExpectedResult("Passed")
{
Assemblies = new[] { new ExpectedAssemblyResult("NUnit3.10.dll", "net462") }
}));

//////////////////////////////////////////////////////////////////////
// ASP.NETCORE TESTS
//////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
namespace NUnit.Engine.Drivers
{
// Functional tests of the NUnitFrameworkDriver calling into the framework.
#if NETFRAMEWORK
[TestFixture("2009")]
[TestFixture("2018")]
#endif
public class NUnitFrameworkDriverTests
{
private const string MOCK_ASSEMBLY = "mock-assembly.dll";
Expand All @@ -22,13 +26,22 @@ public class NUnitFrameworkDriverTests
private NUnitFrameworkDriver _driver;
private string _mockAssemblyPath;

#if NETFRAMEWORK
private string _whichApi;
public NUnitFrameworkDriverTests(string whichApi)
{
_whichApi = whichApi;
}
#endif

[SetUp]
public void CreateDriver()
{
var nunitRef = typeof(NUnit.Framework.TestAttribute).Assembly.GetName();
var nunitRef = typeof(TestAttribute).Assembly.GetName();
_mockAssemblyPath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, MOCK_ASSEMBLY);

#if NETFRAMEWORK
_driver = new NUnitFrameworkDriver(AppDomain.CurrentDomain, "99", nunitRef);
_driver = new NUnitFrameworkDriver(AppDomain.CurrentDomain, _whichApi, "99", nunitRef);
#else
_driver = new NUnitFrameworkDriver("99", nunitRef);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class NUnitFrameworkApi2009 : NUnitFrameworkApi

const string CONTROLLER_TYPE = "NUnit.Framework.Api.FrameworkController";

NUnitFrameworkDriver _driver;
string _driverId;

AppDomain _testDomain;
AssemblyName _nunitRef;
Expand All @@ -37,10 +37,14 @@ class NUnitFrameworkApi2009 : NUnitFrameworkApi
object? _frameworkController;
Type? _frameworkControllerType;

public NUnitFrameworkApi2009(NUnitFrameworkDriver driver, AppDomain testDomain, AssemblyName nunitRef)
public NUnitFrameworkApi2009(AppDomain testDomain, string driverId, AssemblyName nunitRef)
{
_driver = driver;
Guard.ArgumentNotNull(testDomain, nameof(testDomain));
Guard.ArgumentNotNull(driverId, nameof(driverId));
Guard.ArgumentNotNull(nunitRef, nameof(nunitRef));

_testDomain = testDomain;
_driverId = driverId;
_nunitRef = nunitRef;
}

Expand All @@ -56,7 +60,8 @@ public string Load(string testAssemblyPath, IDictionary<string, object> settings
var requestedRuntime = settings.ContainsKey(EnginePackageSettings.RequestedRuntimeFramework)
? settings[EnginePackageSettings.RequestedRuntimeFramework] : null;

var idPrefix = string.IsNullOrEmpty(_driver.ID) ? "" : _driver.ID + "-";
var idPrefix = string.IsNullOrEmpty(_driverId) ? "" : _driverId + "-";

try
{
_frameworkController = CreateObject(CONTROLLER_TYPE, _testAssemblyPath, idPrefix, settings);
Expand Down
34 changes: 22 additions & 12 deletions src/NUnitEngine/nunit.engine.core/Drivers/NUnitFrameworkDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using NUnit.Common;
using NUnit.Engine.Internal;
using NUnit.Engine.Extensibility;
using NUnit.Engine.Runners;

namespace NUnit.Engine.Drivers
{
Expand All @@ -27,25 +28,34 @@ public class NUnitFrameworkDriver : IFrameworkDriver
/// <param name="testDomain">The application domain in which to create the FrameworkController</param>
/// <param name="nunitRef">An AssemblyName referring to the test framework.</param>
public NUnitFrameworkDriver(AppDomain testDomain, string id, AssemblyName nunitRef)
: this(testDomain, nunitRef.Version >= new Version(3,2,0) ? "2018" : "2009", id, nunitRef) { }

/// <summary>
/// Internal generic constructor used directly by our tests.
/// </summary>
/// <param name="testDomain">The application domain in which to create the FrameworkController</param>
/// <param name="nunitRef">An AssemblyName referring to the test framework.</param>
internal NUnitFrameworkDriver(AppDomain testDomain, string api, string id, AssemblyName nunitRef)
{
Guard.ArgumentNotNull(testDomain, nameof(testDomain));
Guard.ArgumentNotNull(api, nameof(api));
Guard.ArgumentValid(api == "2009" || api == "2018", $"Invalid API specified: {api}", nameof(api));
Guard.ArgumentNotNullOrEmpty(id, nameof(id));
Guard.ArgumentNotNull(nunitRef, nameof(nunitRef));

ID = id;

// Under .NET Framework, the Api class must be injected in the test domain
//testDomain.AssemblyResolve += TestDomain_AssemblyResolve;

_api = (NUnitFrameworkApi2018)testDomain.CreateInstanceFromAndUnwrap(
Assembly.GetExecutingAssembly().Location,
typeof(NUnitFrameworkApi2018).FullName!,
false,
0,
null,
new object[] { ID, nunitRef },
null,
null).ShouldNotBeNull();
_api = api == "2018"
? (NUnitFrameworkApi)testDomain.CreateInstanceFromAndUnwrap(
Assembly.GetExecutingAssembly().Location,
$"NUnit.Engine.Drivers.NUnitFrameworkApi{api}",
false,
0,
null,
new object[] { ID, nunitRef },
null,
null).ShouldNotBeNull()
: new NUnitFrameworkApi2009(testDomain, ID, nunitRef);
}

private Assembly? TestDomain_AssemblyResolve(object? sender, ResolveEventArgs args)
Expand Down
13 changes: 13 additions & 0 deletions src/TestData/NUnit3.0.1/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using NUnit.Framework;

namespace NUnit.Engine.TestData
{
public class NUnit_3_0_1_Test
{
[Test]
public void Test() { Assert.Pass("test passes"); }
}
}
13 changes: 13 additions & 0 deletions src/TestData/NUnit3.0.1/NUnit3.0.1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>NUnit.TestData</RootNamespace>
<TargetFramework>net462</TargetFramework>
<OutputPath>../../../bin/$(Configuration)/testdata/nunit3.0</OutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.0.0" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions src/TestData/NUnit3.0/NUnit3.0.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>NUnit.TestData</RootNamespace>
<TargetFramework>net462</TargetFramework>
<OutputPath>../../../bin/$(Configuration)/testdata/nunit3.0</OutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.0.0" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions src/TestData/NUnit3.0/NUnit_3_0_Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using NUnit.Framework;

namespace NUnit.Engine.TestData
{
public class NUnit_3_0_Test
{
[Test]
public void Test() { Assert.Pass("test passes"); }
}
}
13 changes: 13 additions & 0 deletions src/TestData/NUnit3.10/NUnit3.10.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>NUnit.TestData</RootNamespace>
<TargetFramework>net462</TargetFramework>
<OutputPath>../../../bin/$(Configuration)/testdata/nunit3.0</OutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.0.0" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions src/TestData/NUnit3.10/NUnit_3_10_Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using NUnit.Framework;

namespace NUnit.Engine.TestData
{
public class NUnit_3_10_Test
{
[Test]
public void Test() { Assert.Pass("test passes"); }
}
}
13 changes: 13 additions & 0 deletions src/TestData/NUnit3.2/NUnit3.2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>NUnit.TestData</RootNamespace>
<TargetFramework>net462</TargetFramework>
<OutputPath>../../../bin/$(Configuration)/testdata/nunit3.0</OutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.0.0" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions src/TestData/NUnit3.2/NUnit_3_2_Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using NUnit.Framework;

namespace NUnit.Engine.TestData
{
public class NUnit_3_2_Test
{
[Test]
public void Test() { Assert.Pass("test passes"); }
}
}

0 comments on commit 9543ea0

Please sign in to comment.