Skip to content

Lifetime problem with Xunit: InvalidOperationException: There is no currently active test. #692

@StefanBertels

Description

@StefanBertels

I get reproducable runtime error on one of two tests if I run all tests of the following test class / project. If I run just one test (e.g. manually in IDE Rider) everything looks good (no error).

Just guessing: maybe related to #657

Exception

System.InvalidOperationException: There is no currently active test.

System.InvalidOperationException
There is no currently active test.
   at Xunit.Sdk.TestOutputHelper.GuardInitialized() in /_/src/xunit.execution/Sdk/Frameworks/TestOutputHelper.cs:line 52
   at Xunit.Sdk.TestOutputHelper.QueueTestOutput(String output) in /_/src/xunit.execution/Sdk/Frameworks/TestOutputHelper.cs:line 61
   at Xunit.Sdk.TestOutputHelper.WriteLine(String message) in /_/src/xunit.execution/Sdk/Frameworks/TestOutputHelper.cs:line 109
   at TestProject1.UnitTest1.Test2() in TestProject1\UnitTest1.cs:line 18
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Here is my C# project:

UnitTest1.cs

public class UnitTest1(Xunit.Abstractions.ITestOutputHelper testOutputHelper)
{
    [FsCheck.Xunit.Property]
    public void Test1()
    {
        testOutputHelper.WriteLine($"{nameof(Test1)}");
    }

    [Xunit.Fact]
    public void Test2()
    {
        testOutputHelper.WriteLine($"{nameof(Test2)}");
    }
}

TestProject1.csproj

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <IsTestProject>true</IsTestProject>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="FsCheck.Xunit" Version="3.0.0" />
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
        <PackageReference Include="xunit" Version="2.9.3" />
        <PackageReference Include="xunit.runner.visualstudio" Version="3.0.1" />
    </ItemGroup>
</Project>

Reproducable by just running dotnet test in project folder.

No error if I downgrade to xunit 2.8.1 / FsCheck.Xunit 2.16.6.

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <IsTestProject>true</IsTestProject>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="FsCheck.Xunit" Version="2.16.6" />
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
        <PackageReference Include="xunit" Version="2.8.1" />
        <PackageReference Include="xunit.runner.visualstudio" Version="3.0.1" />
    </ItemGroup>
</Project>

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions