Search Results for

    Show / Hide Table of Contents

    NUnit2029

    Consider using Assert.That(actual, Is.LessThan(expected)) instead of ClassicAssert.Less(actual, expected)

    Topic Value
    Id NUnit2029
    Severity Info
    Enabled True
    Category Assertion
    Code ClassicModelAssertUsageAnalyzer

    Description

    Consider using the constraint model, Assert.That(actual, Is.LessThan(expected)), instead of the classic model, ClassicAssert.Less(actual, expected).

    Motivation

    The assert ClassicAssert.Less from the classic Assert model makes it easy to confuse the expected and the actual argument, so this analyzer marks usages of ClassicAssert.Less.

    [Test]
    public void Test()
    {
        ClassicAssert.Less(actual, expected);
    }
    

    How to fix violations

    The analyzer comes with a code fix that will replace ClassicAssert.Less(actual, expected) with Assert.That(actual, Is.LessThan(expected)). So the code block above will be changed into.

    [Test]
    public void Test()
    {
        Assert.That(actual, Is.LessThan(expected));
    }
    

    Configure severity

    Via ruleset file

    Configure the severity per project, for more info see MSDN.

    Via .editorconfig file

    # NUnit2029: Consider using Assert.That(actual, Is.LessThan(expected)) instead of ClassicAssert.Less(actual, expected)
    dotnet_diagnostic.NUnit2029.severity = chosenSeverity
    

    where chosenSeverity can be one of none, silent, suggestion, warning, or error.

    Via #pragma directive

    #pragma warning disable NUnit2029 // Consider using Assert.That(actual, Is.LessThan(expected)) instead of ClassicAssert.Less(actual, expected)
    Code violating the rule here
    #pragma warning restore NUnit2029 // Consider using Assert.That(actual, Is.LessThan(expected)) instead of ClassicAssert.Less(actual, expected)
    

    Or put this at the top of the file to disable all instances.

    #pragma warning disable NUnit2029 // Consider using Assert.That(actual, Is.LessThan(expected)) instead of ClassicAssert.Less(actual, expected)
    

    Via attribute [SuppressMessage]

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
        "NUnit2029:Consider using Assert.That(actual, Is.LessThan(expected)) instead of ClassicAssert.Less(actual, expected)",
        Justification = "Reason...")]
    
    • 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