7

I'm trying to set up some unit tests for a private project. Since I don't mind the exact runner, I tried NUnit, XUnit and MStest, but none worked so far.

After some research, I cloned the official Microsoft repository here :https://github.com/dotnet/docs/blob/master/samples/core/getting-started/unit-testing-using-nunit/PrimeService.Tests/PrimeService_IsPrimeShould.cs But these tests can't be executed as well. When doing so, I get the following error in the output:

System.ArgumentException: Illegal characters in path. at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional) at System.IO.Path.Combine(String path1, String path2) at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.DotnetHostHelper.TryGetExecutablePath(String executableBaseName, String& executablePath) at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.DotnetHostHelper.GetDotnetPath() at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Hosting.DotnetTestHostManager.GetTestHostProcessStartInfo(IEnumerable1 sources, IDictionary2 environmentVariables, TestRunnerConnectionInfo connectionInfo) at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.SetupChannel(IEnumerable`1 sources, CancellationToken cancellationToken) at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyDiscoveryManager.DiscoverTests(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 eventHandler)

I also checked out several SO answers like Unit Tests not discovered in Visual Studio 2017 or How to run NUnit tests in Visual Studio 2017?, but none of them fixed the problem.

My Visual Studio 2017 Version is currently 15.6.0 and I'm also using ReSharper. It seems like this is a local environmental problem, unfortunately, I didn't find any hints regarding that problem. Has anyone a idea, how that problem could be solved?

Matthias Müller
  • 3,336
  • 3
  • 33
  • 65

4 Answers4

9

I had exactly the same problem after moving from NUnit to XUnit on a large solution.

I tried creating a new project that contained just a single dummy test. This also exhibited the problem, meaning it was environmental.

I eventually discovered Git had placed double quotes around its entry in the PATH environment variable, like this:

...;"C:\Program Files (x86)\Git\bin";...

Unfortunately, my path was very long (3600 chars), so the Windows GUI would not let me change it as it was more then 2047 chars.

I was able to correct it by changing the Path stored in the Windows Registry at Computer\HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment

I was able to reproduce the behaviour at will by re-adding the quotes to the Path environment variable.

I think there was a bug with an older version of Git I had installed (long ago) which added the quoted locate to the path environment variable, and newer updates never changed it. I suspect the NUnit test runner was tolerant of the quotes, while the XUnit test runner is not.

I hope my experience helps with your issue.

Raymond Wilson
  • 217
  • 2
  • 6
  • Hm I think in my case it was in fact because of the update, but I did miss the git variable, which is also a very valuable information to have. – Matthias Müller Mar 29 '18 at 06:12
  • 1
    In my case it was Java Runtime, which adds double quotes in path. Thank you. – Karel Kral May 30 '18 at 10:04
  • this helped me as well. I had double quotes around Java path. – Raha Oct 20 '18 at 00:58
  • Same case was here for Java. However, I couldn't edit registry and remove the quote as it gave some write error. When I explored this same by going to ControlPanel > System > Advance settings window, it didn't show the quotes in the entry. So I simply edit the entry without any modification and saved. It solved the xUnit test problem in VS2017 – Arjun_TECH Feb 26 '19 at 14:11
  • I had to reboot, but removing four double quotes from the PATH variable seems to have done the trick! Thank you! – HenningNT Aug 15 '19 at 05:11
2

Just in case someone still has the same issue of "0 Tests Found -- Invalid Character in path". I was experiencing it but I was able to solve it, however, it took me two days to find the solution. The solution is very simple but at the time it was not clear to the surface. Solution: (Windows - PCs)

  • Open This PC -> Properties -> Advanced system settings -> Environment Variable -> User variables
  • Scroll to the "path" and click Edit and then click "Edit text..."
  • check the whole text and remove any double quotes, <> if they are available and click OK.
  • Do the same with System variables path.
  • Head back to Visual Studio, build your solution and Run All tests.

After these steps, the tests should be found this time.

jtate
  • 2,612
  • 7
  • 25
  • 35
  • Some similar trick worked for me. I had to remove some unnecessary javapath from my env variables. Thanks a ton for saving my two days worth of hassle @jtate – kjosh Mar 06 '20 at 23:43
1

In my case the problem disappeared when I updated VS2017 to version 15.6.4.

FyCyka LK
  • 54
  • 5
  • I aded the Git Variable and updated, now it works. Strange thing, I didn't find anything about this error on the web. – Matthias Müller Mar 29 '18 at 06:13
  • Yeah strange indeed. In the release notes they mentioned some fixes for unit tests but none of them is about this issue. Maybe they touched something that fixed it for us. – FyCyka LK Apr 01 '18 at 12:43
1

In my case VS 2019, there was no issue in Environment Path...so I had 2 options either reinstall (not sure whether it will work or not) or use VS on different machine, I used VS on different machine, it worked...its strange that VS IDE has some issue when run test cases...

Rohit Maurya
  • 154
  • 1
  • 1
  • 10