Skip to content

NUnit1013

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

NUnit1013

Async test method must have non-generic Task return type when no result is expected.

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

Description

Async test method must have non-generic Task return type when no result is expected.

Motivation

To prevent tests that will fail at runtime due to improper construction.

How to fix violations

Example Violation

[TestCase(1)]
public async Task<string> Nunit1013SampleTest(int numberValue)
{

    return await ConvertNumber(numberValue);
}

public Task<string> ConvertNumber(int numberValue)
{
    return Task.FromResult(numberValue.ToString());
}

Explanation

The NUnit ExpectedResult syntax is not used, so it's an error for this method to return something that isn't being checked.

Fix

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 severity

Via ruleset file.

Configure the severity per project, for more info see MSDN.

Via #pragma directive.

#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.

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("Structure", 
    "NUnit1013:Async test method must have non-generic Task return type when no result is expected.",
    Justification = "Reason...")]
Clone this wiki locally