简体   繁体   中英

NUnit TestRunner working directory is different depending on target framework of project

I am developing a library which has a test project in the solution. The library uses several framework from the .NET universe (like net462 or netcoreapp2.0) and works fine. The test project currently uses netcoreapp2.1 and the latest NUnit framework and test runner.

I have tests that rely on files that are included in the test project and are copied to the output folder during the build process. When the test project uses .NET Core as target framework, the files are found and the tests pass. When the test project uses anything else than .NET Core (I didn't test all .NET Framework versions, but some), the files are not found and the tests fail.

When not using .NET Core the working directory of the test runner seems to be located at C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\Common7\\IDE\\ which is not the project's output directory.

This is the not working project file

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <OutputPath>bin\Release\</OutputPath>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
   <OutputPath>bin\Debug\</OutputPath>
  </PropertyGroup>

    <PropertyGroup>
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
    <PackageReference Include="NUnit" Version="3.10.1" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
  </ItemGroup>
</Project>

And this is the working project file

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <OutputPath>bin\Release\</OutputPath>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
   <OutputPath>bin\Debug\</OutputPath>
  </PropertyGroup>

    <PropertyGroup>
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
    <PackageReference Include="NUnit" Version="3.10.1" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
  </ItemGroup>
</Project>

Does anyone know more about this behaviour? How do I fix this?

As explained in the cited issue, NUnit does not change the current working directory at all. This is by design, based on the notion that library programs should never change the cd but should leave it as set by the calling program.

There may be reasons why either the runner or Visual Studio itself changes the directory in the case of a .NET Core project. I don't know what they are but I do know that NUnit doesn't do it.

The obvious workaround is to use TestContext.TestDirectory in your tests. It is provided by NUnit for this purpose. Note that it only works in test code. If you need something similar outside of the test code, you can copy the logic from TestContext.TestDirectory , which is quite well tested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM