Lesson 14 of 93 · 5 min read · Updated
Range and Type Assertions in xUnit: InRange, IsType and IsAssignableFrom
On this page
Some results do not have one exact correct value. A price after a calculation must stay inside a valid band, and a factory method must return an object of the right kind. xUnit covers these contracts with range assertions (InRange, NotInRange) and type assertions (IsType, IsNotType, IsAssignableFrom). This lesson shows both groups and explains when a type assertion is the wrong tool.
Technical Requirements
- .NET 11 SDK
- xunit 2.9.3 (with xunit.runner.visualstudio and Microsoft.NET.Test.Sdk)
Range Assertions Describe a Valid Domain
The test raises a monitor price by 10 percent. IncreasePriceByPercent rounds to two decimal places, so 5,000 becomes exactly 5,500.
M02.XUnitBasics.Tests/RangeAndTypeAssertionTests.cs
Assert.InRange(actual, low, high) passes when low <= actual <= high. Both bounds are inclusive, so a value equal to a bound passes. NotInRange is the opposite: here it states that the price is no longer inside the old range of 0 to 5,000, so the increase really happened.
When should you use a range instead of Assert.Equal(5_500m, product.Price)? Use Equal when the exact value is the contract, as it is for a deterministic price calculation. Use a range when the contract itself is a range: a generated discount between 5 and 15 percent, a timeout that must stay under a limit, or a measurement with known tolerance. A range that is wider than the real contract is a weak test. It passes for wrong results too.
Both assertions work with any type that implements IComparable, such as numbers, dates and TimeSpan. There is also an overload that takes an IComparer<T> for custom ordering.
The Code Under Test: A Small Type Hierarchy
The type examples use a notification hierarchy built with records, and a factory that picks the type from a channel name:
M02.XUnitBasics/Notifications.cs
The factory's return type is Notification. The runtime type depends on the channel. (The subject text means "your order is being prepared", and the error says "unknown channel".)
IsType: Exact Type Match
M02.XUnitBasics.Tests/RangeAndTypeAssertionTests.cs
Assert.IsType<T> passes only when the runtime type is exactly T. A subclass of T fails. Like Assert.Single, it returns the value already cast to T, so the next line can read email.Subject without a manual cast. When it fails, the message names both the expected and the actual type.
Here the exact type is the contract. The whole point of the "email" channel is that the caller gets an email, so an exact check is correct.
IsAssignableFrom: "Is It Some Kind of T?"
M02.XUnitBasics.Tests/RangeAndTypeAssertionTests.cs
Assert.IsAssignableFrom<T> passes when the object can be assigned to a variable of type T: the type itself, a subclass, or an implementation of an interface. Choose it when the contract is the base type. If a new PushNotification subtype appears later, this test keeps passing, which is what you want.
The name reads backwards to many people. Read it as "T is assignable from the object's type".
The Pitfall: Testing Types Instead of Behaviour
Type assertions are easy to overuse. If the runtime type is not part of the contract, a type check ties the test to an implementation detail. Say a method returns IReadOnlyList<Product>. Asserting IsType<List<Product>> breaks the day someone returns an array, even though every caller still works. Assert what the caller can observe instead: the count, the elements, the order.
A simple test for yourself: would a caller write if (x is EmailNotification)? If yes, the type is part of the contract and IsType is fine. If the caller only uses the base type, assert behaviour or use IsAssignableFrom.
The last test in the file checks the error path of the factory:
M02.XUnitBasics.Tests/RangeAndTypeAssertionTests.cs
It asserts ParamName instead of the message. The parameter name is a stable part of the contract, while the message text may change. The next lesson covers exception assertions in detail.
Summary
Assert.InRangeandNotInRangecheck that a value lies inside or outside inclusive bounds.- Use a range only when the contract is a range. Prefer
Assert.Equalfor deterministic values. Assert.IsType<T>requires the exact runtime type and returns the cast value.Assert.IsAssignableFrom<T>acceptsTand its subtypes, so it survives new subclasses.- If the runtime type is not part of the contract, assert behaviour instead of the type.