How to get Directory while running unit test

后端 未结 14 815
余生分开走
余生分开走 2021-02-02 05:07

Hi when running my unit test I\'m wanting to get the directory my project is running in to retrieve a file.

Say I have a Test project named MyProject. Test I run:

<
14条回答
  •  囚心锁ツ
    2021-02-02 05:38

    For NUnit this is what I do:

    // Get the executing directory of the tests 
    string dir = NUnit.Framework.TestContext.CurrentContext.TestDirectory;
    
    // Infer the project directory from there...2 levels up (depending on project type - for asp.net omit the latter Parent for a single level up)
    dir = System.IO.Directory.GetParent(dir).Parent.FullName;
    

    If required you can from there navigate back down to other directories if required:

    dir = Path.Combine(dir, "MySubDir");
    

提交回复
热议问题