Search Results for

    Show / Hide Table of Contents

    ThrowsNothing Constraint

    ThrowsNothingConstraint tests that the code under test does not throw any exception.

    Usage

    Throws.Nothing
    

    Examples

    [Test]
    public void ThrowsNothingConstraint_Examples()
    {
        // Test that method completes without throwing
        Assert.That(() => Add(1, 2), Throws.Nothing);
    
        // Verify cleanup doesn't throw
        var resource = new DisposableResource();
        Assert.That(() => resource.Dispose(), Throws.Nothing);
    }
    
    private static int Add(int a, int b) => a + b;
    
    private class DisposableResource : IDisposable
    {
        public void Dispose() { }
    }
    

    Notes

    1. Use Throws.Nothing to explicitly verify that code doesn't throw.
    2. This is different from not testing at all - it documents the expectation.
    3. If the delegate throws any exception, the constraint fails with the exception details.

    See Also

    • Throws Constraint - Test that code throws specific exceptions
    • 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