Edit on GitHub

xUnit2023 Info

Do not use collection methods for single-item collections

Cause

A violation of this rule occurs when you use Assert.Collection to verify that a collection has a single item in it.

Reason for rule

Using Assert.Collection is designed for inspecting multiple items in a collection. When a collection is only expected to have a single item in it, using Assert.Single to get the item makes the code more concise and readable, especially when performing multiple assertions against the item.

How to fix violations

To fix a violation of this rule, replace Assert.Collection with Assert.Single and move the assertions out into the body of the unit test.

Examples

Violates

using Xunit;

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

        Assert.Collection(
          collection,
          item => Assert.Equal(1, item)
        );
    }
}

Does not violate

using Xunit;

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

        var item = Assert.Single(collection);
        Assert.Equal(1, item);
    }
}
Copyright © .NET Foundation. Contributions welcomed at https://github.com/xunit/xunit/tree/gh-pages.