Edit on GitHub

xUnit2008 Warning

Do not use boolean check to match on regular expressions

Cause

A violation of this rule occurs when Assert.True or Assert.False are used to check for regular expression matches.

Reason for rule

There are specialized assertions for regular expression matching that give more detailed information upon failure.

How to fix violations

Replace the assertions with Assert.Matches or Assert.DoesNotMatch.

Examples

Violates

using System.Text.RegularExpressions;
using Xunit;

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

        Assert.True(Regex.IsMatch(result, "foo (.*?) baz"));
    }
}

Does not violate

using Xunit;

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

        Assert.Matches("foo (.*?) baz", result);
    }
}
Copyright © .NET Foundation. Contributions welcomed at https://github.com/xunit/xunit/tree/gh-pages.