NUnit2028
Consider using Assert.That(actual, Is.GreaterThanOrEqualTo(expected)) instead of ClassicAssert.GreaterOrEqual(actual, expected)
Topic | Value |
---|---|
Id | NUnit2028 |
Severity | Info |
Enabled | True |
Category | Assertion |
Code | ClassicModelAssertUsageAnalyzer |
Description
Consider using the constraint model, Assert.That(actual, Is.GreaterThanOrEqualTo(expected))
, instead of the classic
model, ClassicAssert.GreaterOrEqual(actual, expected)
.
Motivation
The assert ClassicAssert.GreaterOrEqual
from the classic Assert model makes it easy to confuse the expected
and the
actual
argument, so this analyzer marks usages of ClassicAssert.GreaterOrEqual
.
[Test]
public void Test()
{
ClassicAssert.GreaterOrEqual(actual, expected);
}
How to fix violations
The analyzer comes with a code fix that will replace ClassicAssert.GreaterOrEqual(actual, expected)
with
Assert.That(actual, Is.GreaterThanOrEqualTo(expected))
. So the code block above will be changed into.
[Test]
public void Test()
{
Assert.That(actual, Is.GreaterThanOrEqualTo(expected));
}
Configure severity
Via ruleset file
Configure the severity per project, for more info see MSDN.
Via .editorconfig file
# NUnit2028: Consider using Assert.That(actual, Is.GreaterThanOrEqualTo(expected)) instead of ClassicAssert.GreaterOrEqual(actual, expected)
dotnet_diagnostic.NUnit2028.severity = chosenSeverity
where chosenSeverity
can be one of none
, silent
, suggestion
, warning
, or error
.
Via #pragma directive
#pragma warning disable NUnit2028 // Consider using Assert.That(actual, Is.GreaterThanOrEqualTo(expected)) instead of ClassicAssert.GreaterOrEqual(actual, expected)
Code violating the rule here
#pragma warning restore NUnit2028 // Consider using Assert.That(actual, Is.GreaterThanOrEqualTo(expected)) instead of ClassicAssert.GreaterOrEqual(actual, expected)
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit2028 // Consider using Assert.That(actual, Is.GreaterThanOrEqualTo(expected)) instead of ClassicAssert.GreaterOrEqual(actual, expected)
Via attribute [SuppressMessage]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
"NUnit2028:Consider using Assert.That(actual, Is.GreaterThanOrEqualTo(expected)) instead of ClassicAssert.GreaterOrEqual(actual, expected)",
Justification = "Reason...")]