2

Having upgraded a large project from VS2008 to VS2013, a large number of unit tests are now failing because the associated data file cannot be found. The original DataSource attributes were created by the test connection string wizard that VS2008 provided, but this is no longer available in VS2013 Pro. The data files are definitely there, in exactly the same place in the solution, and all have the properties set to Copy Always. I suspect that the required arguments to the DataSource attribute have subtly changed but the MSDN documentation offers little help in this respect.

The error is:

Result 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 'MatrixSampleResultGrid_ExcludeHiddenResults.csv'. Make sure the object exists and that you spell its name and the path name correctly.

Previously this error has always been reported because the data file has been moved or renamed without the attribute being updated, but that is definitely not the case here.

This is a typical current DataSource attribute definition:

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\MatrixSampleResultGrid_ExcludeHiddenResults.csv", "MatrixSampleResultGrid_ExcludeHiddenResults#csv", DataAccessMethod.Sequential)]

In the VS solution (ie on disk) the actual path to the datafile is

[theProjectRootFolder]\TestData\MatrixSampleResultGrid_ExcludeHiddenResults.csv

The test results are published to

[theProjectRootFolder]\TestResults\[testrun_datetimestamp]\In and ...\Out

although I notice that none of the data files have been copied to the In or Out folders. Is that significant?

With VS2008 these attributes have worked unchanged every day for years, so I can only conclude that for VS2013 the data is no longer appropriate, but what has changed? Without that wizard I can't even reconstruct the attribute so I am at a loss.

Also, I don't know what location "|DataDirectory|" represents in the context of a test run.

Can anyone help? TIA.

haughtonomous
  • 4,602
  • 11
  • 34
  • 52

1 Answers1

3

I have figured out the solution to why all our VS2008 data-driven tests fail to find their data files in VS2013. Having spent hours reading all the MSDN documentation I could find on unit testing and TDD in VS with absolutely no illumination (useless!), I am posting the solution here to save others in a similar situation from all the pain:

The VS2013 test framework seems to have different rules on where to look for the data file (different to VS2008, that is). Either we were inadvertently employing an ‘undocumented feature’ in VS2008 which no longer works, or MS have simply changed it. I don’t know which, but it has changed.

However the fix is simple once you have tumbled to the cause. Assuming the data file is in a subfolder of the test project folder (ex [projectfolder]\TestData) , the old VS2008 test attributes:

(ex) [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\MyDataFile.csv", " MyDataFile#csv", DataAccessMethod.Sequential)][DeploymentItem("Test Projects\\Project1Tests\\TestData\\MyDataFile.csv"), TestMethod()]

need to be amended to

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "MyDataFile.csv", "MyDataFile#csv", DataAccessMethod.Sequential), DeploymentItem("TestData\\MyDataFile.csv"), TestMethod]

The changes are

  1. The second DataSource argument is just the filename (not a relative path), and
  2. The DeploymentItem argument is a path relative to the project folder that contains the tests.

Also (this has been documented elsewhere on this forum)

  1. The datafile properties must be set to BuildAction=none (or the default blank), Copy To Output Directory=Copy Always.

and you have to have a TestSettings configuration in the solution with Deployment checked on.

Do all that and the old VS2008 data driven tests will magically all start finding their datafiles.

Onilol
  • 1,315
  • 16
  • 41
haughtonomous
  • 4,602
  • 11
  • 34
  • 52
  • @Heil As mentioned in msdn DeploymentItem param is the path is relative to the build output directory. link is: https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.testtools.unittesting.deploymentitemattribute.-ctor?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DEN-US%26k%3Dk(Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItemAttribute.%2523ctor)%3Bk(TargetFrameworkMoniker-.NETFramework%2CVersion%3Dv4.0)%3Bk(DevLang-csharp)%26rd%3Dtrue – Allen Aug 15 '17 at 03:28