Search Results for

    Show / Hide Table of Contents

    Range Constraint

    RangeConstraint tests that a value falls within an inclusive range. Both the lower and upper bounds are included in the valid range.

    Usage

    Is.InRange(IComparable from, IComparable to)
    

    Modifiers

    .Using(IComparer comparer)
    .Using(IComparer comparer)
    .Using(Comparison comparer)
    

    Examples

    [Test]
    public void RangeConstraint_Examples()
    {
        // Numeric ranges (inclusive)
        Assert.That(5, Is.InRange(1, 10));
        Assert.That(1, Is.InRange(1, 10));   // Lower bound is included
        Assert.That(10, Is.InRange(1, 10));  // Upper bound is included
        Assert.That(0, Is.Not.InRange(1, 10));
    
        // DateTime ranges
        var start = new DateTime(2024, 1, 1);
        var end = new DateTime(2024, 12, 31);
        Assert.That(new DateTime(2024, 6, 15), Is.InRange(start, end));
    
        // String ranges (alphabetical comparison)
        Assert.That("banana", Is.InRange("apple", "cherry"));
    }
    

    Notes

    1. The range is inclusive on both ends: Is.InRange(1, 10) passes for values 1, 10, and everything in between.

    2. For exclusive bounds, combine Is.GreaterThan and Is.LessThan with And:

      Assert.That(5, Is.GreaterThan(1).And.LessThan(10));  // Exclusive bounds
      

    See Also

    • GreaterThan Constraint
    • LessThan Constraint
    • GreaterThanOrEqual Constraint
    • LessThanOrEqual Constraint
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0