Why the following xUnit
test doesn't get passed. ProgramFixture
constructor is running 3 times, but I just want to keep the same instance for all tests, so the Count
property should be: 1, 2, 3 in a sequence. Why it's giving me: 1, 1, 1 as it's instantiating a new ProgramFixture
for every InlineData
test.
Program:
namespace UnitTest
{
public sealed class Program
{
public int Count { get; set; }
public void IncrementCount()
{
++this.Count;
}
// Mandatory Main method for the entry point.
public static void Main() { }
}
}
xUnit:
using System;
using System.Diagnostics;
using Xunit;
namespace UnitTestTests
{
public sealed class ProgramFixture : IDisposable
{
private bool disposed = false;
public UnitTest.Program Program { get; }
public ProgramFixture()
{
this.Program = new();
Debug.WriteLine("################## ProgramFixture constructor runned.");
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (this.disposed)
return;
if (disposing)
{
Debug.WriteLine("################## ProgramFixture disposed.");
}
this.disposed = true;
}
}
public sealed class UnitTest1 : IClassFixture<ProgramFixture>
{
private readonly ProgramFixture programFixture;
public UnitTest1(ProgramFixture programFixture)
{
this.programFixture = programFixture;
}
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
public void Test1(int expectedCount)
{
this.programFixture.Program.IncrementCount();
Assert.Equal(expectedCount, this.programFixture.Program.Count);
}
}
}
I'm using Visual Studio Community 2019 16.10.3
Is that a bug on Visual Studio
or some machine specific bug?
EDIT
My Project files:
Program:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>
xUnit:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<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>
<ProjectReference Include="..\UnitTest\UnitTest.csproj" />
</ItemGroup>
</Project>
EDIT 2
I noticed I have inverted the Assert.Equal
method signature order for its parameters, so I edited Assert.Equal(this.programFixture.Program.Count, expectedCount)
to Assert.Equal(expectedCount, this.programFixture.Program.Count)
so in the first Error screenshot the expected and actual values shown at the right panel are different from this one, however the problem still the same.