kódfodrász
kódfodrász

Reputation: 121

Assign a friendly name to a data set item provided to an xUnit Theory, to be used for display in the results after the theory name

I have started to use xUnit for unit tests. I have some test code working, and one test failing. The test data is provided via a MemberDataAttribute to the Theory. When the tests are executed in Visual Studio Test Explorer I see the outcome of the tests, many passing, some failing.

The results look like TestCaseName(expected: 1234abcdef1234abdc....) where I see the hashcode of the object[] used as argumentum list for the invocation of the Theory method (or at least i presume it is that).

This is generally OK, but I'd like to know if there is an elegant way to provide a hint to xUnit to display some test dataset item identitifier for the individual cases?

I think it is clear how much easier it would make to navigate the test results with respect to the input data, and this is why I'm convinced that a solution is already provided by the framework.

Upvotes: 3

Views: 2308

Answers (1)

Oblivious Sage
Oblivious Sage

Reputation: 3405

Override ToString() for your arguments.

What you're actually seeing in the failed case is the ToString() result for all the objects in the object[] passed to the theory. If you override the ToString() methods for those classes (or make custom subclasses of those classes specifically to override ToString, something I occasionally do for classes that already have a ToString() designed to give information that's helpful elsewhere) then they will display exactly the information you want in failed test cases.

Upvotes: 3

Related Questions