Saturday, August 3, 2019

C# - Unit Test Exception - The unit test adapter failed to connect to the data source or to read the data.

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

C# - Unit Test Exception - The unit test adapter failed to connect to the data source or to read the data.

  
I followed the below steps to fix this problem.

Step 1. Open the Property of Pesons.csv File

C# - Unit Test Exception - The unit test adapter failed to connect to the data source or to read the data.


Step 2.  Change the "Build Action" property of CSV File to Content and "Copy to Output Directory" to Copy always OR Copy if newer.


Copy to Output Directory


After made above change, ran unit test



Happy Testing!!

No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...