-
Notifications
You must be signed in to change notification settings - Fork 155
NUnit1013
Mikkel Nylander Bundgaard edited this page Apr 25, 2020
·
2 revisions
Topic | Value |
---|---|
Id | NUnit1013 |
Severity | Error |
Enabled | True |
Category | Structure |
Code | TestMethodUsageAnalyzer |
Async test method must have 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.
#pragma warning disable NUnit1013 // Async test method must have non-generic Task return type when no result is expected.
Code violating the rule here
#pragma warning restore NUnit1013 // Async test method must have 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 // Async test method must have non-generic Task return type when no result is expected.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
"NUnit1013:Async test method must have non-generic Task return type when no result is expected.",
Justification = "Reason...")]
Copyright (c) 2018 The NUnit Project - Licensed under CC BY-NC-SA 4.0