I recently tried to convert my unit test into data driven unit test, so that my unit test can able to test multiple scenarios.
For this purpose, I added one CSV data file which have multiple row and used DataSource attribute to access CSV file.
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", @"|DataDirectory|\Data\Persons.csv", "Persons#csv", DataAccessMethod.Sequential)]
public void Verify_Person_Name()
{
string _firstName = TestContext.DataRow[0].ToString();
string _lastName = TestContext.DataRow[1].ToString();
string _name = TestContext.DataRow[2].ToString();
Person person = new Person(_firstName, _lastName);
Assert.IsTrue(person.Name == _name);
}
When I ran above unit test, I got below error.
Message: The unit test adapter failed to connect to the data source or to read the data. For more information on troubleshooting this error, see "Troubleshooting Data-Driven Unit Tests" (http://go.microsoft.com/fwlink/?LinkId=62412) in the MSDN Library. Error details: The Microsoft Jet database engine could not find the object 'Persons.csv'. Make sure the object exists and that you spell its name and the path name correctly
I followed the below steps to fix this problem.
Step 1. Open the Property of Pesons.csv File
Step 2. Change the "Build Action" property of CSV File to Content and "Copy to Output Directory" to Copy always OR Copy if newer.
After made above change, ran unit test
Happy Testing!!
No comments:
Post a Comment