Skip to content

🧪 [MTP] AwesomeAssertions throws different NUnit.Framework.AssertionException than expected at runtime #1348

@cbersch

Description

@cbersch

I was trying to run our existing tests which use NUnit and AwesomeAssertions with MTP. However, I got an error of the type

  NUnit.Framework.AssertionException : Expected a <NUnit.Framework.AssertionException> to be thrown, but found <NUnit.Framework.AssertionException>:
NUnit.Framework.AssertionException: Expected string to be <null>, but found "foo".
   at AwesomeAssertions.Execution.LateBoundTestFramework.Throw(String message)
[...]

The cause of this error is, that the exception we throw is from a different Assembly (the values of Exception.GetType().Assembly.m_assembly differ). We get it by scanning AppDomain.CurrentDomain.GetAssemblies(), which accesses the default AssemblyLoadContext.

The problem appears with NUnit3TestAdapter v 6.0.0-alpha.100, but not with 5.2.0.
And only, if the NUnit.Framework.FixtureLifeCycle is specified in the project itself.

I know, a lot of 'ifs', but I'm stuck looking for the cause, or what we could do on part of AwesomeAssertions to fix this.

Repro:
MyProject.csproj:

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<TargetFramework>net10.0</TargetFramework>
		<ImplicitUsings>enable</ImplicitUsings>
		<EnableNUnitRunner>true</EnableNUnitRunner>
		<OutputType>Exe</OutputType>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="AwesomeAssertions" Version="9.2.1" />
		<PackageReference Include="NUnit" Version="4.4.0" />
		<PackageReference Include="NUnit3TestAdapter" Version="6.0.0-alpha.100" />
	</ItemGroup>

	<ItemGroup>
		<AssemblyAttribute Include="NUnit.Framework.FixtureLifeCycle">
		<_Parameter1>NUnit.Framework.LifeCycle.InstancePerTestCase</_Parameter1>
		<_Parameter1_IsLiteral>true</_Parameter1_IsLiteral>
		</AssemblyAttribute>
	</ItemGroup>
</Project>

Test.cs:

using AwesomeAssertions;
using NUnit.Framework;

public sealed class ExceptionTests
{
    [Test]
    public void TestMethod_AwesomeAssertions()
    {
        Action act = () => "foo".Should().BeNull();

        act.Should().Throw<AssertionException>();
    }
}

Either commenting the AssemblyAttribute or changing the adapter version to 5.2.0 fixes the problem.

Any clue, which changes in the adapter may cause this?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions