Back to Nunit

NUnit1009

docs/articles/nunit-analyzers/NUnit1009.md

latest2.7 KB
Original Source

NUnit1009

One may not specify ParallelScope.Children on a non-parameterized test method

TopicValue
IdNUnit1009
SeverityError
EnabledTrue
CategoryStructure
CodeParallelizableUsageAnalyzer

Description

One may not specify ParallelScope.Children on a non-parameterized test method.

Motivation

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

How to fix violations

Example Violation

csharp
[Parallelizable(ParallelScope.Children)]
[Test]
public void NUnit1009SampleTest()
{
    Assert.Pass();
}

Explanation

In the sample above, the Parallelizable attribute is used with ParallelScope.Children.

However, in a non-parameterized test, such as a [Test] and not a [TestCase], there will be no children generated, and thus this type of parallelization does not make sense.

Fix

Remove the attribute:

csharp
[Test]
public void NUnit1009SampleTest()
{
    Assert.Pass();
}

Or, turn the test into one that will have children generated, such as a TestCase:

csharp
[Parallelizable(ParallelScope.Children)] // These will now run in parallel
[TestCase(1)]
[TestCase(2)]
public void NUnit1009SampleTest(int numberValue)
{
    Assert.That(numberValue, Is.GreaterThan(0));
}
<!-- start generated config severity -->

Configure severity

Via ruleset file

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

Via .editorconfig file

ini
# NUnit1009: One may not specify ParallelScope.Children on a non-parameterized test method
dotnet_diagnostic.NUnit1009.severity = chosenSeverity

where chosenSeverity can be one of none, silent, suggestion, warning, or error.

Via #pragma directive

csharp
#pragma warning disable NUnit1009 // One may not specify ParallelScope.Children on a non-parameterized test method
Code violating the rule here
#pragma warning restore NUnit1009 // One may not specify ParallelScope.Children on a non-parameterized test method

Or put this at the top of the file to disable all instances.

csharp
#pragma warning disable NUnit1009 // One may not specify ParallelScope.Children on a non-parameterized test method

Via attribute [SuppressMessage]

csharp
[System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
    "NUnit1009:One may not specify ParallelScope.Children on a non-parameterized test method",
    Justification = "Reason...")]
<!-- end generated config severity -->