2

I have a few unit tests that require files from the project to be used to run the unit tests. These files are just images. I need to get the image file using some kind of function within c#, other than pasting the full path like below.

string filePath = @"C:\Users\user1\Documents\Visual Studio 2008\Projects\app1\app1.Tests\Fakes\test_files\test-image.jpg"; 

I will prefer to do something like:

string filePath = app.path + "\Fakes\test_files\test-images.jpg" 

How can I do that?

Thanks!

1
  • I've updated my answer based on your clarification; see the last part below. Commented Sep 20, 2009 at 21:52

3 Answers 3

2

If your question is about getting these files at runtime... you don't want to do this. You should be deploying these files somehow as part of your build process so that they end up in a predictable location, relative to your compiled binaries and other content.

In Visual Studio, you can set any project file's Copy to Output Directory property to have it place the file in your output folder.

Copy to Output Directory screenshot


Since it sounds like you want to get these files as part of your unit tests, you would want to use something like the MSTest attribute [DeploymentItem] instead. This will place the files in your test directory at runtime. Read about Test Deployment on MSDN.

Sign up to request clarification or add additional context in comments.

2 Comments

I edited the question, since I did not explain correctly the first time.
@bobbymcr - I think you need to do all three steps: 1) Mark the build action as content, 2) Set Copy to Output Directory to Copy Always/Copy if Newer, AND 3) Set the DeploymentItemAttribute on the test.
0

Just to be clear, this would not be a unit test. A unit test should not rely on the environment. Anything in a database, a file system, etc. should not be used. A unit test should only be used to test a single unit of code. This is due to the fact that if anyone pulls down the source and runs the unit tests (the first thing you should do after getting the source) it will not run. They have to have the environment set up before the tests will pass. These are referred to as integration tests.

That being said, you could set up a variable if it is just in one class or an app.config if it is throughout multiple classes. You could then add a path in there as the default path and use that as "app.path".

1 Comment

I agree with you in 95% of the time. But there are times when you want to test something special and you need for example an image. In these cases, I like to refer to the definition of a unit test found in "Working effectively with legacy code" that say that a unit test should be a test that run FAST. So in the case of an image (testing vision library), I try to find the most small image to fit the job. If it's longer than a couple of millisecond then I discard the test but if it's very fast, I count them as a unit test.
0

I dislike the above top-rated answer, because it doesn't answer the original question.

There are legitimate reasons to want to obtain the root path of an ASP.NET webapp, including MVC variety. For example, I need it so that I can pass in a 'URI pattern' which an XSL transform later uses to resolve a dynamically served XML document (using Xpath document function).

For the complete URI, you can perform a string operation on this.Request.Url.AbsoluteUri from within your controller/page.

Use this.Request.Path for the path to current or this.Request.ApplicationPAth for the root path of the application.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.