Table of Contents
  v2 v3   Warning

xUnit2011

"Do not use empty collection check"

Cause

A violation of this rule occurs when Assert.Collection or Assert.CollectionAsync is used without element inspectors to check for an empty collection.

Reason for rule

There are specialized assertions for checking collection sizes.

How to fix violations

To fix a violation of this rule, you can:

  • Use Assert.Empty instead.
  • Add element inspectors to the Assert.Collection/Assert.CollectionAsync call.

Examples

Violates

using Xunit;

public class xUnit2011
{
    [Fact]
    public void TestMethod()
    {
        var result = new[] { 1 };

        Assert.Collection(result);
    }
}

Does not violate

using Xunit;

public class xUnit2011
{
    [Fact]
    public void TestMethod()
    {
        var result = new[] { 1 };

        Assert.Empty(result);
    }
}
using Xunit;

public class xUnit2011
{
    [Fact]
    public void TestMethod()
    {
        var result = new[] { 1 };

        Assert.Collection(
            result,
            value => Assert.Equal(1, value)
        );
    }
}