Assert that arrays are equal in Visual Studio 2008 test framework

Anteru picture Anteru · May 22, 2009 · Viewed 33.4k times · Source

Is there an easy way to check in a unit test that two arrays are equal (that is, have the same number of elements, and each element is the same?).

In Java, I would use assertArrayEquals (foo, bar);, but there seems to be no equivalent for C#. I tried Assert.AreEqual(new string[]{"a", "b"}, MyFunc("ab"));, but even though the function returns an array with "a", "b" the check still fails

This is using Visual Studio 2008 Team Suite, with the built-in unit test framework.

Answer

Anteru picture Anteru · May 22, 2009

It's CollectionAssert.AreEqual, see also the documentation for CollectionAssert.