Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 634
Ryan Hayes Avatar asked Jan 27 '11 20:01

Ryan Hayes


People also ask

Where do I put .runsettings file?

In the IDE, select Test > Configure Run Settings > Select Solution Wide runsettings File, and then select the . runsettings file.

How do I run a test file in C#?

Either go to Solution Explorer (Ctrl +Alt+L) and right click on Solution 'BasicMath' and click on Build Solution or go to Build Menu (Alt + B) and Click on Build Solution. Choose Test Menu -> Run -> All Test or instead of this step, you can follow the given step also.

How do I test a file in Visual Studio?

Tests can be run from Test Explorer by right-clicking in the code editor on a test and selecting Run test or by using the default Test Explorer shortcuts in Visual Studio.

What is Testcontext C#?

Used to store information that is provided to unit tests.


2 Answers

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.

like image 174
Pure.Krome Avatar answered Oct 26 '22 23:10

Pure.Krome


You can use DeploymentItemAttribute, http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.deploymentitemattribute(v=VS.90).aspx.

like image 23
sarvesh Avatar answered Oct 27 '22 01:10

sarvesh