0

I have a unit test project called "RegressionTests".

In here are various sub-folders one of which is called "FileUpload"

In this folder there is a cs file called "FileUploadRegressionTests.cs" and a source file called "test.txt". test.txt is set to be copied to the output folder.

In my unit test when I run it I try the following:

if (System.IO.File.Exists("test.txt")) {Console.Write("FOUND 1\r\n");}
if (System.IO.File.Exists("FileUpload\\test.txt")) {Console.Write("FOUND 2\r\n");}
if (System.IO.File.Exists("C:\\dev\\...FileUpload\\test.txt")) {Console.Write("FOUND 3\r\n");}

So here I am trying to access the file. If I use a relative path test.txt or 'FileUpload\test.txt' (because in the output folder it is copied into the project subfolder) it does not find the file. Only if I use the absolute path will it find the file... I can't leave it like this otherwise the next person to run the test will fail if they clone the project into a different folder.

I tried to get the current directory with:

System.IO.Directory.GetCurrentDirectory()

But this just returns C:\Windows\System (or something like this)

How can I reference my file with a relative path?

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • what does System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) give you? – BugFinder Sep 20 '17 at 13:40
  • don't use IO hard stuff in unit tests. In Your application You can have a dependency on IO specific stuff, and mock it in the unit tests. Using file system in unit test makes it super slow for the task. – ntohl Sep 20 '17 at 13:41
  • @BugFinder I tried various iterations of the GetExecutingAssembly and they seemed to yield null values - I think this may be to do with it being a unit test and not an actual executable output file? – code_fodder Sep 20 '17 at 13:44
  • @ntohl at the moment, I don't really mind which way I do this - working at all is better then working slow at the moment! : ) ... but I agree it does take an age (5-10 seconds) to run a simple test : ( – code_fodder Sep 20 '17 at 13:45
  • @code_fodder unit-tests have special folder handling anyway... https://stackoverflow.com/a/649449/1859959 . Just spare Yourself from the whole topic of problem by using `IFileSystem.Read / Write`, and compare the test.txt in memory, and check existence of file by `FileSystemMock`. – ntohl Sep 20 '17 at 14:04
  • @ntohl - yeah I see where you are coming from with the mocking...but in our case we actually want to have the external dependency file (breaking unit test methodology somewhat). The problem is, is that our Regression tests do more the simple unit tests - we are using this to run some system level tests as well (in the case I want to FTP a file) so access to the physical file would be a good thing. – code_fodder Sep 20 '17 at 14:18

1 Answers1

0

You can use '..' to get the parent directory and '.' to get the current directory:

if (System.IO.File.Exists(@"..\..\test.txt"))

will look for test.txt up two directory levels.

if (System.IO.File.Exists(@".\test.txt"))

will look for test.txt in the current directory.

Valid
  • 767
  • 7
  • 14