Search Results for

    Show / Hide Table of Contents

    NUnit2046

    Use CollectionConstraint for better assertion messages in case of failure

    Topic Value
    Id NUnit2046
    Severity Info
    Enabled True
    Category Assertion
    Code UseCollectionConstraintAnalyzer

    Description

    Use Has.Length/Has.Count/Is.Empty instead of testing property directly.

    Motivation

    Consider the difference in error message between the following pairs of Asserts:

    int[] array = { 1, 2 };
    Assert.That(array.Length, Is.EqualTo(1));
    Assert.That(array, Has.Length.EqualTo(1));
    

    The first gives: Expected: 1, But was: 2, the second: Expected: property Length equal to 1, But was: 2 making it clear we talking about number of elements, not just a number.

    The next example:

    int[] array = { 1, 2 };
    Assert.That(array.Length, Is.EqualTo(0));
    Assert.That(array, Is.Empty);
    

    This results in Expected: 0, But was: 2 in the first case and Expected: <empty>, But was: < 1, 2 > in the second.

    int[] array = Array.Empty<int>();
    Assert.That(array.Length, Is.GreaterThanOrEqualTo(1));
    Assert.That(array, Is.Not.Empty);
    

    This would change: Expected: greater than or equal to 1, But was: 0 into Expected: not <empty>, But was: <empty>

    How to fix violations

    Replace test on explicit .Length or .Count properties with Has.Length and Has.Count respectively. If testing against the value 0 use Is.Empty or Is.Not.Empty instead.

    Configure severity

    Via ruleset file

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

    Via .editorconfig file

    # NUnit2046: Use CollectionConstraint for better assertion messages in case of failure
    dotnet_diagnostic.NUnit2046.severity = chosenSeverity
    

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

    Via #pragma directive

    #pragma warning disable NUnit2046 // Use CollectionConstraint for better assertion messages in case of failure
    Code violating the rule here
    #pragma warning restore NUnit2046 // Use CollectionConstraint for better assertion messages in case of failure
    

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

    #pragma warning disable NUnit2046 // Use CollectionConstraint for better assertion messages in case of failure
    

    Via attribute [SuppressMessage]

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
        "NUnit2046:Use CollectionConstraint for better assertion messages in case of failure",
        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