XUnit has some cool stuff like Theory, which lets you write multiple test cases to run against the same test code. This lets you test multiple paths with different inputs without having to write multiple tests. This can save you a lot of time as you can cover a lot of ground with a single chunk of code.
Theory
In this (admittedly contrived) example, we have a MyCouponService that returns true if the Guid starts with a “abc”. So, we can write a couple of different test cases to check to make sure that it works the way we’re expecting:
This works, but there’s kind of a problem – All of the tests cases show as one single test in the test explorer. If one of these tests fails, its difficult to know which test case is failing.
IXUnitSerializable
In order to have the test cases show up as individual tests, each parameter needs to be serializable by XUnit. Unfortunately for us, a Guid is not serializable by default.
One way to alleviate this is to create a test case class and implement IXUnitSerializable. This requires that you implement a Serialize and a Deserialize method. The Serialize method will let us tell XUnit how a field is serialized to a string. For us, its easy, we just say ToString on the Guid. The Deserialize method is, I think, used if you have your test cases stored in an external file that is read in by XUnit. For our case, it doesn’t matter, so we can just leave it blank.
This enumerates all of our test cases individually, making it a lot easier to see the status of the individual tests.