Search code examples
c#visual-studio-2019vs-unit-testing-framework

Visual Studio 2019 running UnitTests from different folders?


I recently upgraded Visual Studio vom 2013 to 2019. Now I'm runnig VS Professional 2019, V16.7.4.

I have loaded a Solution with 3 C#-projects and 3 corrosponding projects with unit-tests. The Unittest-Dlls are built into the right folder and some testdata is copy below that folder via a Postbuild-Script. The Unittest itself searches its Testdate below its runningdirectory which is found via

public static string TestDataDir
{
    get
    {
        Assembly assemblyExecuting = Assembly.GetExecutingAssembly();
        string sFullDllName = assemblyExecuting.Location;
        string sExecutingDir = Path.GetDirectoryName(sFullDllName);
        string sTestDataDir = Path.Combine(sExecutingDir, "Testdata");
        System.Diagnostics.Debug.WriteLine("TestDataDir= " + sTestDataDir);
        return sTestDataDir;
    }
}

All this worked well in Visual Studio 2013 but now (2019) this happens:

Good case: Good case when running from 3rd level

When I open the Test Explorer I see a hierarchical view. When I run the tests from the "3rd level" (where all Testclasses are listed), they work well and the result of TestDataDir is S:\WinSPC\V21xx.vs2019\__BIN\i386\Debug\GuidedSequence\UnitTests\GSDataIO\Testdata - which is what I want


Bad case: Bad case - running from top

But when I try to run the tests from the 1st or 2nd level (where all tests in all testclasses should run) TestDataDir returns S:\WinSPC\V21xx.vs2019\__src\_Libraries\GuidedSequence\TestResults\Deploy_werber 2020-09-22 12_50_41\Out\Testdata what makes the test fail of course because thats the wrong folder.


So how comes the difference? And whats more important: How to run all tests from the Directory where they are built to like in the good-case?

I only run my tests at the developmentmachine, so deployment is needed and I find its kind of annoying to pollute my sourcefolder with all the testresults.


What I tried to do already is to use a .testsettings-file where I disable any deployment and set "local execution" but everytime I use such a file all tests are just skipped.


Solution

  • I think I got it finally: .testsettings-files are deprecated and replaced by .runsettings-files. When I create such a file and use it, tests work again. Unfortunately there is no Click-Editor anymore so you have to search the internet for options und hack them in with an editor ;-(