xUnit : Assert two List<T> are equal?

C#Xunit

C# Problem Overview


I'm new to TDD and xUnit so I want to test my method that looks something like:

List<T> DeleteElements<T>(this List<T> a, List<T> b);

Is there any Assert method that I can use ? I think something like this would be nice

List<int> values = new List<int>() { 1, 2, 3 };
List<int> expected = new List<int>() { 1 };
List<int> actual = values.DeleteElements(new List<int>() { 2, 3 });

Assert.Exact(expected, actual);

Is there something like this ?

C# Solutions


Solution 1 - C#

2021-Apr-01 update

I recommend using FluentAssertions. It has a vast IntelliSense-friendly assertion library for many use cases including collections

collection.Should().Equal(1, 2, 5, 8);
collection.Should().NotEqual(8, 2, 3, 5);
collection.Should().BeEquivalentTo(8, 2, 1, 5);

Original answer

xUnit.Net recognizes collections so you just need to do

Assert.Equal(expected, actual); // Order is important

You can see other available collection assertions in CollectionAsserts.cs

For NUnit library collection comparison methods are

CollectionAssert.AreEqual(IEnumerable, IEnumerable) // For sequences, order matters

and

CollectionAssert.AreEquivalent(IEnumerable, IEnumerable) // For sets, order doesn't matter

More details here: CollectionAssert

MbUnit also has collection assertions similar to NUnit: Assert.Collections.cs

Solution 2 - C#

In the current version of XUnit (1.5) you can just use

> Assert.Equal(expected, actual);

The above method will do an element by element comparison of the two lists. I'm not sure if this works for any prior version.

Solution 3 - C#

With xUnit, should you want to cherry pick properties of each element to test you can use Assert.Collection.

Assert.Collection(elements, 
  elem1 => Assert.Equal(expect1, elem1.SomeProperty),
  elem2 => { 
     Assert.Equal(expect2, elem2.SomeProperty);
     Assert.True(elem2.TrueProperty);
  });

This tests the expected count and ensures that each action is verified.

Solution 4 - C#

Recently, I was using xUnit 2.4.0 and Moq 4.10.1 packages in my asp.net core 2.2 app.

In my case I managed to get it work with two steps process:

  1. Defining an implementation of IEqualityComparer<T>

  2. Pass the comparer instance as a third parameter into Assert.True method:

    Assert.True(expected, actual, new MyEqualityComparer());

But there is another nicer way to get the same result using FluentAssertions package. It allows you to do the following:

// Assert          
expected.Should().BeEquivalentTo(actual));

Interestingly that Assert.Equal() always fails even when I ordered the elements of two lists to get them in the same order.

Solution 5 - C#

Just found the NotStrictEqual that seems to do it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPetar PetrovView Question on Stackoverflow
Solution 1 - C#Konstantin SpirinView Answer on Stackoverflow
Solution 2 - C#hwiechersView Answer on Stackoverflow
Solution 3 - C#Drew WilliamsView Answer on Stackoverflow
Solution 4 - C#Dmitry StepanovView Answer on Stackoverflow
Solution 5 - C#Toby KolkView Answer on Stackoverflow