Back to Nunit

NUnit4001

docs/articles/nunit-analyzers/NUnit4001.md

latest1.9 KB
Original Source

NUnit4001

Simplify the Values attribute

TopicValue
IdNUnit4001
SeverityInfo
EnabledTrue
CategoryStyle
CodeSimplifyValuesAnalyzer

Description

Consider removing unnecessary parameters from the ValuesAttribute.

Motivation

When used without any arguments, the [Values] attribute on a (nullable) boolean or an (nullable) enum parameter will automatically include all possible values.

Therefore the Values attribute like

csharp
[Test]
public void MyBoolTest([Values(true, false)] bool value) { /* ... */ } 

can be simplified to

csharp
[Test]
public void MyBoolTest([Values] bool value) { /* ... */ } 

How to fix violations

Remove all arguments of the Values attribute.

<!-- start generated config severity -->

Configure severity

Via ruleset file

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

Via .editorconfig file

ini
# NUnit4001: Simplify the Values attribute
dotnet_diagnostic.NUnit4001.severity = chosenSeverity

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

Via #pragma directive

csharp
#pragma warning disable NUnit4001 // Simplify the Values attribute
Code violating the rule here
#pragma warning restore NUnit4001 // Simplify the Values attribute

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

csharp
#pragma warning disable NUnit4001 // Simplify the Values attribute

Via attribute [SuppressMessage]

csharp
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style",
    "NUnit4001:Simplify the Values attribute",
    Justification = "Reason...")]
<!-- end generated config severity -->