简体   繁体   中英

Best practice for using test files/directory in Visual Studio 2010 C# test projects

I have a test project in a Visual Studio 2010 solution, which tests another project that is an FTP utility. The test project needs to FTP files here and there, so I use a test file to pass to the utilities methods and verify it gets uploaded/archived/etc. In my test project I have a folder under the root of it called TestFiles, with a single .txt file in it. I want to have it to where whenever someone checks out the solution from source control and runs the unit tests, that the file in that folder is grabbed and used in the FTP unit tests.

I've tried using this, and variations in the post-build event command line, but with no luck:

copy $(ProjectDir)TestFiles\Test.txt $(ProjectDir)Debug\bin\Test.txt

All I really want to do is make sure that I can use a relative path for any local test files, so that I am guaranteed not to have problems no matter where someone on my team checks the project out to (or if Team Foundation Server runs an automated build and fires the unit tests for my project).

I'm currently trying to access the file in my unit tests like this, so that I just grab it from the debug area:

ftp.uploadThisFileSomewhere(
AppDomain.CurrentDomain.BaseDirectory + "\\TestFiles\\Test.txt");

So, is this the proper way to grab a test file? I don't think it is, but I can't seem to find a good best practices doc on it. If it's good enough, how do you copy/guarantee a relative location no matter where the project is built/run ?

The suggested practice is to use DeploymentItem Attribute for each item that needs to be autocopied during the MSTest process.

There's a number of steps to follow, from defining the items, to making sure each file has the correct properties, blah blah blah. So, I wrote a nice lit of instructions about how to do this.

Have a read of a detailed description about how to do this .

Include the file in your project and set it's properties to Content and Copy If Newer. It will be copied to to the build/run directory at both debug and release.

Hi I have got same problem and I used Resources. I created TestFiles.resx in my test project, next I added files. And in test units I used for example TestFiles.SomeFile and I was able to forget about paths etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM