Edit on GitHub

xUnit2013   v2 v3   Warning

Do not use equality check to check for collection size.

Cause

A violation of this rule occurs when Assert.Equals or Assert.NotEquals are used to check if a collection has 0 or 1 elements.

Reason for rule

There are specialized assertions for checking collection sizes.

How to fix violations

Use Assert.Empty, Assert.NotEmpty, or Assert.Single instead.

Examples

Violates

using System.Linq;
using Xunit;

public class xUnit2013
{
    [Fact]
    public void TestMethod()
    {
        var result = new[] { "Hello" };

        Assert.Equal(1, result.Count());
    }
}

Does not violate

using Xunit;

public class xUnit2013
{
    [Fact]
    public void TestMethod()
    {
        var result = new[] { "Hello" };

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