Skip to content

Latest commit

 

History

History
100 lines (78 loc) · 3.27 KB

NUnit2048.md

File metadata and controls

100 lines (78 loc) · 3.27 KB

NUnit2048

Consider using Assert.That(...) instead of StringAssert(...)

Topic Value
Id NUnit2048
Severity Warning
Enabled True
Category Assertion
Code StringAssertUsageAnalyzer

Description

Consider using the constraint model, Assert.That(actual, {0}(expected)), instead of the classic model, StringAssert.{1}(expected, actual).

Motivation

The classic Assert model contains less flexibility than the constraint model and makes it easy to mix the expected and the actual parameter, so this analyzer marks usages of all StringAssert methods from the classic Assert model.

[Test]
public void Test()
{
    StringAssert.Contains(expected, actual);
    StringAssert.DoesNotContain(expected, actual);
    StringAssert.StartsWith(expected, actual);
    StringAssert.DoesNotStartWith(expected, actual);
    StringAssert.EndsWith(expected, actual);
    StringAssert.DoesNotEndWith(expected, actual);
    StringAssert.AreEqualIgnoreCase(expected, actual);
    StringAssert.AreNotEqualIgnoreCase(expected, actual);
    StringAssert.IsMatch(expected, actual);
    StringAssert.DoesNotMatch(expected, actual);
}

How to fix violations

The analyzer comes with a code fix that will replace StringAssert.<method>(expected, actual) with Assert.That(actual, <constraint>(expected)). So the code block above will be changed into.

[Test]
public void Test()
{
    Assert.That(actual, Does.Contain(expected));
    Assert.That(actual, Does.Not.Contain(expected));
    Assert.That(actual, Does.StartWith(expected));
    Assert.That(actual, Does.Not.StartWith(expected));
    Assert.That(actual, Does.EndWith(expected));
    Assert.That(actual, Does.Not.EndWith(expected));
    Assert.That(actual, Is.EqualTo(expected).IgnoreCase);
    Assert.That(actual, Is.Not.EqualTo(expected).IgnoreCase);
    Assert.That(actual, Does.Match(expected));
    Assert.That(actual, Does.Not.Match(expected));
}

Configure severity

Via ruleset file

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

Via .editorconfig file

# NUnit2048: Consider using Assert.That(...) instead of StringAssert(...)
dotnet_diagnostic.NUnit2048.severity = chosenSeverity

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

Via #pragma directive

#pragma warning disable NUnit2048 // Consider using Assert.That(...) instead of StringAssert(...)
Code violating the rule here
#pragma warning restore NUnit2048 // Consider using Assert.That(...) instead of StringAssert(...)

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

#pragma warning disable NUnit2048 // Consider using Assert.That(...) instead of StringAssert(...)

Via attribute [SuppressMessage]

[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
    "NUnit2048:Consider using Assert.That(...) instead of StringAssert(...)",
    Justification = "Reason...")]