I’m writing a unit test for file uploading feature of my web application. I mocked a test file, and save it in the “Test files” subdirectory of the directory that my test project resident in.
As I don’t want to hard coded the file path. How can I know the directory of my project by code? I used @”..\Test files\test1.xml”, but it doesn’t work.
Thanks in advance!
Remember that your unit test project will be running relative from it’s own bin directory, and not from the project directory. You can get the path to that bin directory using:
However, the easiest thing to do is set your test files and folders to be copied to the output directory. Just click on your file in the solution explorer, hit F4, and change “Copy to Output Directory” to “Copy always” or “Copy if newer”. The folder structure will automatically also be copied. Then your relative path statements will work correctly.