docs/articles/nunit-analyzers/NUnit1005.md
| Topic | Value |
|---|---|
| Id | NUnit1005 |
| Severity | Error |
| Enabled | True |
| Category | Structure |
| Code | TestMethodUsageAnalyzer |
The type of the value specified via ExpectedResult must match the return type of the method. Otherwise, this will lead to an error at run-time.
To prevent tests that will fail at runtime due to improper construction.
[TestCase(1, ExpectedResult = true)]
public int NUnit1005SampleTest(int inputValue)
{
return inputValue;
}
The sample above uses NUnit's ExpectedResult syntax. It defines a result of true (a bool) but the return type of
the method is int.
Either modify the TestCase parameter:
[TestCase(1, ExpectedResult = 1)]
public int NUnit1005SampleTest(int inputValue)
{
return inputValue;
}
Or modify the return type and logic of the method:
[TestCase(1, ExpectedResult = true)]
public bool NUnit1005SampleTest(int inputValue)
{
return inputValue > 0;
}
Configure the severity per project, for more info see MSDN.
# NUnit1005: The type of the value specified via ExpectedResult must match the return type of the method
dotnet_diagnostic.NUnit1005.severity = chosenSeverity
where chosenSeverity can be one of none, silent, suggestion, warning, or error.
#pragma warning disable NUnit1005 // The type of the value specified via ExpectedResult must match the return type of the method
Code violating the rule here
#pragma warning restore NUnit1005 // The type of the value specified via ExpectedResult must match the return type of the method
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit1005 // The type of the value specified via ExpectedResult must match the return type of the method
[SuppressMessage][System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
"NUnit1005:The type of the value specified via ExpectedResult must match the return type of the method",
Justification = "Reason...")]