Build/Test Issues

NCrunch runs in different folder than VS Test Runner

Started by MikeWard on 9,110 views

Ever since v2.5 there has been a group of tests that fail and which passed in previous versions. Today I ran down the issue.

My project uses the NancyFX framework. NancyFX supports dependency injection which is fortunate because it allowed me to resolve this NCrunch issue.

In NancyFX locating assets is generally done by asking Nancy for the root path of the application. While all tests run fine in the Visual Studio test runner, there was a group of tests that would not run in NCrunch. I resolved the issue as follows:

using System;
using System.IO;
using Nancy;

namespace PortalTests.Modules
{
    public class TestRootPathProvider : IRootPathProvider
    {
        public string GetRootPath()
        {
#if NCRUNCH
            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..");
#else
            var path = AppDomain.CurrentDomain.BaseDirectory;
#endif
            return path;
        }
    }
}


I then inject the TestRootPathProvider into my tests.

Why does the Visual Studio Test Runner work differently than NCrunch in this aspect?

Edited

Hi,

Thanks for sharing this issue.

Can you confirm which test framework you're using? I know that in some situations, the AppDomain base directory is inconsistent between MSTest and NCrunch. This is because of the way MSTest handles its 'deployment item' sandboxing, which is different between versions of MSTest and also dependent upon the MSTest configuration file.
I'm using the built-in testing framework in Visual Studio 2015 with its standard test runner. Not using a third party test runner like Resharper or NUnit.

From the command line:

mstest /?
Microsoft (R) Test Execution Command Line Tool Version 14.0.23107.0
Thanks! Are you using the DeploymentItem attribute, or do you have deployment switched on in your VS MSTest test settings?
I've had a go at trying to reproduce this issue, but the paths seem to be aligned in my simplistic test cases.

I'm wondering if there might be some aspect of MSTest here that I've overlooked.

When MSTest runs your tests, is it setting the appdomain's base directory to the same directory as the build output directory of the test DLL?

Edited

Apparently, you found the issue. Updated to 3.3.0.6 and now I have to remove my TestRootPathProvider to get tests to pass.
MikeWard wrote:Apparently, you found the issue. Updated to 3.3.0.6 and now I have to remove my TestRootPathProvider to get tests to pass.


v3.3.0.6 didn't include any changes to the MSTest adapter. So I guess this one is a mystery. It's possible we haven't seen the last of it.

Post a reply

Log in to reply.