Is it possible to ignore order of elements in IEnumerable
, like in Assert.That( ... , Is.EquivalentTo( ... ));
, but using TestCaseData.Returns()
in NUnit 3?
CodePudding user response:
If I understand your question correctly, you want to write test cases in NUnit to determine whether two lists are of identical length and contain the same elements, ignoring the order of the elements.
If my interpretation of your question is correct then I have included an example below which should solve your problem:
[TestFixture]
public class MyTests
{
[TestCaseSource(typeof(MyDataClass), nameof(MyDataClass.TestCases))]
public bool ListEqualTest(IEnumerable<int> list1, IEnumerable<int> list2)
{
return list1.Intersect(list2).Count() == list1.Count() && list1.Count == list2.Count;
}
}
public class MyDataClass
{
public static IEnumerable TestCases
{
get
{
var list1 = new List<int> { 1, 2, 3, 4, 5 };
var list2 = new List<int> { 3, 4, 2, 1, 5 };
var list3 = new List<int> { 6, 7, 8, 9, 10 };
var list4 = new List<int> { 6, 7, 8, 11, 12 };
yield return new TestCaseData(list1, list2).Returns(true);
yield return new TestCaseData(list3, list4).Returns(false);
}
}
}
I adapted the example provided in the NUnit documentation, found here.
My solution obviously makes use of the primitive type int
when defining the IEnumerable<int>
parameters and constructing the List<int>
objects but it won't take much effort at all to adapt it for your specific needs.
Hope this helps.