ClassData attribute

ClassData is another attribute that can be used to create data-driven tests by using data coming from a class. The ClassData attribute takes a class that can be instantiated to fetched data that will be used to execute the data theories. The class with the data must implement IEnumerable<object[]> with each data item returned as an object array. The GetEnumerator method must also be implemented.

Let's create a LoanDTOData class to be used to provide data to test the Test_CalculateLoan_ShouldReturnCorrectRate method. LoanDTOData will return IEnumerable objects of LoanDTO:

public class LoanDTOData : IEnumerable<object[]>{     private IEnumerable<object[]> data => new[]     {                new object[]                {                    new LoanDTO                    { LoanType = LoanType.CarLoan, ...

Get C# and .NET Core Test Driven Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.