How to get Directory while running unit test

后端 未结 14 863
余生分开走
余生分开走 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:44

    In general you may use this, regardless if running a test or console app or web app:

    // returns the absolute path of assembly, file://C:/.../MyAssembly.dll
    var codeBase = Assembly.GetExecutingAssembly().CodeBase;    
    // returns the absolute path of assembly, i.e: C:\...\MyAssembly.dll
    var location = Assembly.GetExecutingAssembly().Location;
    

    If you are running NUnit, then:

    // return the absolute path of directory, i.e. C:\...\
    var testDirectory = TestContext.CurrentContext.TestDirectory;
    

提交回复
热议问题