NUnit1014
The async test method must have a Task<T> return type when a result is expected
Topic | Value |
---|---|
Id | NUnit1014 |
Severity | Error |
Enabled | True |
Category | Structure |
Code | TestMethodUsageAnalyzer |
Description
The async test method must have a Task<T>
return type when a result is expected.
Motivation
To prevent tests that will fail at runtime due to improper construction.
How to fix violations
Example Violation
[TestCase(1, ExpectedResult = true)]
public async Task NUnit1014SampleTest(int numberValue)
{
return;
}
Explanation
The NUnit ExpectedResult
syntax is used, so this method needs to return a type that matches the type of expected
result you're looking for.
Fix
Remove the ExpectedResult
syntax:
[TestCase(1)]
public async Task NUnit1014SampleTest(int numberValue)
{
Assert.Pass();
}
Or, update the return task type to be what you're looking for, e.g. Task<bool>
below:
[TestCase(1, ExpectedResult = true)]
public async Task<bool> NUnit1014SampleTest(int numberValue)
{
return Task.FromResult(true);
}
Configure severity
Via ruleset file
Configure the severity per project, for more info see MSDN.
Via .editorconfig file
# NUnit1014: The async test method must have a Task<T> return type when a result is expected
dotnet_diagnostic.NUnit1014.severity = chosenSeverity
where chosenSeverity
can be one of none
, silent
, suggestion
, warning
, or error
.
Via #pragma directive
#pragma warning disable NUnit1014 // The async test method must have a Task<T> return type when a result is expected
Code violating the rule here
#pragma warning restore NUnit1014 // The async test method must have a Task<T> return type when a result is expected
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit1014 // The async test method must have a Task<T> return type when a result is expected
Via attribute [SuppressMessage]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
"NUnit1014:The async test method must have a Task<T> return type when a result is expected",
Justification = "Reason...")]