Visual studio unit test, how to set the PATH?

Antonino Perricone 0 Reputation points
2025-08-13T08:40:49.33+00:00

Hello,

I would like to create a folder structure like this:

  • Solution directory
    • Solution file .sln
    • Project 1
      • source files of project 1
    • Test project
      • Source files of tests
      • WorkFolder
        • File used for testing

My problem is defining the WorkFolder, I tried on the property pages to set the working directory:

User's image

but it is ignored by Test Explorer.I also tried to create a RunSettings file, but I don't see how to specify the folder...

For me, this is a basic feature and I feel like an idiot for not being able to do this setup.

Please help,

Perry

Developer technologies | Visual Studio | Testing
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Krunalkumar Patel 5 Reputation points
    2025-08-14T04:33:39.9966667+00:00

    If your goal is simply to have Test Explorer run tests with WorkFolder.

    [TestInitialize]
    public void Init()
    {
        var workFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\WorkFolder");
        Directory.SetCurrentDirectory(Path.GetFullPath(workFolderPath));
    }
    

    This works because AppDomain.CurrentDomain.BaseDirectory is the bin folder, so going up ..\..\.. reaches your project folder.

    1 person found this answer helpful.

  2. Susmitha T (INFOSYS LIMITED) 910 Reputation points Microsoft External Staff
    2025-08-14T07:12:58.4366667+00:00

    Hope you are doing good! Thank you for reaching out. Please find the answer below. 

    1. In your test code, during the test initialization phase, you can change the current directory to point to your WorkFolder.

     [TestInitialize]
    public void Init()
    {
     var workFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,@"......\WorkFolder");  Directory.SetCurrentDirectory(Path.GetFullPath(workFolderPath));

    This works because AppDomain.CurrentDomain.BaseDirectory usually points to the bin folder of your project, so navigating up with ...... will reach your project folder where WorkFolder resides.

    1. If you're still having issues, ensure that your test project references and paths are correctly set in the properties pane of your project. Sometimes, certain configurations or project setups can interfere with the expected paths.

     

    1. If you've already tried creating a RunSettings file but didn’t see how to specify the folder, make sure you're placing it where Visual Studio is set to look for it, and it correctly references the directory you want.

     

    If issue still persist after following all the steps, We’ll be happy to assist further if needed." Kindly mark the answer as accepted if the issue resolved".

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.