docs/articles/nunit-analyzers/NUnit2043.md
| Topic | Value |
|---|---|
| Id | NUnit2043 |
| Severity | Info |
| Enabled | True |
| Category | Assertion |
| Code | ComparisonConstraintUsageAnalyzer |
Using ComparisonConstraint will lead to better assertion messages in case of failure.
Using Is.GreaterThan constraint will lead to better assertion messages in case of failure,
so this analyzer marks all usages of a comparison operators >, >=, < and <=
where it is possible to replace with the appropriate comparison constraint.
[Test]
public void Test()
{
ClassicAssert.True(actual > expected);
}
The analyzer comes with a code fix that will replace ClassicAssert.True(actual > expected) with
Assert.That(actual, Is.GreaterThan(expected)). So the code block above will be changed into
[Test]
public void Test()
{
Assert.That(actual, Is.GreaterThan(expected));
}
Configure the severity per project, for more info see MSDN.
# NUnit2043: Use ComparisonConstraint for better assertion messages in case of failure
dotnet_diagnostic.NUnit2043.severity = chosenSeverity
where chosenSeverity can be one of none, silent, suggestion, warning, or error.
#pragma warning disable NUnit2043 // Use ComparisonConstraint for better assertion messages in case of failure
Code violating the rule here
#pragma warning restore NUnit2043 // Use ComparisonConstraint for better assertion messages in case of failure
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit2043 // Use ComparisonConstraint for better assertion messages in case of failure
[SuppressMessage][System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
"NUnit2043:Use ComparisonConstraint for better assertion messages in case of failure",
Justification = "Reason...")]