How should I assert that two collections contain the same elements id order does NOT matter?
This means that the number of each element in the two collections are the same. Here are some examples:
Equal:
1,2,3,4 == 1,2,3,4
1,2,3,4 == 4,2,3,1
2,1,2 == 2,2,1
1,2,2 == 2,2,1
Not Equal:
1 != 1,1
1,1,2 != 1,2,2
Is there some canned function that will do what I want? I assume this would be in Microsoft.VisualStudio.QualityTools.UnitTestFramework.Assert or in LINQ. Assert would be preferable, since it would presumably give more information about how they are different.
Using Counter() , we usually are able to get frequency of each element in list, checking for it, for both the list, we can check if two lists are identical or not.
You can compare two array lists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it returns true and if not it returns false.
Using JUnit We can use the logic below to compare the equality of two lists using the assertTrue and assertFalse methods. In this first test, the size of both lists is compared before we check if the elements in both lists are the same. As both of these conditions return true, our test will pass.
The equals() method of List interface compares the specified object with this collection for equality. It returns a Boolean value true if both the lists have same elements and are of the same size.
You can use CollectionAssert.AreEquivalent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With