-
Notifications
You must be signed in to change notification settings - Fork 153
NUnit2011
Mikkel Nylander Bundgaard edited this page Apr 25, 2020
·
2 revisions
Topic | Value |
---|---|
Id | NUnit2011 |
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.Contain
(or Does.Not.Contain
) constraint will lead to better assertion messages in case of failure,
so this analyzer marks all usages of string Contains
method where it is possible to replace
with Does.Contain
constraint.
[Test]
public void Test()
{
string actual = "...";
string expected = "...";
Assert.True(actual.Contains(expected));
}
The analyzer comes with a code fix that will replace Assert.True(actual.Contains(expected))
with
Assert.That(actual, Does.Contain(expected))
. So the code block above will be changed into
[Test]
public void Test()
{
string actual = "...";
string expected = "...";
Assert.That(actual, Does.Contain(expected));
}
Configure the severity per project, for more info see MSDN.
#pragma warning disable NUnit2011 // Use ContainsConstraint.
Code violating the rule here
#pragma warning restore NUnit2011 // Use ContainsConstraint.
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit2011 // Use ContainsConstraint.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
"NUnit2011:Use ContainsConstraint.",
Justification = "Reason...")]
Copyright (c) 2018 The NUnit Project - Licensed under CC BY-NC-SA 4.0