Skip to content

NUnit1006

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

NUnit1006

ExpectedResult must not be specified when the method returns void.

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

Description

ExpectedResult must not be specified when the method returns void. 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 = "1")]
public void NUnit1006SampleTest(int inputValue)
{
    return;
}

Explanation

An ExpectedResult was defined, but the return type of the method in our sample is of type void, meaning it does not return a result.

Fix

Either modify the TestCase to remove the ExpectedResult:

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

Or modify the return type of the test method:

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

Configure severity

Via ruleset file.

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

Via #pragma directive.

#pragma warning disable NUnit1006 // ExpectedResult must not be specified when the method returns void.
Code violating the rule here
#pragma warning restore NUnit1006 // ExpectedResult must not be specified when the method returns void.

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

#pragma warning disable NUnit1006 // ExpectedResult must not be specified when the method returns void.

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("Structure", 
    "NUnit1006:ExpectedResult must not be specified when the method returns void.",
    Justification = "Reason...")]
Clone this wiki locally