Skip to content

Commit

Permalink
Targets #420 Assembly loading when on TestRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
alelom committed Feb 24, 2023
1 parent 9b473eb commit 09b890c
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@

namespace BH.Test.Engine.NUnit
{
public class NUnitSampleTestClass : NUnitTest
public class CorrectlyReferencedAssembliesTests : NUnitTest
{
[Test]
public void VerifyLoadedAssemblies()
{
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
var domainLoadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();

var assembliesIncluidingAnalytical = loadedAssemblies.Select(a => a.GetName().Name);
var loadedAssemblies = domainLoadedAssemblies.Select(a => a.GetName().Name);

assembliesIncluidingAnalytical.ShouldContain("Dimensional_oM");
assembliesIncluidingAnalytical.ShouldContain("Analytical_oM");
loadedAssemblies.ShouldContain("Dimensional_oM");
loadedAssemblies.ShouldContain("Analytical_oM");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,16 @@

namespace BH.Test.Engine.NUnit
{
public class NUnitSampleTestClass
public class SampleTestClass : NUnitTest { }

public class BadlyReferencedAssembliesTests
{
[Test]
public void LoadAssembliesShouldThrowException()
{
NUnitTest testClass = new NUnitTest();
NUnitTest testClass = new SampleTestClass();

Should.Throw(() => testClass.LoadReferencedAssemblies(), typeof(FileLoadException));
}

[Test]
public void ErrorReportingPassingTest()
{
BH.Engine.Base.Compute.RecordError($"Some error logged via BH.Engine.Base.Compute.{nameof(Compute.RecordError)}");
Assert.Pass(); // Make it pass intentionally here; the recorded error should still make the test report failure on TearDown
}

[Test]
public void WarningReportingPassingTest()
{
BH.Engine.Base.Compute.RecordWarning($"Some warning logged via BH.Engine.Base.Compute.{nameof(Compute.RecordWarning)}");
Assert.Pass();
}


[Test]
public void NoteReportingPassingTest()
{
BH.Engine.Base.Compute.RecordNote($"Some note logged via BH.Engine.Base.Compute.{nameof(Compute.RecordNote)}");
Assert.Pass();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>

<Configurations>Debug;Release;Test</Configurations>

<OutputType>Library</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit.Engine" Version="3.16.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Shouldly" Version="4.1.0" />
<PackageReference Include="System.Data.Odbc" Version="7.0.0" />
<PackageReference Include="System.Data.OleDb" Version="7.0.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
<PackageReference Include="System.IO.Ports" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\NUnit_Engine\NUnit_Engine.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Test_oM">
<HintPath>C:\ProgramData\BHoM\Assemblies\Test_oM.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions .ci/unit-tests/NUnit_TestRunner_Tests/TestsRunner_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using BH.oM.Test.NUnit;
using NUnit.Framework;
using BH.Engine.Test.NUnit;
using BH.oM.Test.Results;
using Newtonsoft.Json;
using Shouldly;

namespace NUnit_TestRunner_Tests
{
public class TestsRunner_Tests
{
[Test]
public void Run_NUnit_Engine_Tests()
{
string? solutionPath = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory)?.Parent?.Parent?.Parent?.Parent?.FullName;
string NUnit_Engine_Tests_path = Path.Combine(solutionPath ?? "", "NUnit_Engine_Tests\\bin\\Debug\\net6.0\\NUnit_Engine_Tests.dll");

TestRun testRunResult = Compute.RunTests(NUnit_Engine_Tests_path);
Console.WriteLine(JsonConvert.SerializeObject(testRunResult, Formatting.Indented));

TestResult testResult = testRunResult.ToTestResult();
testResult.ShouldNotBeNull();
Console.WriteLine(JsonConvert.SerializeObject(testResult, Formatting.Indented));

testRunResult.Failed.ShouldBe(1);
}
}
}
6 changes: 6 additions & 0 deletions .ci/unit-tests/Test_Toolkit_Tests.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NUnit_Engine", "..\..\NUnit
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NUnit_Engine_UnloadableAssembliesTests", "NUnit_Engine_UnloadableAssembliesTests\NUnit_Engine_UnloadableAssembliesTests.csproj", "{63393726-FCA1-4AA1-992D-C5D800AB9B5C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NUnit_TestRunner_Tests", "NUnit_TestRunner_Tests\NUnit_TestRunner_Tests.csproj", "{2E22FED0-36B6-4488-9EA5-81C7B2C17DF4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{63393726-FCA1-4AA1-992D-C5D800AB9B5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{63393726-FCA1-4AA1-992D-C5D800AB9B5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{63393726-FCA1-4AA1-992D-C5D800AB9B5C}.Release|Any CPU.Build.0 = Release|Any CPU
{2E22FED0-36B6-4488-9EA5-81C7B2C17DF4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2E22FED0-36B6-4488-9EA5-81C7B2C17DF4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2E22FED0-36B6-4488-9EA5-81C7B2C17DF4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2E22FED0-36B6-4488-9EA5-81C7B2C17DF4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
35 changes: 35 additions & 0 deletions NUnit_Engine/Compute/RunTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
using System.Xml.Serialization;
using BH.oM.Base.Attributes;
using System.Xml;
using System.Collections;
using System.Reflection;
using System.Linq;

namespace BH.Engine.Test.NUnit
{
Expand All @@ -44,6 +47,11 @@ public static TestRun RunTests(string filePath)
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
return null;

// Load assemblies referenced by this assembly.
// Required to ensure that all dependencies (including dynamically-loaded assemblies) are present for the test run, regardless of the executing environment.
// In a normal test run in VS (not via NUnit.Engine) this is taken care of from the base BH.oM.Test.NUnit.NUnitTest class.
LoadReferencedAssemblies(filePath);

ITestEngine testEngine = TestEngineActivator.CreateInstance();
var package = new TestPackage(filePath);
var testRunner = testEngine.GetRunner(package);
Expand All @@ -60,5 +68,32 @@ public static TestRun RunTests(string filePath)
}
return result;
}

[Description("Makes sure that the assemblies referenced by the input assembly are loaded in memory." +
"Required to ensure that all dependencies (including dynamically-loaded assemblies) are present for the test run, regardless of the executing environment.")]
private static void LoadReferencedAssemblies(string dllPath)
{
var loadedAssemblies = new HashSet<string>();
var assembliesToCheck = new Queue<Assembly>();

assembliesToCheck.Enqueue(Assembly.LoadFile(dllPath));

while (assembliesToCheck.Any())
{
var assemblyToCheck = assembliesToCheck.Dequeue();

var referencedAssemblies = assemblyToCheck.GetReferencedAssemblies();

foreach (var reference in referencedAssemblies)
{
if (!loadedAssemblies.Contains(reference.FullName))
{
var assembly = Assembly.Load(reference);
assembliesToCheck.Enqueue(assembly);
loadedAssemblies.Add(reference.FullName);
}
}
}
}
}
}
3 changes: 2 additions & 1 deletion NUnit_Engine/NUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

using NUnit.Framework;
using NUnitTestStatus = NUnit.Framework.Interfaces.TestStatus;
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -33,7 +34,7 @@

namespace BH.oM.Test.NUnit
{
public class NUnitTest
public abstract class NUnitTest
{
[OneTimeSetUp]
public void LoadReferencedAssemblies()
Expand Down

0 comments on commit 09b890c

Please sign in to comment.