I can't seem to grasp how MSTest deployment items are supposed to be configured. I have been able to get them working properly by modifying the project's test settings file, but this is less then ideal -- the deployment item configuration is separated from individual tests, and the file paths appear to be stored as absolute paths unless the files are under the solution folder.
Am I not supposed to be able to add a deployment item using the [DeploymentItem]
attribute on either a [TestClass]
or [TestMethod]
without having to create/modify a project test settings file? How do I accomplish this?
(Frankly, I don't understand the need for a separate deployment item configuration -- why not just use the existing 'Copy to Output Directory' settings for project files that should be deployment items?)
Looks like this works by default in VS.NET 2012
For my case, I add the DeploymentItem attribute, it can't take effect immediately. I have to close and re-open the solution, then the configured DeploymentItem attributes will take effect.
Ok - this post here helped me figure out what I needed to do WITHOUT having to manually add items to the
.testsettings
file.Step 1 - Enable the MS Test
DeploymentItem
attribute.First up, we need to turn on / enable the
DeploymentItem
attribute.Goto TEST -> EDIT TEST SETTINGS -> Current Active settings .. eg :: Local (local.testsettings)
Now goto DEPLOYMENT and make sure Enable Deployment is ticked ON. (By default, it's off).
Step 2 - Check the File's properties
Now we need to make sure the file which you wish to use in the unit test, is setup to be copied to the BIN directory when you compile. Only files that are in the BIN directory can be used in an MS Test unit test. Why? Because each time an MS Test is ran, it has to make a copy of the sources ... and this means it makes a copy of the current BIN directory files (for the current Configuration).
For example... Current Configuration is Debug (as opposed to Release).
I then add my file ... (take note of the folder structure in the Project)...
and then make sure this file is ALWAYS copied over to the bin directory when the project is compiled.
PRO TIP: Copy Always will also work, but always copy the source file over the destination file .. even if they are identical. This is why I prefer Copy if Newer ... but whatever floats your boat
Ok ladies and gents - still with me? Wikid.
When we compile, the file should now exist in the Bin dir....
Step 3 - Now use the DeploymentItem attribute
Ok, now we can finally use the
DeploymentItem
attribute in our code. When we do this, this tells the MSTest to copy the file (from the location relative to the bin directory) to the new MS Test directory...So lets break this down..
We all know what that is.
Starting in the bin directory, go into the
Test Data
folder and copy the100LogEntries.txt
file to a destination folderTest Data
, in the root MS Test output directory which MS Test creates when each and every test is ran.So this is what my output folder structure looks like. (Excuse all the mess...)
and voila! we have deployment files, programatically.
PRO TIP #2 - if u don't use a 2nd string argument in the
DeploymentItem
attribute, then the file will be copied to the root OUT folder, of the current MS Test.Now the path to the file is relative to the
OUT
folder for the current MS Test. As such, i explicity said to deploy the file into a directory calledTest Data
... so I need to make sure I reference that correctly in my code when I want to read in the file.Just to confirm -> the full path of that filename is translated to something like
C:\lots of blah blah blah\My Solution\TestResults\PureKrome_PUREKROME-PC 2011-01-05 23_41_23\Out\Test Data
.. for that current MS Test.HTH.
Now have a picture of a Unicorn, for reading so much :)
In VS 2012, All you need is to make the log4net.properties (or whatever config file for log4net ) file Copy if newer. ( right click log4net.properties file to bring up the properties and configure it )
If you need separate deployment item in individual test cases then please go ahead and use
[DeploymentItem("string file path")]
attribute in each test cases.In Visual Studio 2012 the output directory is the working directory which means that the DeploymentItem attribute isn't needed for the general case (where you don't have specific per-test or per-class deployment items). This means that if you don't need to use the DeploymentItem attribute if there is a set of files that you want all your tests to use or you are not too pernickety about each TestClass/TestMethod having separate deployment dependencies.
You can simply click Project | Show All Files and include the subfolders and files in Visual Studio with the 'Copy always' or 'Copy if newer' attribute to your project and the files will be copied to your output directory with hierarchy intact.
The same applies when running vstest.console.exe from the command line. See here for the details.
Update
There are cases where the default directory will not be the output directory. Specifically when you choose
Run All Tests
the default path would be underTestResults\Deploy_...
The same applies when using arunsettings
file or tests useDeploymentItems
The output directory will be the default directory when you are not using DeploymentItems and: -