docs/articles/nunit-analyzers/NUnit1013.md
| Topic | Value |
|---|---|
| Id | NUnit1013 |
| Severity | Error |
| Enabled | True |
| Category | Structure |
| Code | TestMethodUsageAnalyzer |
The async test method must have a non-generic Task return type when no result is expected.
To prevent tests that will fail at runtime due to improper construction.
[TestCase(1)]
public async Task<string> NUnit1013SampleTest(int numberValue)
{
return await ConvertNumber(numberValue);
}
public Task<string> ConvertNumber(int numberValue)
{
return Task.FromResult(numberValue.ToString());
}
The NUnit ExpectedResult syntax is not used, so it's an error for this method to return something that isn't being
checked.
Utilize the ExpectedResult syntax:
[TestCase(1, ExpectedResult = "1")]
public async Task<string> NUnit1013SampleTest(int numberValue)
{
return await ConvertNumber(numberValue);
}
public Task<string> ConvertNumber(int numberValue)
{
return Task.FromResult(numberValue.ToString());
}
Or, use an assertion and a generic Task rather than Task<string>:
[TestCase(1)]
public async Task NUnit1013SampleTest(int numberValue)
{
var result = await ConvertNumber(numberValue);
Assert.That(result, Is.EqualTo("1"));
}
public Task<string> ConvertNumber(int numberValue)
{
return Task.FromResult(numberValue.ToString());
}
Configure the severity per project, for more info see MSDN.
# NUnit1013: The async test method must have a non-generic Task return type when no result is expected
dotnet_diagnostic.NUnit1013.severity = chosenSeverity
where chosenSeverity can be one of none, silent, suggestion, warning, or error.
#pragma warning disable NUnit1013 // The async test method must have a non-generic Task return type when no result is expected
Code violating the rule here
#pragma warning restore NUnit1013 // The async test method must have a non-generic Task return type when no result is expected
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit1013 // The async test method must have a non-generic Task return type when no result is expected
[SuppressMessage][System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
"NUnit1013:The async test method must have a non-generic Task return type when no result is expected",
Justification = "Reason...")]