Search Results for

    Show / Hide Table of Contents

    NUnit1005

    The type of the value specified via ExpectedResult must match the return type of the method

    Topic Value
    Id NUnit1005
    Severity Error
    Enabled True
    Category Structure
    Code TestMethodUsageAnalyzer

    Description

    The type of the value specified via ExpectedResult must match the return type of the method. Otherwise, this will lead to an error at run-time.

    Motivation

    To prevent tests that will fail at runtime due to improper construction.

    How to fix violations

    Example Violation

    [TestCase(1, ExpectedResult = true)]
    public int NUnit1005SampleTest(int inputValue)
    {
        return inputValue;
    }
    

    Explanation

    The sample above uses NUnit's ExpectedResult syntax. It defines a result of true (a bool) but the return type of the method is int.

    Fix

    Either modify the TestCase parameter:

    [TestCase(1, ExpectedResult = 1)]
    public int NUnit1005SampleTest(int inputValue)
    {
        return inputValue;
    }
    

    Or modify the return type and logic of the method:

    [TestCase(1, ExpectedResult = true)]
    public bool NUnit1005SampleTest(int inputValue)
    {
        return inputValue > 0;
    }
    

    Configure severity

    Via ruleset file

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

    Via .editorconfig file

    # NUnit1005: The type of the value specified via ExpectedResult must match the return type of the method
    dotnet_diagnostic.NUnit1005.severity = chosenSeverity
    

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

    Via #pragma directive

    #pragma warning disable NUnit1005 // The type of the value specified via ExpectedResult must match the return type of the method
    Code violating the rule here
    #pragma warning restore NUnit1005 // The type of the value specified via ExpectedResult must match the return type of the method
    

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

    #pragma warning disable NUnit1005 // The type of the value specified via ExpectedResult must match the return type of the method
    

    Via attribute [SuppressMessage]

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
        "NUnit1005:The type of the value specified via ExpectedResult must match the return type of the method",
        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