I've got a test which requires an XML file to be read in and then parsed. How can I have this file copied into the test run folder each time?
The XML file is set to "Copy if newer" and a compile mode of "none" (since it's not really a compile-able thing)
use a DeploymentItem attribute
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CarMaker;
namespace DeploymentTest
{
[TestClass]
public class UnitTest1
{
[TestMethod()]
[DeploymentItem("testFile1.xml")]
public void ConstructorTest()
{
string file = "testFile1.xml";
Assert.IsTrue(File.Exists(file), "deployment failed: " + file +
" did not get deployed");
}
}
}
It seems that if you provide a TestSettings file for the Solution then you can uncheck the "Enable deployment" option and stop mstest from trying to run from the ...TestResults\...\out folder where it doesn't copy your extra files (unless you make them a deployment option).
This is also useful if you depend on the extra files being in a preserved folder structure because Deployment items all seem to be copied directly (flat) into the temporary run folder (out) if you use the Deployment, Add Folder option in the TestSettings (answers above suggest you can keep the structure if you add each item as its own DeploymentItem).
For me it worked fine running tests directly in Visual Studio (i.e. my extra files in their structure were found and used by tests) because I had created a TestSettings file for another reason long ago (which has Enable deployment unchecked), but not when TeamCity ran mstest to run tests because I hadn't specified that the TestSettings file should be used.
To create a TestSettings file in Visual Studio, right click on the Solution and choose New Item, and select the TestSettings template. To use the TestSettings file at the command prompt of mstest.exe add the option, /testsettings:C:\Src\mySolution\myProject\local.testsettings (or add as an extra command line option in TeamCity with appropriate path)
The Preet answer is used to deploy items for a single test. If you want to do it at solution level, use the .testrunconfig settings.
Best solution to me is using testsettings, especially if multiple tests need the same datafiles.
First create a testsettings file, and add the deployment items you need (file or folder name):
<TestSettings name="Local" id="00ebe0c6-7b64-49c0-80a5-09796270f111" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<Description>These are default test settings for a local test run.</Description>
<Deployment>
<DeploymentItem filename="Folder1\TestScripts\test.xml" outputDirectory="TestScripts"/>
<DeploymentItem filename="Folder2\TestData\" outputDirectory="TestData"/>
</Deployment>
<...../>
Running in visual studio, use "select Test Settings File" from "Test\Test Settings" menu to select new testsettings
Running mstest, use the /testsettings parameter to have mstest use your testsettings.
You can define DeploymentItem in a class that holds a method with AssemblyInitialize attribute. Then you're sure that the files are copied regardless of which test you run.
Unfortunately DeploymentItem attribute is executed only on classes which contain tests you're running. So if you have 10 test classes which use the same set of files, you have to add the attribute to all of them.
Also found out that changes in *.testsettings files are not automatically refreshed in Visual Studio. Therefore after adding files / folders into deployment in testsettings, you have to reopen solution file and then run the tests.
In Visual Studio 2012, vstest.console.exe (the built-in test runner) runs with the output dir as the current path. This means that you only need to include the items in your solution with the 'Copy always' or 'Copy if newer' property for them to be used by your test. You don't need the DeploymentItem attribute for the general case. The same applies when running vstest.console.exe from the command line inside your output/test directory.
There are some cases where a separate folder is used, one of them being when you are using the DeploymentItem attribute. See here for more information.
Related
My Visual Studio 2013 Custom Build Tool step is failing because the directory in which the step is being executed is not the directory where the project file is (which was by default the case up until recently). I can patch it by adding a cd command to the start of the step to change to the project directory but I was wondering if anyone could tell me
how this directory path is set
how to change it.
The build always assumes the project directory as 'base' directory.
This gives msbuild a set location (Builds to bin\debug is a subfolder off 'Base', reference hint paths and a lot more besides).
I would just change the execute of your tool to be reference based (i.e ....\tool.exe or similar) or make use of the path environment variables ($(OutDir),$(TargetPath),$(ProjectPath),$(TargetDir) etc).
Another option that I make use of is to have a batch file called 'post.bat' that has the necessary steps to execute a custom tool. This is then placed in the project folder and added to the project as an artefact.
Without knowing exactly where your custom tool resides relative to the project (or solution) or what the 'working directory' requirements of the custom tool are I cannot suggest more.
I do have few config files and dll files that are needed in order to run the final product and I want to instruct visual studio build to pick them up.
So far it was easy to add them to the project but I wasn't able to identify the correct mix of properties that I need to setup for these files, in order to get the files inside the bin directory (target).
Note: the term deploy here doed not refer to the after build deployment, that would be a custom task anyway.
So far I see these options:
Excluded from Build: No
Content: Yes
Item Type: contains a list of ~20 options but none of them sounds like something that would work. I already tried most of them.
Select the project that requires the configuration file and right click on it and then select Properties.
Go to the Build Events tab and type in the Post-Build event command line something like the following
copy pathfiletocopy $(ProjectDir)$(OutDir)
now you could check the Copy Only on Successful Build option.
Of course you need to adapt the command line to your liking.
You can also press the button Edit Post-build and see a list of predefined macros that refers to specific informations related to your projects and solution. You could also insert other CMD shell commands like IF $(ConfigurationName) == "Release" to execute commands only when you compile for release instead of debug (for example running an obfuscator)
When I build a Visual Studio project, the executable is written to the output directory specified in the projects Property Page.
I have a project that has some extra files (e.g., .ini file) that are used by the program.
How can I configure the project to copy the file to the output directory so that when the program runs, it has a copy of the other file in its CWD?
I checked the Property Page of the file and there was nothing useful other than an option to exclude it from the build (which is disabled), and the custom-build-tool command is empty (plus it is a plain-text file that does not need any processing).
For copying a files to the output directory in Visual Studio 2003 you could use Post-Build event:
Right click on the project->Properties
Common Properties->Build Events
Set Post-Build Event Command Line to:
xcopy /y $(ProjectDir)my_file.ini $(ProjectDir)$(OutDir)
OK and build!
Please try select the file in Solution Explorer. Then you should be able to see its properties in Properties window (press F4 if it is not visible). You will find there two properties:
"Build Action" and
"Copy to Output Directory"
Set "Build Action" to "Content", and then - select an appropriate value for "Copy to Output Directory" setting.
File properties window with "Build Action" and "Copy to Output Directory" settings
If the way above doesn't work for you, please read this post "Copy to output directory issue with .inf file". And have a look at this one then "Visual Studio: default build action for non-default file-types"
While I was searching the file’s Property Page for a build-action field, I had a thought: set the custom build step to copy the file (manually). This turned out to be easier than I thought. I had figured it would require using cmd or other external executable (xcopy, robocopy, etc.), but that is not necessary.
I set the Custom Build Step as follows:
Command Line : copy $(InputFileName) $(OutDir)
Description : Copying foobar...
Outputs : $(InputFileName)
Setting the outputs field (correctly) was critical in order to prevent VS from always thinking the project is out of date and requiring to be rebuilt (I’m not certain if it needs to be prefixed with $(OutDir)\).
It is reflected in the Output window as such:
Copying foobar...
1 file(s) copied.
Compiling resources...
Linking...
For VS 2017 the command Dmitry Pavlov posted would be the following:
xcopy /y "$(ProjectDir)my_file.ini" "$(OutDir)"
Quotes are important in case there are spaces in the path to the project directory.
Expanding on Synetech's answer.
In VS2019 right click the file you want to copy in the Solution Explorer and select Properties. Then under General >> Item Type change to Copy File then hit Apply.
You now should have UI fields in the Properties Page for Destination etc.
In case this helps anyone, I needed to copy the output dll of the project i was building into another project.
xcopy /y "$(ProjectDir)$(OutDir)$(TargetName)$(TargetExt)"
"C:\Application\MyApplicationName\bin\x86\Debug"
/y = overwrite file if already exists
$(ProjectDir) = location on your machine where the project lives
$(OutDir) = is where your current build setup outputs the build
$(TargetName) = What the project being built is set to be called. Ex: XXX of XXX.dll
$(TargetExt) = the extension of the build Ex: .dll of XXX.dll
"C:/..../x86/Debug" is the location to copy to.
You need the extra $(OutDir). Otherwise, in the rebuild/clean step it will throw away your source.
CommandLine : copy "$(SolutionDir)last-script.js" "$(TargetDir)Debug"
Outputs : $(TargetDir)Debug\last-script.js
Improving Synetech
answer :
In VS 2013 C++ project Command Line : copy %(Identity) $(OutDir) Description : Copying foobar... Outputs : %(Identity)
It works , But it leads to circular dependency , i.e. it will be executed each time you demand increamental build, no meter it has been already copied.
To solve this , you can add that item at target folder, change path to $(OutDir), and use that in first added item as Output. Drawback - two items with similar name are in solution.
Also usefull xcopy with /d /y parameters in postbuild - copy only if target file date is older.
You could also after the unload the project (Right click on the project >> Unload Project) add the following inside an existent <ItemGroup> tag:
<Content Include="..\..\Config\db.config">
<Link>Config\db.config</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<SubType>Designer</SubType>
</Content>
In this case it will grab the db.config file from 2 folders up and put it in the output folder(by default bin/Debug) after creating a Config folder with the db.config file inside
Is there a way how to use web.config from my project that us under the test.
I have in mind something dynamically coping the file into my project?
You could use VS build events. Assuming you want to copy web.config from your webapp project to test project you can try something like this:
Select your Test project and open its properties window (Alt + Enter)
Navigate to Build events tab
Edit Pre-build event command line textbox to following line:
copy "$(SolutionDir)MyWebApp\web.config" "$(TargetDir)"
Command above will take web.config file from your MyWebApp project in solution directory and copy it to current project's (Test) target directory (which should be \bin).
Now, everytime you build your test project, web.config will be copied to its target directory.
I don't know what info from web.config you need in your test project. However, the need for a config file is usually a smell that you actually do integration testing rather than unit testing.
Here is the problem:
1. Create a TestProject in your Visual Studio solution.
2. Open the .testrunconfig file and under the 'deployment' menu item, select the 'Enable Deployment' checkbox.
3. Now click on 'Add Directory...' button and add a folder which has some files in it.
4. Run the test project (use a dummy test).
Ok, now go check the TestResults folder: You will see that all the files got directly copied (to the top level)- the folder itself is not copied (with the files under them). This messes up my paths during testing. Can anybody tell how to get the folder copied instead of just the files underneath ?
Thanks.
Use the [DeploymentItem] attribute on the test classes that use it. You can specify a directory:
[TestClass]
[DeploymentItem("blahblah\\myDirectory", "myDirectory")]
public class MyTest
{
}
Note:
DeploymentItem is very slow when starting the tests. It seems to copy 2 files per second.
You can specify the attribute on a test base class. But it does not always work if you have more than one test project.
You can probably specify it on a TestClass that has a method marked with [AssemblyInitialize]. Then you have only to provide it once. Not sure, you have to try.
The source directory is relative to the solution location. This is hardly documented.
Open the .testsettings file in notepad. Now, you should see that for every folder to copy
<DeploymentItem filename="FolderName\" />
Change this to
<DeploymentItem filename="FolderName\" outputDirectory="FolderName\" />
The other option you have is to create another folder beneath the original folder, and then that folder will be deployed to the out directory. For example you can have this structure:
TestFolder/
TestFolder/TestDeployment/
And then in the testrunconfig you still select the TestFolder folder and the TestDeployment folder will be deployed to the out directory.
I just had this problem too today. I solved it by adding a folder called "deployment_files" in the project that contained the required folder. Then I put the required folder into the "deployment_files" folder. THEN, I opened the LocalTestRun.testrunconfig file under the "Solution Items" folder in the Solution Explorer. Went to the "Deployment" panel in the testrunconfig property window. Added the "deployment_files" directory to the deployment and voila. The folder within that was copied to the test results Out folder.