简体   繁体   English

.NET Core 与 NUnit 测试

[英].NET Core with NUnit test

I'm using .NET Core and am creating a unit test.我正在使用 .NET Core 并正在创建单元测试。

To do this, I'm running "dotnet new nunit -n tests" to create a project with the first class to test another project.为此,我正在运行“dotnet new nunit -n tests”来创建一个带有第一个类的项目来测试另一个项目。

The problem is: I can run the simple code created with "dotnet test", but I can't code because I'm receiving 'could not be found NUnit' error.问题是:我可以运行用“dotnet test”创建的简单代码,但我无法编码,因为我收到“找不到 NUnit”错误。

PS.: I'm using Visual Studio Code (last version). PS.:我使用的是Visual Studio Code (最新版本)。

My .csproj:我的.csproj:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="nunit" Version="3.11.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
  </ItemGroup>

</Project>

The error:错误:

The type or namespace name 'NUnit' could not be found (are you missing a using directive or an assembly reference?)找不到类型或命名空间名称“NUnit”(您是否缺少 using 指令或程序集引用?)

I solved the problem using Visual Studio 2017 (not Visual Studio Code).我使用Visual Studio 2017 (不是 Visual Studio Code)解决了这个问题。 It is better to architect a Microsoft solution.最好构建一个 Microsoft 解决方案。

EDIT : If you are using Visual Studio (complete version) you can see better the solution, projects and references.编辑:如果您使用的是 Visual Studio(完整版),您可以更好地看到解决方案、项目和参考资料。

If you are using Visual Studio Code and include new class or files, the reference is not added in you project file (.csproj) automatic.如果您使用的是 Visual Studio Code 并包含新类或文件,则不会自动将引用添加到您的项目文件 (.csproj) 中。 Maybe have another way to do that, but in the moment, I just use the complete version.也许有另一种方法可以做到这一点,但目前,我只使用完整版本。

I ran into this issue too and was perplexed by it.我也遇到了这个问题,并被它弄糊涂了。 It turns out the problem is with the solution, not the dotnet new nunit project that was created.结果证明问题出在解决方案上,而不是创建的dotnet new nunit项目。

The solution is auto-including the sub directory and attempting to build it as source.解决方案是自动包含子目录并尝试将其构建为源。 The directory can be excluded by right clicking the new test project in the solution and selecting "Exclude from Project".可以通过右键单击解决方案中的新测试项目并选择“从项目中排除”来排除该目录。 Rebuild and the errors go away.重建和错误消失。

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

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