Writing Unit Tests: How to get folder with testfiles programmatically
I am writing unit tests in visual studio 2010.
For test some functionali开发者_如何转开发ty, I have added a folder with testfiles. I need to get this folder programmatically without a hard path in a string.The folder contains in <projectDirectory>/TestFiles
I have tried to use AppDomain.CurrentDomain.BaseDirectory
.
This will only work if I run my unit tests with resharper.
<projectDirectory>/bin/debug
so I can easily go to TestFiles.
If I am running test with visual studio, the BaseDirectory is:
<sameFolderAsSolutionFile>\TestResults\<username>_<pcname> <datatime>\Out
I have moved my solution file to another folder. So my projects aren't in the same folder as my solution file.
Example:<sameFolderAsSolutionFile> = C:\SolutionFiles
<projectDirectory> = C:\Projects\MyProject
Can someone tell me how to get the path to my test-files without using a hardcoded string?
EDIT
I haven't found a solution yet. Visual Studio is using another build folder for testing. So everything what is normally builded into the bin folder will be builded into another folder for the test.MY TEMP-SOLUTION
I have added a App.config file in my test project. In this configuration file I have added a setting with the required path to the test files:<appSettings>
<add key="TestFiles" value="C:\Projects\MyProject\TestFiles"/>
</appSettings>
You can copy this folder into the deployment folder of the unit tests with the DeploymentItem attribute.
Even the question is already answered, I want to contribute with a solution that worked for me, as did not the others. You will need to copy the file to the bin directory of the test project marking the file in its properties Copy to ouput directory = "Copy always", then in the test you can read the file with this code:
var baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var testFile = Path.Combine(baseDir, "TestFiles", "file.txt");
var file = File.OpenRead(testFile))
The most reliable way is to use TestContext. This object has whole bunch of properties for deployment path, results path etc. See TestContext Properties.
You can use Path.Combine(Assembly.GetExecutingAssembly().Location,"TestFiles")
to get to that Folder without hard coding anything. You have to make sure that you added the folder and files to your test project and have set its CopyToOutput
directory appropriately.
We do it this way:
var assembly = Assembly.GetAssembly(this.GetType());
var codebase = assembly.CodeBase.Replace("file:///", "");
var baseDir = Path.GetDirectoryName(codebase);
This C# code snippet comes from the unit test setup routine and retrieves the directory of the unit test code. From here you can navigate ... I do not think its very elegant...but :)
In case someone like me comes along, if you're using a .testsettings file and the DeploymentItem attribute on the class doesn't work even though you've set your files as Content and to Always Copy, it's because either you already have a Deployment section in your .testsettings file or you need to use DeploymentItem in the .testsettings file. Here's how ours looks now:
<Deployment>
<DeploymentItem filename="Tests\Unit Tests\ConnectionStrings.config" />
<DeploymentItem filename="Tests\Unit Tests\<project dir>\bin\Debug\TestFiles\" outputDirectory="TestFiles" />
</Deployment>
One does a file, the other does 13 files in a directory. Note the trailing slash for the directory's "filename"!
Solution and more info was found at:
MsTest DeploymentItem OutputDirectory in testsettings
精彩评论