In MSTest you can do something like:
[TestMethod]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV",
"testdata.csv", "testdata#csv", DataAccessMethod.Sequential)]
public void TestSomething()
{
double column1 = Convert.ToDouble(TestContext.DataRow["column1"]);
...
Assert.AreEqual(...);
}
What is the equivalent code in NUnit 2.5?
I think the Nunit equivilent is to mark a method as a setup method and then load the data into a field to be used in subsequent tests.
You have to code it yourself, more or less.
I would look at the parameterized tests documentation in NUnit 2.5 and see if you can do something like what you're doing there. I do not recall NUnit having a built-in CSV reading attribute to drive parameterized tests. There may be a community plug-in somewhere though.
I should also point out that if you are just looking for non-MS Unit Testing framework libraries to help you out, xUnit.net does have this functionality. Check out this blog post from Ben Hall
I got csv based data driven testing in NUnit working as follows:
Use the csv reader from code project, wrapped up in a private method returning IEnumerable in your test class, and then reference this with a TestCaseSource attribute on your test cases. Include your csv file in your project and set "Copy to Output Directory" to "Copy Always".
original post at: http://timwise.blogspot.com/2011/05/data-driven-test-in-nunit-with-csv.html
Here is another example very similar to Tim Abell's however not using a framework for the CSV reader and showing the specifics of the test. Note when you use the TestCaseAttribute the TestAttribute can be omitted.
CSV Data:
10,200,210 20,190,210 30,180,210 40,170,210 50,160,210 60,150,210 70,140,210 80,130,210 90,120,210 100,110,210
Note: The 3rd column is a sum of the first two columns and this will be asserted in the unit test.
Results:
Find below an alternative using TestCaseData objects and setting a return type (which off-course is mandatory)