2

I am trying to set up unit tests for a C# project and I think I'm confused about how the directory structure of my test project is set up

In VS2017, I the project expands as follows

Solution
|--*project to test*
|    |--*project content*
|
+--*test project*
     |--Files
     |   |--TestDestination
     |   |--TestDir
     |   +--EmptyDir
     |
     |--Controllers
     |--Services
     +--Models

I have some pre-configured test directories and files in the 'Files' directory that I would like to access from my test classes, but I cannot figure out for the life of me how to express the path to that directory programmatically.

I have tried

Path.Combine("Files", "FileManager", "TestDir")

Which (I think) would navigate to the TestDir directory relative to the project in Java, but I guess C# does not share this convention. I also tried:

Path.Combine(Directory.GetCurrentDirectory(), "Files", "FileManager" "TestDir")

And

Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Files", "FileManager", "TestDir");

Both of which navigate to the following directory

'C:\Users\John\Source\Repos\*Solution*\Unit_Tests\bin\Debug\netcoreapp2.0\Files\FileManager\TestDir

Which (obviously) is not correct.

Is there a good way to access my project directory programmatically? Or would it be better to get it this way and navigate out of it? If so, how could I do that?

jlat96
  • 491
  • 1
  • 5
  • 10
  • Why do you need to navigate to it relatively? just use path combine and navigate up as many directories needed with ../ – johnny 5 Jun 27 '18 at 19:29

1 Answers1

0

you just need to navigate to the parent directories using ../ From the looks of it you need to navigate 3 parent directories up to get to the test project so this should work

Path.Combine(Directory.GetCurrentDirectory(), "../../../Files", "TestDir")
johnny 5
  • 19,893
  • 50
  • 121
  • 195