Search Results for

    Show / Hide Table of Contents

    Constraints

    This page lists all the constraints available in NUnit.

    • Alphabetical List of Constraints
    • Constraints by Category

    For additional topics, see:

    • Async Constraint Support - Using constraints with async code
    • Contains Helper - The Contains.* syntax helper
    • Tolerances - Approximate comparisons with the Within modifier

    Alphabetical List of Constraints

    Constraint Name Example
    AllItemsConstraint Assert.That(numbers, Is.All.GreaterThan(0));
    AndConstraint Assert.That(value, Is.GreaterThan(0).And.LessThan(100));
    AnyOfConstraint Assert.That(42, Is.AnyOf(0, -1, 42, 100));
    AssignableFromConstraint Assert.That("Hello", Is.AssignableFrom<string>());
    AssignableToConstraint Assert.That("Hello", Is.AssignableTo<object>());
    AttributeConstraint Assert.That(typeof(MyFixture), Has.Attribute<CategoryAttribute>().Property("Name").EqualTo("Database"));
    AttributeExistsConstraint Assert.That(typeof(MyClass), Has.Attribute<SerializableAttribute>());
    BinarySerializableConstraint Assert.That(someObject, Is.BinarySerializable);
    CollectionEquivalentConstraint Assert.That(new[] { 3, 2, 1 }, Is.EquivalentTo(new[] { 1, 2, 3 }));
    CollectionOrderedConstraint Assert.That(new[] { 1, 2, 3 }, Is.Ordered);
    CollectionSubsetConstraint Assert.That(new[] { 1, 3 }, Is.SubsetOf(new[] { 1, 2, 3 }));
    CollectionSupersetConstraint Assert.That(new[] { 1, 2, 3 }, Is.SupersetOf(new[] { 1, 3 }));
    ContainsConstraint Assert.That("Hello World", Does.Contain("World"));
    DefaultConstraint Assert.That(default(DateTime), Is.Default);
    DelayedConstraint Assert.That(() => flag, Is.True.After(500).MilliSeconds.PollEvery(50).MilliSeconds);
    DictionaryContainsKeyConstraint Assert.That(dictionary, Does.ContainKey("Alice"));
    DictionaryContainsKeyValuePairConstraint Assert.That(dictionary, Does.ContainKey("Hello").WithValue("World"));
    DictionaryContainsValueConstraint Assert.That(dictionary, Does.ContainValue(85));
    EmptyCollectionConstraint Assert.That(new List<string>(), Is.Empty);
    EmptyConstraint Assert.That(value, Is.Empty);
    EmptyDirectoryConstraint Assert.That(new DirectoryInfo(path), Is.Empty);
    EmptyStringConstraint Assert.That("", Is.Empty);
    EndsWithConstraint Assert.That("Hello World!", Does.EndWith("World!"));
    EqualConstraint Assert.That(2 + 2, Is.EqualTo(4));
    ExactCountConstraint Assert.That(numbers, Has.Exactly(2).GreaterThan(3));
    ExactTypeConstraint Assert.That("Hello", Is.TypeOf<string>());
    FalseConstraint Assert.That(2 + 2 == 5, Is.False);
    FileOrDirectoryExistsConstraint Assert.That(new FileInfo(path), Does.Exist);
    GreaterThanConstraint Assert.That(7, Is.GreaterThan(3));
    GreaterThanOrEqualConstraint Assert.That(7, Is.GreaterThanOrEqualTo(7));
    InstanceOfTypeConstraint Assert.That("Hello", Is.InstanceOf<object>());
    LessThanConstraint Assert.That(3, Is.LessThan(7));
    LessThanOrEqualConstraint Assert.That(3, Is.LessThanOrEqualTo(7));
    MultipleOfConstraint Assert.That(12, Is.MultipleOf(3));
    NaNConstraint Assert.That(0.0 / 0.0, Is.NaN);
    NoItemConstraint Assert.That(numbers, Has.None.LessThan(0));
    NotConstraint Assert.That(42, Is.Not.EqualTo(0));
    NullConstraint Assert.That(obj, Is.Null);
    OrConstraint Assert.That(value, Is.LessThan(10).Or.GreaterThan(40));
    PropertyConstraint Assert.That(person, Has.Property("Age").GreaterThan(18));
    PropertyExistsConstraint Assert.That("Hello", Has.Property("Length"));
    RangeConstraint Assert.That(5, Is.InRange(1, 10));
    RegexConstraint Assert.That("555-123-4567", Does.Match(@"^\d{3}-\d{3}-\d{4}$"));
    ReusableConstraint ReusableConstraint myConstraint = Is.Not.Null;
    SameAsConstraint Assert.That(obj2, Is.SameAs(obj1));
    SamePathConstraint Assert.That("/folder1/./junk/../folder2", Is.SamePath("/folder1/folder2"));
    SamePathOrUnderConstraint Assert.That("/folder1/folder2/folder3", Is.SamePathOrUnder("/folder1/folder2"));
    SomeItemsConstraint Assert.That(numbers, Has.Some.GreaterThan(3));
    StartsWithConstraint Assert.That("Hello World!", Does.StartWith("Hello"));
    SubPathConstraint Assert.That("/folder1/folder2", Is.SubPathOf("/folder1/"));
    SubstringConstraint Assert.That("Hello World!", Does.Contain("lo Wor"));
    ThrowsConstraint Assert.That(() => int.Parse("abc"), Throws.TypeOf<FormatException>());
    ThrowsNothingConstraint Assert.That(() => Add(1, 2), Throws.Nothing);
    TrueConstraint Assert.That(2 + 2 == 4, Is.True);
    UniqueItemsConstraint Assert.That(new[] { 1, 2, 3 }, Is.Unique);
    WhiteSpaceConstraint Assert.That(" ", Is.WhiteSpace);
    XmlSerializableConstraint Assert.That(someObject, Is.XmlSerializable);

    Constraints by Category

    Collection Constraints

    Constraint Name Example
    AllItemsConstraint Assert.That(numbers, Is.All.GreaterThan(0));
    AnyOfConstraint Assert.That(42, Is.AnyOf(0, -1, 42, 100));
    CollectionEquivalentConstraint Assert.That(new[] { 3, 2, 1 }, Is.EquivalentTo(new[] { 1, 2, 3 }));
    CollectionOrderedConstraint Assert.That(new[] { 1, 2, 3 }, Is.Ordered);
    CollectionSubsetConstraint Assert.That(new[] { 1, 3 }, Is.SubsetOf(new[] { 1, 2, 3 }));
    CollectionSupersetConstraint Assert.That(new[] { 1, 2, 3 }, Is.SupersetOf(new[] { 1, 3 }));
    DictionaryContainsKeyConstraint Assert.That(dictionary, Does.ContainKey("Alice"));
    DictionaryContainsKeyValuePairConstraint Assert.That(dictionary, Does.ContainKey("Hello").WithValue("World"));
    DictionaryContainsValueConstraint Assert.That(dictionary, Does.ContainValue(85));
    EmptyCollectionConstraint Assert.That(new List<string>(), Is.Empty);
    ExactCountConstraint Assert.That(numbers, Has.Exactly(2).GreaterThan(3));
    NoItemConstraint Assert.That(numbers, Has.None.LessThan(0));
    SomeItemsConstraint Assert.That(numbers, Has.Some.GreaterThan(3));
    UniqueItemsConstraint Assert.That(new[] { 1, 2, 3 }, Is.Unique);

    Comparison Constraints

    Constraint Name Example
    GreaterThanConstraint Assert.That(7, Is.GreaterThan(3));
    GreaterThanOrEqualConstraint Assert.That(7, Is.GreaterThanOrEqualTo(7));
    LessThanConstraint Assert.That(3, Is.LessThan(7));
    LessThanOrEqualConstraint Assert.That(3, Is.LessThanOrEqualTo(7));
    MultipleOfConstraint Assert.That(12, Is.MultipleOf(3));
    RangeConstraint Assert.That(5, Is.InRange(1, 10));

    Compound Constraints

    Constraint Name Example
    AndConstraint Assert.That(value, Is.GreaterThan(0).And.LessThan(100));
    NotConstraint Assert.That(42, Is.Not.EqualTo(0));
    OrConstraint Assert.That(value, Is.LessThan(10).Or.GreaterThan(40));

    Condition Constraints

    Constraint Name Example
    DefaultConstraint Assert.That(default(DateTime), Is.Default);
    EmptyConstraint Assert.That(value, Is.Empty);
    FalseConstraint Assert.That(2 + 2 == 5, Is.False);
    NaNConstraint Assert.That(0.0 / 0.0, Is.NaN);
    NullConstraint Assert.That(obj, Is.Null);
    TrueConstraint Assert.That(2 + 2 == 4, Is.True);
    WhiteSpaceConstraint Assert.That(" ", Is.WhiteSpace);

    File and Directory Constraints

    Constraint Name Example
    EmptyDirectoryConstraint Assert.That(new DirectoryInfo(path), Is.Empty);
    FileOrDirectoryExistsConstraint Assert.That(new FileInfo(path), Does.Exist);
    SamePathConstraint Assert.That("/folder1/./junk/../folder2", Is.SamePath("/folder1/folder2"));
    SamePathOrUnderConstraint Assert.That("/folder1/folder2/folder3", Is.SamePathOrUnder("/folder1/folder2"));
    SubPathConstraint Assert.That("/folder1/folder2", Is.SubPathOf("/folder1/"));

    String Constraints

    Constraint Name Example
    ContainsConstraint Assert.That("Hello World", Does.Contain("World"));
    EmptyStringConstraint Assert.That("", Is.Empty);
    EndsWithConstraint Assert.That("Hello World!", Does.EndWith("World!"));
    RegexConstraint Assert.That("555-123-4567", Does.Match(@"^\d{3}-\d{3}-\d{4}$"));
    StartsWithConstraint Assert.That("Hello World!", Does.StartWith("Hello"));
    SubstringConstraint Assert.That("Hello World!", Does.Contain("lo Wor"));

    Type Constraints

    Constraint Name Example
    AssignableFromConstraint Assert.That("Hello", Is.AssignableFrom<string>());
    AssignableToConstraint Assert.That("Hello", Is.AssignableTo<object>());
    ExactTypeConstraint Assert.That("Hello", Is.TypeOf<string>());
    InstanceOfTypeConstraint Assert.That("Hello", Is.InstanceOf<object>());

    See also

    • Constraint Model
    • 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