Skip to content

NUnit1007

Mikkel Nylander Bundgaard edited this page Apr 25, 2020 · 2 revisions

NUnit1007

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

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 #pragma directive.

#pragma warning disable NUnit1007 // Method has non-void return type, but no result is expected in ExpectedResult.
Code violating the rule here
#pragma warning restore NUnit1007 // 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 // Method has non-void return type, but no result is expected in ExpectedResult.

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("Structure", 
    "NUnit1007:Method has non-void return type, but no result is expected in ExpectedResult.",
    Justification = "Reason...")]
Clone this wiki locally