Sunday, March 20, 2011

two-way list comparison in C# unit test

In my C# unit tests, I often query for a list of rows based on a list of IDs. I then want to ensure that 1) for all the IDs, there was at least one row found that has that ID and 2) for all the returned rows, each row has an ID that is in the list of IDs to find. Here is how I usually ensure that:

Assert.IsTrue(ids.All(
    id => results.Any(result => result[primaryKey].Equals(id))
), "Not all IDs were found in returned results");

Assert.IsTrue(results.All(
    result => ids.Any(id => result[primaryKey].Equals(id))
), "Returned results had unexpected IDs");

I think the use of Any and All is convenient for such checks, but I wanted to see if anyone thinks this is less readable than it could be, or if there is perhaps a nicer way of doing two-way checks like this. I'm using MSTest in Visual Studio 2008 Team System for unit testing. This perhaps should be community wiki if it's too subjective.

Edit: I'm now using a solution based on Aviad P.'s suggestion, and also the fact that the following test passes:

string[] ids1 = { "a", "b", "c" };
string[] ids2 = { "b", "c", "d", "e" };
string[] ids3 = { "c", "a", "b" };
Assert.AreEqual(
    1,
    ids1.Except(ids2).Count()
);
Assert.AreEqual(
    2,
    ids2.Except(ids1).Count()
);
Assert.AreEqual(
    0,
    ids1.Except(ids3).Count()
);
From stackoverflow
  • IMO, not as readable as it could be. Create and document a method which returns true / false. Then call Assert.IsTrue(methodWithDescriptiveNameWhichReturnsTrueOrfalse(), "reason for failure");

    Mark Seemann : +1 This is essentially the *Custom Assertion* xUnit Test Pattern, although you might as well make it a void method and move the assertion into the method.
  • You might choose to use the Except operator:

    var resultIds = results.Select(x => x[primaryKey]);
    
    Assert.IsTrue(resultIds.Except(ids).Count() == 0,
     "Returned results had unexpected IDs");
    
    Assert.IsTrue(ids.Except(resultIds).Count() == 0,
     "Not all IDs were found in returned results");
    
    Sarah Vessels : This looks good, and I like how I would write `x[primaryKey]` only once. However, I think it should be `Count() == 0`, considering how `Except` is described.
    Aviad P. : Actually, you need to keep the >0, but swap the messages around. Modified my answer.
    Sarah Vessels : Why would you need `>0`? I expect there to be no difference between the list of result IDs retrieved and the list of IDs that I queried. Intellisense describes `Except` as producing the "set difference of two sequences".
    Aviad P. : Of course, you're right. My mistake, answer modified.
    Mark Seemann : A better alternative to `Count() == 0` is `Any()`.
    Aviad P. : Don't you mean !Any() ?
    Sarah Vessels : Instead of doing `Count() == 0` or `Any()`, I ended up using `Assert.AreEqual` with `Count()` and `0`.
  • NUnit has the CollectionAssert family of assertions which help readability.

0 comments:

Post a Comment