Search Results for

    Show / Hide Table of Contents

    NUnit2016

    Consider using Assert.That(expr, Is.Null) instead of ClassicAssert.Null(expr)

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

    Description

    Consider using the constraint model, Assert.That(expr, Is.Null), instead of the classic model, ClassicAssert.Null(expr).

    Motivation

    The classic Assert model contains less flexibility than the constraint model, so this analyzer marks usages of ClassicAssert.Null from the classic Assert model.

    [Test]
    public void Test()
    {
        object obj = null;
        ClassicAssert.Null(obj);
    }
    

    How to fix violations

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

    [Test]
    public void Test()
    {
        object obj = null;
        Assert.That(obj, Is.Null);
    }
    

    Configure severity

    Via ruleset file

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

    Via .editorconfig file

    # NUnit2016: Consider using Assert.That(expr, Is.Null) instead of ClassicAssert.Null(expr)
    dotnet_diagnostic.NUnit2016.severity = chosenSeverity
    

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

    Via #pragma directive

    #pragma warning disable NUnit2016 // Consider using Assert.That(expr, Is.Null) instead of ClassicAssert.Null(expr)
    Code violating the rule here
    #pragma warning restore NUnit2016 // Consider using Assert.That(expr, Is.Null) instead of ClassicAssert.Null(expr)
    

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

    #pragma warning disable NUnit2016 // Consider using Assert.That(expr, Is.Null) instead of ClassicAssert.Null(expr)
    

    Via attribute [SuppressMessage]

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
        "NUnit2016:Consider using Assert.That(expr, Is.Null) instead of ClassicAssert.Null(expr)",
        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