Do not use boolean asserts for simple equality tests
You should not use boolean assertions (like Assert.True
or Assert.False
) with simple equality comparisons
against literal values like null
, numeric constants, or enum values.
The error message provided when the assertion fails is less useful, since it merely indicates that you expected
a value to be true
or false
. Using a better assertion (like Assert.Equal
) will provide a better user
experience, because it will show you the actual values in question when the comparison fails.
To fix a violation of this rule, replace the boolean assertion with one more appropriate (including Assert.Equal
,
Assert.NotEqual
, Assert.Null
, or Assert.NotNull
);
using Xunit;
public class xUnit2024
{
[Fact]
public void TestMethod()
{
var x = 42;
Assert.True(x == 2112);
}
}
using Xunit;
public class xUnit2024
{
[Fact]
public void TestMethod()
{
var x = new object();
Assert.True(x != null);
}
}
using Xunit;
public class xUnit2024
{
[Fact]
public void TestMethod()
{
var x = 42;
Assert.Equal(2112, x);
}
}
using Xunit;
public class xUnit2024
{
[Fact]
public void TestMethod()
{
var x = new object();
Assert.NotNull(x);
}
}