XUnit – Part 8: Using TheoryData Instead of MemberData and ClassData

I previously wrote about using MemberData, ClassData in this  post. The problem with those are their reliance on IEnumerable<object[]>. This can cause runtime issues even if we don’t get any errors at compile time. In this post I’m going to introduce a strongly typed option in xUnit called TheoryData. So in this post I’m going to re-implement the things I did in my previous post with MemberData and ClassData. Except this time I’m going to use TheoryData.

Using TheoryData Instead of ClassData and MemberData

In this section I’m going to replace the code that I’ve written before with ClassData and Member Data to use the theory data instead.

Replacing ClassData with TheoryData

Previously I used ClassData like this, I’m going to convert this code to use TheoryData instread.

We can rewrite the same thing using the TheoryData like so.

Doing things like this has many advantages. First of all, it’s simple and less code to write. Second it’s strongly-typed code  which is going to give us compile time error if anything in wrong in our code.

We can see that if we try to add the string instead of the expected type we receive a compile time error. This was not he case when we’ve used ClassData because then we were relying on object to provide data.

Replacing MemberData with TheoryData

Previously I also used MemberData for provide data to my unit tests. MemberData was simpler in that it didn’t need for a new class to be created.

But with MemberData we still relied on object, not to mention that the code is not as intuitive. Let’s convert the code above to the code that uses TheoryData instead.

As you can see in the code above, this approach is much more intuitive and short. Also we get the advantages of compile time checking when we want to fill in our data for our tests which means even more accurate tests. I personally will not going to use ClassData and MemberData. Instead I’m going to use TheoryData that as I’ve mentioned has a lot of advantages over those two. Andrew Lock also has a post about this which can be found here.


In this post we saw how we can use TheoryData to supply our test with data in strongly-typed fashion.


Hamid Mosalla

Hi, I'm Hamid Mosalla, I'm a software developer, indie cinema fan and a classical music aficionado. Here I write about my experiences mostly related to web development and .Net.


4 thoughts on “XUnit – Part 8: Using TheoryData Instead of MemberData and ClassData

  1. Hi, thanks for this post.

    I have one questions:
    How to know which record from a list is incorrect? if some test not passed

  2. The title and some of the code samples’ naming is misleading. You’re not actually replacing MemberData with TheoryData at all. You’re supplementing it. In fact, TheoryData vs. loosely-typed IEnumerable is a dimension orthogonal to MemberData vs. InlineData vs. ClassData. The former is a strictly-typed wrapper around IEnumerrable regardless of where that IEnumerable is. The latter is the “where” of your test data. You’re not replacing anything. You’re strictly-type-ifying it.

Leave a Reply

Your email address will not be published. Required fields are marked *