MSTEST0026: Avoid conditional access in assertions

Property Value
Rule ID MSTEST0026
Title Avoid conditional access in assertions
Category Usage
Fix is breaking or non-breaking Non-breaking
Enabled by default Yes
Default severity Info
Introduced in version 3.5.0
Is there a code fix No

Cause

This rule raises a diagnostic when an argument containing a null conditional operator (?.) or ?[] is passed to the assertion methods below:

  • Assert.IsTrue
  • Assert.IsFalse
  • Assert.AreEqual
  • Assert.AreNotEqual
  • Assert.AreSame
  • Assert.AreNotSame
  • CollectionAssert.AreEqual
  • CollectionAssert.AreNotEqual
  • CollectionAssert.AreEquivalent
  • CollectionAssert.AreNotEquivalent
  • CollectionAssert.Contains
  • CollectionAssert.DoesNotContain
  • CollectionAssert.AllItemsAreNotNull
  • CollectionAssert.AllItemsAreUnique
  • CollectionAssert.AllItemsAreInstancesOfType
  • CollectionAssert.IsSubsetOf
  • CollectionAssert.IsNotSubsetOf
  • StringAssert.Contains
  • StringAssert.StartsWith
  • StringAssert.EndsWith
  • StringAssert.Matches
  • StringAssert.DoesNotMatch

Rule description

The purpose of assertions in unit tests is to verify that certain conditions are met. When a conditional access operator is used in an assertion, it introduces an additional condition that may or may not be met, depending on the state of the object being accessed. This can lead to inconsistent test results and make test less clear.

How to fix violations

Ensure that arguments do not contain (?.) or ?[] when passed to the assertion methods. Instead, perform null checks before making the assertion.

Company? company = GetCompany();
Assert.AreEqual("Contoso", company?.Name); // MSTEST0026
StringAssert.Contains(company?.Address, "Brazil"); // MSTEST0026

// Fixed code
Assert.IsNotNull(company);
Assert.AreEqual("Contoso", company.Name);
StringAssert.Contains(company.Address, "Brazil");

When to suppress warnings

We do not recommend suppressing warnings from this rule.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable MSTEST0026
// The code that's violating the rule is on this line.
#pragma warning restore MSTEST0026

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.MSTEST0026.severity = none

For more information, see How to suppress code analysis warnings.