Edit on GitHub

xUnit2017 Warning

Do not use Contains() to check if a value exists in a collection

Cause

A violation of this rule occurs when Enumerable.Contains() is used to check if a value exists in a collection.

Reason for rule

There are specialized assertions for checking for elements in collections.

How to fix violations

Use Assert.Contains or Assert.DoesNotContain instead.

Examples

Violates

[Fact]
public void ExampleMethod()
{
    IEnumerable<string> result = GetItems();

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

Does not violate

[Fact]
public void ExampleMethod()
{
    IEnumerable<string> result = GetItems();

    Assert.Contains("foo", result);
    Assert.DoesNotContain("bar", result);
}

How to suppress violations

#pragma warning disable xUnit2017 // Do not use Contains() to check if a value exists in a collection
#pragma warning restore xUnit2017 // Do not use Contains() to check if a value exists in a collection
Copyright © 2017 .NET Foundation. Contributions welcomed at https://github.com/xunit/xunit.analyzers.