33

xUnit – Strongly Typed Test Data

 4 years ago
source link: https://www.tuicool.com/articles/ERjimuz
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.

juMva2j.png!web

This is the SampleData class:

A7vyiqA.png!web

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

bIFjIfz.png!web

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK