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 .editorconfig file
# 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
#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...")]