Best way to reference external test files?

Class library usage, coding and language questions.
Certified Professional
Certified Professional
Posts: 83
Joined: Fri Jul 12, 2013 2:41 pm

Best way to reference external test files?

Post by kmck » Mon Feb 03, 2014 7:45 pm

Say your AUT needs to access a file (i.e. an image file to upload into a system and change a header, or a text document to attach to an email, etc.), what would you say is the best way about doing this?

The code I've been using is:

Code: Select all

Which will reference the bin/Debug folder. I'll then store a TestFiles folder in the project's folder and use general code that moves up two folders in the directory to access the project directory, and then access the TestFiles folder from there (have I lost you yet?).

So far this has worked very well when running via Ranorex Studio. However...

I have some tests that I run from command line, and the System.IO.Directory.GetCurrentDirectory() code will reference the folder where the .bat file is located, not where the .exe being run is located.

I could go through each test suite and add a condition to check if the current directory is the bin/Debug folder, but I feel like it's a little clunky. Has anyone else reference test files like this and have a more efficient way of doing so?

Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Best way to reference external test files?

Post by krstcs » Mon Feb 03, 2014 8:08 pm

I would recommend always running your tests (even the command-line driven ones) from the directory where your test files are stored. In this case you should "cd" into the \bin\Debug directory before you run your test. This will solve several other issues that might come up.

You could also create a shortcut and add the directory in the "Start In:" field. This will tell windows to set the current directory to that directory and then run the file.

I would also suggest that a more robust method would be to include the full path of the file in your test, instead of a relative path. You could even put it in as a global parameter and pass the value at runtime with the /param:<global paramter name>=<value> switch.
Shortcuts usually aren't...