2

dotnet new xunit-> dotnet restore-> dotnet test

总测试:1.通过:1.失败:0.跳过:0.测试运行成功。测试执行时间:1,7148 秒。

.csproj; 将目标框架更改为 net461:

  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
    <PackageReference Include="xunit" Version="2.2.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
  </ItemGroup>

</Project>

然后 dotnet restore-> dotnet test

Starting test execution, please wait...
No test is available in C:\Projects\testing\bin\Debug\net461\testing.dll. Make sure that installed test discoverers & executors, platform & framework version sett
ings are appropriate and try again.

我应该如何使用 xunit 测试 net461 项目?我已经有一个从 .NET Core 1.0 升级的大项目,并且在升级之前测试工作正常,因此更改测试框架需要一些工作。

更新

事实证明,这可能与 xunit 和测试无关——针对 net461 的 ASP.NET Core 项目将不再在我的机器上运行,无论是通过 VS 还是通过 cmd。

我正在尝试运行的项目是 VS 模板中的一个新的空 Web 项目。csproj 看起来像这样:

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

  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    <RuntimeIdentifier>win7-x86</RuntimeIdentifier>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
  </ItemGroup>

</Project>

我得到的错误是这样的:

dotnet run

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.AspNetCore.Hosting, Version=1.1.1.0, Culture=neutral, PublicKeyTo
ken=adb9793829ddae60' or one of its dependencies. The system cannot find the file specified.
   at WebApplication2.Program.Main(String[] args)

我尝试从我的机​​器上删除 Visual Studio 和 .NET Core 的所有痕迹并重新安装它们,但错误是一样的。

4

2 回答 2

1

运行时标识符推断和测试 sdk 的工作方式存在问题。

尝试将此添加到您的<PropertyGroup>(假设您在 64 位 Windows 上):

<RuntimeIdentifier>win7-x64</RuntimeIdentifier>

你也可以添加

<OutputType>Exe</OutputType>

这会将您的类库项目变成一个 exe,但这应该可以解决这个问题。

于 2017-04-07T17:25:06.623 回答
0

是的,我也遇到过同样的问题。不确定 Microsoft 是否已经有可用的修复程序,但似乎 ASP.NET Core 1.1.x 的某些包的目标是 NetStandard Library 1.6.1,它与 .NET Framework兼容...请参阅 MS 网站上的矩阵here :https ://docs.microsoft.com/en-us/dotnet/articles/standard/library#net-platforms-support

我们现在决定坚持使用 ASP.NET Core 1.0.x。

于 2017-03-30T13:32:12.670 回答