Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get path in project folder from unit test

Tags:

c#

I have a unit test project and library project. I want to use the pdf file in library project as template. When I use Environment.CurrentDirectory I get path of the unit test project, but I want to get the path of the library project to reference pdf file.

Path.Combine(Environment.CurrentDirectory, "../PDFs/template.pdf"); 

What is the equivalent statement to get path to file in library project when executing the unit test?

like image 795
billboard Avatar asked Oct 17 '22 19:10

billboard


1 Answers

You can't. The unit test project has a copy of the referenced assembly in its bin folder and that doesn't know about the original "source" location (nor should it).

Unit tests should be able to execute in a vacuum, not relying on something to exist elsewhere in the filesystem (or ideally not rely on the filesystem or other external resources at all).

The only way to solve the problem as posited is to ensure that your "template.pdf" is copied to the output directory of the unit test, probably with a post-build step. A much better approach would be to just include the template in the unit test project.

like image 138
BradleyDotNET Avatar answered Oct 21 '22 07:10

BradleyDotNET