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