Search Results for

    Show / Hide Table of Contents

    Assumptions

    Assumptions are intended to express the state a test must be in to provide a meaningful result. They are functionally similar to assertions, however an unmet assumption will produce an Inconclusive test result, as opposed to a Failure.

    Assumptions make use of the Assume static class.

    Syntax Example

    [TestCase(5)]
    [TestCase(0)]
    [TestCase(-5)]
    public void Number_Divided_By_Itself_Is1(int numberToCheck)
    {
        var divisor = GetMatchingDivisor(numberToCheck);
    
        Assume.That(divisor, Is.Not.EqualTo(0), () => "divisor must not be zero in order for this test to be valid");
    
        var result = numberToCheck / divisor;
        Assert.That(result, Is.EqualTo(1));
    }
    
    /// <summary>
    /// Returns same number that's passed in
    /// </summary>
    private int GetMatchingDivisor(int inputNumber)
    {
        // If this ever returned 0, we'd be in trouble
        return inputNumber;
    }
    

    Assume.That() has the same set of overloads as Assert.That(). For further details, see the Constraint Model documentation.

    Note

    Failing assumptions indicate that running tests is invalid, while Multiple Asserts allow testing to continue after a failure. For that reason, the two features are incompatible and assumptions may not be used within a multiple assert block.

    • 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