xUnit has a quirky system for consuming test data. Strongly typed test data can be specified with the MemberData attribute and the Theory attribute but it’s not intuitive.

The MemberData attribute allows you to specify a getter that returns an enumeration of object arrays. It expects the type to be IEnumerable<object[]> . The trick is to return a List with multiple object arrays in it. Here is some example code for the getting the strongly typed test data. This makes for much cleaner unit testing.

This repo can be cloned here. This is the source code for the unit test.

The getter for the test data:

Sampledata.png

This is the SampleData class:

Sampledataclass.png

This is how your unit test can consume the strongly typed data:Sampledatausage

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s