I’ve created data driven tests many times in Visual Studio and I’ve used Excel spreadsheets, comma separated value (CSV) files and XML. I’ve even written a blog post or two on the subject. No matter how many times I do it though, it seems that I always get stuck at the beginning because the data files don’t get deployed. WTF? You’d think I would remember how to do it. Apparently, that’s not the case cuz tonight I ran into the same fricken, aggravating error message and just stared at it like I’d never seen it before.
Here’s the 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.” BTW, this was in VS 2010.
Best part is that if you go to that link, it’s not “Troubleshooting Data-Driven Unit Tests”. It’s titled "Working with Unit Tests” and it’s under VS 2005. No troubleshooting tips to be found there. Nothing that helped anyway.
So, I stared at my code and thought, why can’t MSTest find the stupid file and deploy it?!!! It’s not like it’s hidden. It’s sitting right there in a folder next to the test.
1: [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\WidthTestData.xml",
2: "Row", DataAccessMethod.Sequential)]
5: public void WhenObjectCreatedShouldValidateWidthValues()
I did searches on the error and came across many frustrated forum messages trying to find out where the RelativePathRoot is because the DeploymentItem attribute file location is supposed to start from “RelativePathRoot” and go from there. With that problem in mind, I pointed the DeploymentItem path to many, many variations. Project folder\subfolder\file, ..\subfolder\file, ..\..\subfolder\file, etc. None worked. I even hardcoded the path and that didn’t work. Hmmm, a clue.
RelativePathRoot wasn’t the problem. (BTW, if you were wondering, RelativePathRoot is just the folder of the solution that contains your test project. Maybe that will be useful to someone.)
I decided to use the Test Settings / Deployment option (double click on “Local.testsettings” in Solution Items and you will get the Test Settings dialog) to force deploy the file rather than rely on the DeploymentItem attribute. When I clicked on the “Deployment” option I saw it immediately (again). I forgot to check to see if the “Enable deployment” option was checked. There’s 45 minutes of aggravation I’ll never get back.
Once I clicked that option, my data driven tests started working and all was good – for the moment anyway. So, keep that in mind when you run into that cryptic error message when you’re creating data driven tests.
One other thing, setting the file to “Copy always” or “Copy if newer” is NOT the answer. Don’t do it.
And now, back to my regularly scheduled testing…