I have a method that calculates intraclass correlation based off a datatable of values. I wanted to write a unit test for this method that would read in the results from a CSV file and pass the datatable to the method I’ve written. I thought I could use the DataSourceAttribute on the method to pass in the entire dataset, but I guess using this method only passes in one row at a time.
[TestMethod()]
[DeploymentItem("IntraclassCorrelationValues.csv")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\IntraclassCorrelationValues.csv", "IntraclassCorrelationValues#csv", Microsoft.VisualStudio.TestTools.UnitTesting.DataAccessMethod.Sequential)]
public void TestCorrelation() {}
Is there another method or attribute that will pass in the entire dataset instead of row by row to a test method or am I stuck writing code to read each line and build the datatable myself?
Note that the DataSource and DeploymentItem attributes for TestMethod are provided for the purposes of running multiple tests under a single test method. This can be compared to data driven tests, or parameterized tests. The idea is to allow you to specify multiple sets of parameters, each set for a single test run. For this reason, your test is reading in one row at a time, as it is designed to run one test per row.
If the input to ‘each test run’ is instead a table or rows, and not just one row, I can see that you could flatten a table to one row and then expand it again in the test (this is simply one option, allowing you to devise the custom flatten/expand code). I can think of other options, but I suppose it depends on your goals. If you need the input to the test to be easily changed without rebuilding the tests, then this data driven model is best. If you simply want to drive the test with a table of data (that won’t change in the future runs of the test), then consider something more static, hard coded in with the test. Besides, that could make the test more readable. You can always write code to fake the source later.