Writing data-driven unit tests
If you take a second look at the previous tests, such as the TestIntersectsWith()
test method, you will see that we tried testing various cases, such as the intersection of one rectangle with several others, some that intersect, and some that don't. This was a simple example, and in practice, there should be many more rectangles that we should test with to cover all of the possible cases of rectangle intersection.
In general, as code evolves, so do the tests and you often have to add more to the testing datasets. Rather than writing explicitly the data in the test method, as in our previous example, you can fetch it from a data source. The test method is then executed once for each row in the data source. The unit testing framework for managed code supports three different scenarios.
Data from attributes
The first option is to provide the data in code but through an attribute called DataRowAttribute
. This attribute has a constructor that...