xUnit MessageSink for Collection Fixture does not generate output

Viewed 197

I'm trying to add diagnostic output to my xUnit tests. For this, I'm using a combination of Collection Fixture and IMessageSink. This is my code:

using System;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace MessageSink
{
    [Collection("My collection")]
    public class UnitTest1
    {
        private readonly MyCollectionFixture _fixture;

        public UnitTest1(MyCollectionFixture fixture)
        {
            _fixture = fixture;
        }

        [Fact]
        public void Test1()
        {
            _fixture.Sink.OnMessage(new DiagnosticMessage("Hello World"));
        }
    }

    public class MyCollectionFixture : IDisposable
    {
        public MyCollectionFixture(IMessageSink sink)
        {
            Sink = sink;
        }

        public IMessageSink Sink { get; }

        public void Dispose()
        {
        }
    }

    [CollectionDefinition("My collection")]
    public class MyCollection : ICollectionFixture<MyCollectionFixture>
    {
    }
}

Within the test project, I've created the following xunit.runner.json:

{
  "$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
  "diagnosticMessages": true
}

And this is my project configuration:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <Nullable>enable</Nullable>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="3.0.2">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>
  <ItemGroup>
    <Content Include="xunit.runner.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
</Project>

I'd expect Hello World within the output of the test runner - but it is empty.

I (hopefully ) followed the docs in the correct way:

What am I doing wrong?

Thanks and cheers

1 Answers

You'll need to extend IClassFixture<MyCollectionFixture> to get that working for diagnostic messages.

For in-test messages, try using ITestOutputHelper which you can pass in into your test class along with MyCollectionFixture.

Check docs out: https://xunit.net/docs/capturing-output

Related