Skip to content

NUnit2014

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

NUnit2014

Use SomeItemsConstraint.

Topic Value
Id NUnit2014
Severity Info
Enabled True
Category Assertion
Code SomeItemsConstraintUsageAnalyzer

Description

Using SomeItemsConstraint will lead to better assertion messages in case of failure.

Motivation

Using Does.Contain (or Does.Not.Contain) constraint will lead to better assertion messages in case of failure, so this analyzer marks all usages of string Contains method where it is possible to replace with Does.Contain constraint.

[Test]
public void Test()
{
    var actual = new List<int> {1,2,3};
    int expected = 1;
    Assert.True(actual.Contains(expected));
}

How to fix violations

The analyzer comes with a code fix that will replace Assert.True(actual.Contains(expected)) with Assert.That(actual, Does.Contain(expected)). So the code block above will be changed into

[Test]
public void Test()
{
    var actual = new List<int> {1,2,3};
    int expected = 1;
    Assert.That(actual, Does.Contain(expected));
}

Configure severity

Via ruleset file.

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

Via #pragma directive.

#pragma warning disable NUnit2014 // Use SomeItemsConstraint.
Code violating the rule here
#pragma warning restore NUnit2014 // Use SomeItemsConstraint.

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

#pragma warning disable NUnit2014 // Use SomeItemsConstraint.

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion", 
    "NUnit2014:Use SomeItemsConstraint.",
    Justification = "Reason...")]
Clone this wiki locally