简体   繁体   English

NUnit TestRunner工作目录因项目目标框架而异

[英]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. 该库使用了.NET Universe中的多个框架(例如net462或netcoreapp2.0),并且运行良好。 The test project currently uses netcoreapp2.1 and the latest NUnit framework and test runner. 该测试项目当前使用netcoreapp2.1以及最新的NUnit框架和测试运行程序。

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. 当测试项目使用.NET Core作为目标框架时,将找到文件并通过测试。 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. 当测试项目使用除.NET Core以外的任何内容时(我没有测试所有.NET Framework版本,而是某些版本),则找不到文件,并且测试失败。

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. 不使用.NET Core时,测试运行程序的工作目录似乎位于C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\Common7\\IDE\\ ,这不是项目的输出目录。

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. 如所引用问题中所述,NUnit根本不更改当前工作目录。 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. 这是设计使然,基于这样的观念:库程序永远不要更改cd,而应保留调用程序设置的cd。

There may be reasons why either the runner or Visual Studio itself changes the directory in the case of a .NET Core project. 对于.NET Core项目,运行程序或Visual Studio本身更改目录可能是有原因的。 I don't know what they are but I do know that NUnit doesn't do it. 我不知道它们是什么,但我确实知道NUnit不这样做。

The obvious workaround is to use TestContext.TestDirectory in your tests. 显而易见的解决方法是在测试中使用TestContext.TestDirectory It is provided by NUnit for this purpose. 为此,它由NUnit提供。 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. 如果您需要测试代码之外的类似内容,则可以从TestContext.TestDirectory复制逻辑,该逻辑已经过很好的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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