nunit icon indicating copy to clipboard operation
nunit copied to clipboard

.NET 5.0 NUnitLite with link level trimming

Open NN--- opened this issue 5 years ago • 3 comments

https://docs.microsoft.com/en-us/dotnet/core/deploying/trimming-options

Given this project and program

a.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <Nullable>enable</Nullable>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
    <PublishTrimmed>true</PublishTrimmed>
    <PublishSingleFile>true</PublishSingleFile>
    <TrimMode>link</TrimMode>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="NUnitLite" Version="3.12.0" />
  </ItemGroup>
</Project>

a.cs

using System.Threading.Tasks;
using NUnit.Framework;
using NUnitLite;

public class C
{
    public static int Main()
    {
        return new AutoRun().Execute(new string[]{});
    }
}

public class T
{
    [Test]
    public Task F()
    {
        return Task.FromResult(1);
    }
}

Publish file: dotnet publish -o publish

Run: publish\a.exe

Output:


Errors, Failures and Warnings

1) Invalid : T.F
Method has non-void return value, but no result is expected

Run Settings
    Number of Test Workers: 8
    Work Directory: C:\Projects\tests\ConsoleApp12
    Internal Trace: Off

Test Run Summary
  Overall result: Failed
  Test Count: 1, Passed: 0, Failed: 1, Warnings: 0, Inconclusive: 0, Skipped: 0
    Failed Tests - Failures: 0, Errors: 0, Invalid: 1

NN--- avatar Dec 18 '20 18:12 NN---

It seems that AsyncToSyncAdapter.IsAsyncOperation does not work in this context, as we determine that the method is not async and thus enter the logic for synchronous methods (and then treat Task as yet another return value).

Context https://github.com/nunit/nunit/blob/ad49f27294bd0f2677d8699756c6ccb10df600f8/src/NUnitFramework/framework/Internal/Builders/NUnitTestCaseBuilder.cs#L181-L204

mikkelbu avatar Dec 18 '20 19:12 mikkelbu

I don't see anywhere that we document it (we should) but we expect async tests to be marked with async.

rprouse avatar Jan 24 '21 21:01 rprouse

async is asynchronous method implementation detail.

 public static bool IsAsyncOperation(MethodInfo method)
        {
            return AwaitAdapter.IsAwaitable(method.ReturnType)
                || method.GetCustomAttributes(false).Any(attr => attr.GetType().FullName == "System.Runtime.CompilerServices.AsyncStateMachineAttribute");
        }

It is either awaitable or AsyncStateMachineAttribute. Isn't Task awaitable ?

I see the actual check is here: https://github.com/nunit/nunit/blob/ad49f27294bd0f2677d8699756c6ccb10df600f8/src/NUnitFramework/framework/Internal/CSharpPatternBasedAwaitAdapter.AwaitShapeInfo.cs#L47 Perhaps something is missing in this check.

NN--- avatar Jan 25 '21 11:01 NN---