Edit on GitHub

xUnit2009   v2 v3   Warning

Do not use boolean check to check for substrings

Cause

A violation of this rule occurs when Assert.True or Assert.False are used to check for substrings with string methods like string.Contains, string.StartsWith and string.EndsWith.

Reason for rule

There are specialized assertions for substring checks.

How to fix violations

To fix a violation of this rule, replace the offending assertion according to this:

Examples

Violates

using Xunit;

public class xUnit2009
{
    [Fact]
    public void TestMethod()
    {
        var result = "foo bar baz";

        Assert.True(result.Contains("bar"));
    }
}

Does not violate

using Xunit;

public class xUnit2009
{
    [Fact]
    public void TestMethod()
    {
        var result = "foo bar baz";

        Assert.Contains("bar", result);
    }
}
Copyright © .NET Foundation. Contributions welcomed at https://github.com/xunit/xunit/tree/gh-pages.