30

Preamble

I know, that this question seems to have anwsers here:

I have nearly the same problem like ReSharper 10 test runner failing to find AppSettings referenced from external file. But these answer stated, that the problem should be gone with ReSharper 10.0.2

Question

I included a custom.xml files with custom settings, and I need to read them from my unit test. But my file is not found.

I looked, where the process runs, and included this line

var curdir = Directory.GetCurrentDirectory();

So I found out, that curdir is C:\\Users\\MYUSER\\AppData\\Local\\JetBrains\\Installations\\ReSharperPlatformVs11_000

Now it is obvious that my file cannot be found. After googling it, it found the above mentioned questions. But none of these solutions works for me.

These are my settings:

ReSharper Settings

Visual Studio Configuration

Microsoft Visual Studio Ultimate 2012

Version 11.0.61219.00 Update 5

Microsoft .NET Framework

Version 4.6.01038

Installierte Version: Ultimate

  • Architecture and Modeling Tools 04940-004-0038003-02857
  • Team Explorer für Visual Studio 2012 04940-004-0038003-02857
  • Microsoft Team Explorer für Visual Studio 2012
  • Visual Basic 2012 04940-004-0038003-02857
  • Microsoft Visual Basic 2012
  • Visual C# 2012 04940-004-0038003-02857
  • Microsoft Visual C# 2012
  • Visual C++ 2012 04940-004-0038003-02857
  • Microsoft Visual C++ 2012
  • Visual F# 2012 04940-004-0038003-02857
  • Microsoft Visual F# 2012
  • Windows Phone SDK 8.0 - DEU 04940-004-0038003-02857
  • Windows Phone SDK 8.0 - DEU
  • ASP.NET and Web Tools 2012.3.41009
  • GenerateUnitTest 1.0
  • Git Source Control Provider 1.0.0.0
  • JetBrains ReSharper Ultimate 10.0.2 Build 104.0.20151218.120627
  • Microsoft Advertising SDK for Windows Phone 6.2.923.0
  • Microsoft Advertising SDK for Windows Phone Build 6.2.923.0
  • Microsoft XNA Game Studio 4.0
  • Microsoft XNA Game Studio 4.0
  • NuGet-Paket-Manager 2.8.60318.667
  • PreEmptive Analytics Visualizer 1.0
  • SQL Server Data Tools 11.1.20627.00
  • Microsoft SQL Server Data Tools
  • Git Extensions
Community
  • 1
  • 1
Rico-E
  • 1,806
  • 2
  • 28
  • 47

6 Answers6

53

You don't list any version of NUnit installed, but you tagged this 'nunit' so I'm going to make an assumption you are using it! Further, I'm assuming NUnit >= 3.0... ignore this if otherwise. :-)

NUnit 3.0 stopped changing current directory. It's a bad idea for us to do that anyway and it's a really bad idea with 3.0, when multiple assemblies in differing directories may be running at the same time.

So, assuming still it's an NUnit problem, instead of using a relative path, use TestContext.CurrentContext.TestDirectory as the location of your file.

Sire
  • 4,086
  • 4
  • 39
  • 74
Charlie
  • 12,928
  • 1
  • 27
  • 31
  • You never know which version information is important... You assumed correctly :) But how the heck do I copy my `custom.xml` in the other directory? I'm using `File.ReadAllText("custom.xml")` somewhere deep in my production code. I don't want to add a reference to NUnit to test if I should use `TestContext.TestDirectory`. And creating an overload which takes the working directory is no option. I assume that `custom.xml` lies next to my assembly. My installer ensures that. How can I reproduce that scenario in my test? – Rico-E Mar 25 '16 at 17:25
  • 1
    There are two ways to go in your production code: (1) Continue to assume that the program is always run with the current directory set to the same location as your assembly. That's a fairly large assumption, but it may be valid in your case. However, consider what happens if the cd is set elsewhere and you run the program using a full path. (2) Actually examine the assembly and figure out where it is located. Assembly.Location works for that if the assembly is never shadow-copied. Assembly.Codebase works better but requires that you to get the LocalPath from the resulting URI. – Charlie Mar 26 '16 at 18:33
  • I should add... whatever you do in your production code, your test code can still use TestContext.TestDirectory. – Charlie Mar 26 '16 at 18:39
  • 4
    Looks as if for NUnit 3.6.1 you can use `TestContext.CurrentContext.TestDirectory` to get the location of where your tests are running. That did the trick for me. – Manfred May 13 '17 at 00:17
9

try this:

Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
Ahmad Mousavi
  • 533
  • 1
  • 6
  • 17
3

setting

In my case was fixed when I changed to this setting

2

The thing that helped me with such issue - I just turned off NUnit 2.x support in ReSharper settings (ReSharper -> Options... -> Tools -> Unit Testing -> NUnit).

Aviw
  • 1,056
  • 11
  • 14
0

I also turned off NUnit 2.x support and even if it has obviously nothing to do with the problem, it solved it (I have ReSharper 2017.3.20180201.111630)

0

We are using TestCaseSource in our tests. So the fix for me in Rider version 2021.1.3 was to change Build-->Unit Testing-->NUnit to Test Runner enter image description here

Brian
  • 688
  • 5
  • 18