Search Results for

    Show / Hide Table of Contents

    NUnit1007

    The method has non-void return type, but no result is expected in ExpectedResult

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

    Description

    The method has non-void return type, but no result is expected in ExpectedResult.

    Motivation

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

    How to fix violations

    Example Violation

    [TestCase(1)]
    public string NUnit1007SampleTest(int inputValue)
    {
        return "";
    }
    

    Explanation

    No ExpectedResult was defined, but the return type of the method in our sample is of type string, meaning it does indeed return a result and we should use the ExpectedResult syntax in order to capture it.

    Fix

    Either modify the TestCase to add an ExpectedResult:

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

    Or modify the return type of the test method to be void:

    [TestCase(1)]
    public void NUnit1007SampleTest(int inputValue)
    {
        return Assert.That(inputValue, Is.EqualTo(1));
    }
    

    Configure severity

    Via ruleset file

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

    Via .editorconfig file

    # NUnit1007: The method has non-void return type, but no result is expected in ExpectedResult
    dotnet_diagnostic.NUnit1007.severity = chosenSeverity
    

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

    Via #pragma directive

    #pragma warning disable NUnit1007 // The method has non-void return type, but no result is expected in ExpectedResult
    Code violating the rule here
    #pragma warning restore NUnit1007 // The method has non-void return type, but no result is expected in ExpectedResult
    

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

    #pragma warning disable NUnit1007 // The method has non-void return type, but no result is expected in ExpectedResult
    

    Via attribute [SuppressMessage]

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
        "NUnit1007:The method has non-void return type, but no result is expected in ExpectedResult",
        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