I'm currently maintaining an "old" system written in C#.net, removing some obsolete features and doing some refactoring. Thanks god, the previous guy wrote some unit tests (MSTests). I quite comfortable with JUnit tests, but didn't do yet much with MSTests.
The test methods have a DeploymentItem
attribute, specifying a text file which is parsed by the business logic method that is being tested and a 2nd DeploymentItem
where just a path has been specified containing a bunch of TIF files that have to be deployed too.
[TestMethod()]
[DeploymentItem(@"files\valid\valid_entries.txt")]
[DeploymentItem(@"files\tif\")]
public void ExistsTifTest()
{
...
}
The tests worked before, but now I had to change the names of the TIF files contained in the \files\tif directory. According to a rule, the TIF filenames have to match a certain pattern which is also checked by the ExistsTifTest()
method.
Now I had to change the filenames in order to adapt them to the new requirements and suddently the TIF files are no more being deployed as before.
Can someone give me a hint why this happens or what may be the cause? The same thing happens also if I add a new text-file say "my2ndTest.txt" beside the "valid_entries.txt" in the \files\valid\ directory with the according DeploymentItem attribute on the test method. The file doesn't get deployed?
I got the images now deployed by defining the deployment path directly in the testrunconfig, but I'd like to understand why these things happen or why for instance my new file "my2ndTest.txt" doesn't get deployed while the others do.
I was having huge problems trying to get files to deploy - trying all the suggestions above.
Then I closed VS2010; restarted it, loaded the solution and everything worked. (!)
I did some checking; After setting the 'Enable deployment' flag on local.TestSetting you should not simply re-run the test from the Test Results window. You have to get the previous test run removed from the UI e.g. by running a different test, or by re-opening your solution.
Besides the Deployment attribute needing to be checked, I discovered something else about the DeploymentItem attribute.
Your deploymentFile.txt needs to be relative to the solution file and not the testfile.cs.
For hopefully helping someone else out: I tried all the suggestions here and still my deployment item was not being copied.
What I had to do (as suggested here) was to add a second parameter to the DeploymentItem attribute:
Not sure if this exactly answers the question, but it may help some. First, I've found the "Enable Deployment" box must be checked for deployment to work. Second, the doc says the source path is "relative to the project path" which at first I took to mean the project folder. In fact, it seems to refer to the build output folder. So if I have a project folder called 'TestFiles' and a file in it called
Testdata.xml
, using the attribute this way doesn't work:I can mark the
Testdata.xml
fileCopy Always
, so that the build puts a copy under the output folder (e.g.,Debug\TestFiles\TestData.xml
). The deployment mechanism will then find the copy of the file located at that path (TestFiles\Testdata.xml
) relative to the build output. Or, I can set the attribute this way:and the deployment mechanism will find the original file. So either works, but I have noticed that using the
Copy Always
I occasionally run into the same problem I have when editing the app.config file in a project - if I don't change code or force a rebuild, nothing triggers copying of files marked to be copied on build.After trying all of the other suggestions listed here I still couldn't figure out what was going on. Finally I discovered that there was no settings file selected under Test/Test Settings menu, which meant that Deployment wasn't being enabled. I clicked the Test/Test Settings/Select Test Settings File menu item, selected the Local.TestSettings file, then everything worked.
For me, the root cause was something else entirely: The production code being exercised by my tests was renaming and/or deleting the .xml test file being deployed.
Therefore, when I would run my tests individually, they'd pass, but when running them all together, the 2nd and subsequent test would fail with "file not found" errors (which I'd originally misdiagnosed as the
DeploymentItem
attribute not working).My solution was to have each individual test method make a copy of the deployed file (using this technique), and then have the production code being tested use the copied file instead of the original.