Skip to content

NUnit2004

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

NUnit2004

Consider using Assert.That(expr, Is.True) instead of Assert.True(expr).

Topic Value
Id NUnit2004
Severity Info
Enabled True
Category Assertion
Code ClassicModelAssertUsageAnalyzer

Description

Consider using the constraint model, Assert.That(expr, Is.True), instead of the classic model, Assert.True(expr).

Motivation

The classic Assert model contains less flexibility than the constraint model, so this analyzer marks usages of Assert.True from the classic Assert model.

[Test]
public void Test()
{
    Assert.True(expression);
}

How to fix violations

The analyzer comes with a code fix that will replace Assert.True(expression) with Assert.That(expression, Is.True). So the code block above will be changed into.

[Test]
public void Test()
{
    Assert.That(expression, Is.True);
}

Configure severity

Via ruleset file.

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

Via #pragma directive.

#pragma warning disable NUnit2004 // Consider using Assert.That(expr, Is.True) instead of Assert.True(expr).
Code violating the rule here
#pragma warning restore NUnit2004 // Consider using Assert.That(expr, Is.True) instead of Assert.True(expr).

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

#pragma warning disable NUnit2004 // Consider using Assert.That(expr, Is.True) instead of Assert.True(expr).

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion", 
    "NUnit2004:Consider using Assert.That(expr, Is.True) instead of Assert.True(expr).",
    Justification = "Reason...")]
Clone this wiki locally