Back to Nunit

NUnit1006

docs/articles/nunit-analyzers/NUnit1006.md

latest2.5 KB
Original Source

NUnit1006

ExpectedResult must not be specified when the method returns void

TopicValue
IdNUnit1006
SeverityError
EnabledTrue
CategoryStructure
CodeTestMethodUsageAnalyzer

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

csharp
[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:

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

Or modify the return type of the test method:

csharp
[TestCase(1, ExpectedResult = "1")]
public string NUnit1006SampleTest(int inputValue)
{
    return inputValue.ToString();
}
<!-- start generated config severity -->

Configure severity

Via ruleset file

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

Via .editorconfig file

ini
# NUnit1006: ExpectedResult must not be specified when the method returns void
dotnet_diagnostic.NUnit1006.severity = chosenSeverity

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

Via #pragma directive

csharp
#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.

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

Via attribute [SuppressMessage]

csharp
[System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
    "NUnit1006:ExpectedResult must not be specified when the method returns void",
    Justification = "Reason...")]
<!-- end generated config severity -->