简体   繁体   中英

AppDomain.CurrentDomain.BaseDirectory changes after running test?

In VS2012, I have a bunch of (MS) unit tests, which use different xml/text files I've stored in the same folder as the test file class.

I open these files using File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + "file")

If I run just one test, it works, the file is read.

If I run all the tests at once, the BaseDirectory returns a different path (in TestResults/Deploy/currentdatetime/Out) and obviously cannot find the file there.

What to do?

Thanks!

We ran into this issue with VS 2017, but all tests including individual tests were being ran from the TestResults folder. Our problem was with log4net.config not getting copied to the TestResults folder (even though Copy Always was true).

Our solution was to delete the LocalTestRun.testrunconfig file from the Solution Items folder (directly under the solution). When we did that it started using the \\bin\\debug\\ folder as it should and found our log4net.config file.

Well didn't make it work, and seems like google thinks its never going to.

Workaround was simply to edit the path like:

    [ClassInitialize]
    public static void ClassInitialize(TestContext context)
    {
        string deployDir = AppDomain.CurrentDomain.BaseDirectory;
        string[] rows = deployDir.Split(new string[] { "TestResults" }, StringSplitOptions.None);
        string projectPath = rows[0].ToString();
        _path = projectPath + "newpath\\";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM