Numbers
Integers, decimal and floating-point values share their comparison, sign and zero assertions, which are defined on ComparableAssertions<TSelf, T> and NumericAssertions<TSelf, T>. Chainable methods return an AssertionsChain<TAssertions, T> whose .And is the assertions for the specific numeric type.
Comparisons
5.Should().BeLessThan(10);
5.Should().BeLessThanOrEqualTo(5);
10.Should().BeGreaterThan(5);
10.Should().BeGreaterThanOrEqualTo(10);
5.Should().BeInRange(1, 10); // Inclusive.See BeLessThan, BeLessThanOrEqualTo, BeGreaterThan, BeGreaterThanOrEqualTo and BeInRange.
Sign and Zero
42.Should().BePositive();
42.Should().NotBeNegative();
(-5).Should().BeNegative();
(-5).Should().NotBePositive();
0.Should().BeZero();
1.Should().NotBeZero();Zero is neither positive nor negative, so 0.Should().BePositive() fails and 0.Should().NotBePositive() passes. See BeZero, BePositive, BeNegative and their Not counterparts.
Integers
IntegerAssertions<T> is available for all integer types (byte, sbyte, short, ushort, int, uint, long, ulong, nint, nuint) via .Should().
int value = 42;
value.Should().Equal(42);
value.Should().NotEqual(0);Integer equality supports cross-type comparisons via IBinaryInteger<T>.CreateChecked. For example, you can compare a byte to an int:
byte b = 10;
b.Should().Equal(10); // int literal compared to byte
b.Should().NotEqual(256); // 256 can't fit in a byte → always passesIf the expected value can't be represented in the value's type (overflow), Equal throws an assertion failure with a descriptive message. NotEqual passes silently in this case.
Decimal
DecimalAssertions is available for decimal via .Should(). Because decimal is an exact type, there is no precision/approximation parameter — comparisons use exact values.
(1.5m).Should().Equal(1.5m);
(1.5m).Should().NotEqual(2.0m);
(5m).Should().BePositive().And.BeLessThan(10m);Equality uses the base Equal and NotEqual.
Floating-Point
FloatingPointAssertions<T> is available for Half, float, and double via .Should().
Approximate Equality
Floating-point comparisons should generally use BeApproximately rather than Equal:
double value = 1.0 / 3.0;
value.Should().BeApproximately(0.333, 0.001);
// Half works the same way
var halfValue = (Half)17.5;
halfValue.Should().BeApproximately((Half)17.5, (Half)0.1);Comparisons with Precision
As well as the exact comparisons above, there are comparison methods that take a precision parameter that defines a tolerance:
double value = 5.001;
value.Should().BeGreaterThan(5.0, 0.0001);
value.Should().BeLessThan(6.0, 0.0001);
value.Should().BeGreaterThanOrEqualTo(5.001, 0.001);
value.Should().BeLessThanOrEqualTo(5.001, 0.001);NaN and Signed Zero
Comparisons, sign and zero assertions follow the behaviour of the comparison operators:
NaNis unordered, so it is not less than, greater than or equal to anything, and is not zero, positive or negative.-0.0is zero, and is not negative.
double.NaN.Should().BeLessThan(1.0); // Fails.
double.NaN.Should().NotBePositive(); // Passes.
(-0.0).Should().BeZero(); // Passes.
(-0.0).Should().BeNegative(); // Fails.Special Values
Uses IFloatingPoint<T> members IsNaN, IsInfinity, IsPositiveInfinity, and IsNegativeInfinity:
double.NaN.Should().BeNaN();
(1.0).Should().NotBeNaN();
double.PositiveInfinity.Should().BeInfinity();
double.NegativeInfinity.Should().BeInfinity();
(1.0).Should().NotBeInfinity();
double.PositiveInfinity.Should().BePositiveInfinity();
double.NegativeInfinity.Should().BeNegativeInfinity();Booleans
BooleanAssertions is available for bool via .Should(). For nullable booleans (bool?), use the extension methods from BooleanExtensions.
bool value = true;
value.Should().BeTrue();
value.Should().NotBeFalse();
bool other = false;
other.Should().BeFalse();
other.Should().NotBeTrue();Nullable booleans:
bool? nullable = true;
nullable.Should().BeTrue(); // fails if null or false
nullable.Should().NotBeFalse(); // passes if null or true
bool? nullValue = null;
nullValue.Should().NotBeTrue(); // passes — null is not true
nullValue.Should().NotBeFalse(); // passes — null is not falseInteger Display Format
By default integers in assertion messages are displayed in decimal. Use With.IntegerFormat to display them in hexadecimal within a scope:
using (With.IntegerFormat(IntegerFormat.Hexadecimal))
{
((byte)255).Should().Equal(0); // message: "Value should equal 0x00 but was 0xFF."
}With.IntegerFormat returns an IDisposable that restores the previous format when disposed. See IntegerFormat for the available formats.