Skip to content

NUnit1014

Mikkel Nylander Bundgaard edited this page Apr 25, 2020 · 2 revisions

NUnit1014

Async test method must have Task return type when a result is expected

Topic Value
Id NUnit1014
Severity Error
Enabled True
Category Structure
Code TestMethodUsageAnalyzer

Description

Async test method must have Task 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 #pragma directive.

#pragma warning disable NUnit1014 // Async test method must have Task<T> return type when a result is expected
Code violating the rule here
#pragma warning restore NUnit1014 // Async test method must have 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 // Async test method must have Task<T> return type when a result is expected

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("Structure", 
    "NUnit1014:Async test method must have Task<T> return type when a result is expected",
    Justification = "Reason...")]
Clone this wiki locally