docs/articles/nunit-analyzers/NUnit1012.md
| Topic | Value |
|---|---|
| Id | NUnit1012 |
| Severity | Error |
| Enabled | True |
| Category | Structure |
| Code | TestMethodUsageAnalyzer |
The async test method must have a non-void return type.
To prevent tests that will fail at runtime due to improper construction.
[Test]
public async void NUnit1012SampleTest()
{
var result = await Task.FromResult(true);
Assert.That(result, Is.True);
}
async methods should generally not return void in C#. For example if an exception is thrown (as they are in the case
of an assertion violation), the exception is actually a part of the task object. If the return type is void, no such
object exists, to the exception is effectively swallowed.
Make the async test method return a Task:
[Test]
public async Task NUnit1012SampleTest()
{
var result = await Task.FromResult(true);
Assert.That(result, Is.True);
}
Or modify the test to not use async behavior:
[Test]
public void NUnit1012SampleTest()
{
var result = true;
Assert.That(result, Is.True);
}
Configure the severity per project, for more info see MSDN.
# NUnit1012: The async test method must have a non-void return type
dotnet_diagnostic.NUnit1012.severity = chosenSeverity
where chosenSeverity can be one of none, silent, suggestion, warning, or error.
#pragma warning disable NUnit1012 // The async test method must have a non-void return type
Code violating the rule here
#pragma warning restore NUnit1012 // The async test method must have a non-void return type
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit1012 // The async test method must have a non-void return type
[SuppressMessage][System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
"NUnit1012:The async test method must have a non-void return type",
Justification = "Reason...")]