从未发现单元测试
Unit Tests are never discovered
我在 Visual Studio 2017 Update 3 预览中有以下解决方案,它包括一个 Xamarin.Forms 项目作为 NetStandard1.4 和一个 NetStandard1.4 dotnet Core Services.API 项目和一个NetStandard1.6 dotnet Core单元测试项目。
单元测试项目仅引用服务项目。 csproj 文件看起来像这样,添加了用于单元测试的 MSTest 框架。但问题是测试从未被发现。
csproj
<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
<AssemblyName>FloatSink.Services.Api.Tests</AssemblyName>
<RootNamespace>FloatSink.Services</RootNamespace>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>full</DebugType>
<DebugSymbols>True</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0"></PackageReference>
<PackageReference Include="Microsoft.TestPlatform.TestHost" Version="15.0.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.1.17" />
<PackageReference Include="MSTest.TestFramework" Version="1.1.17" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Services.API\Services.API.csproj" />
</ItemGroup>
单元测试class
[TestClass]
public class AppBuilderTests
{
private const string DebugBuild = "Debug build";
private const string ReleaseBuild = "Release build";
/// <summary>
/// Test that the ToString() method returns the string we sent into the constructor
/// </summary>
[TestMethod]
public void ToStringReturnsStringFromCtor()
{
// Act
var build = new AppBuild(DebugBuild);
// Assert
Assert.AreEqual(DebugBuild, build.ToString());
}
}
最后这是我构建时的测试输出,select 运行 全部来自测试资源管理器(它是空的)。
[5/26/2017 6:38:23 PM Informational] ------ Load Playlist started ------
[5/26/2017 6:38:23 PM Informational] ========== Load Playlist finished (0:00:00.004501) ==========
[5/26/2017 6:38:24 PM Informational] ------ Discover test started ------
[5/26/2017 6:38:25 PM Informational] ========== Discover test finished: 0
found (0:00:00.5716028) ==========
为什么 MSTest 单元测试不显示在测试资源管理器中并允许我 discover/run 它们?
更新
我将调试类型从 Full 更改为 portable,我也用 xUnit 尝试过,但遇到了同样的问题。所以这不是 MSTest 特有的。
带 xUnit 的 csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
<AssemblyName>FloatSink.Services.Api.Tests</AssemblyName>
<RootNamespace>FloatSink.Services</RootNamespace>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>portable</DebugType>
<DebugSymbols>True</DebugSymbols>
</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>
<ItemGroup>
<ProjectReference Include="..\Services.API\Services.API.csproj" />
</ItemGroup>
</Project>
问题是您的 TargetFramework
设置为 .NET Standard 版本。
尽管测试项目看起来像库,但它们具有更多 运行 可用输出的特征 - 15.0.0 测试 sdk 甚至将输出类型更改为 Exe
以触发生成所需的资产运行 测试。 (在 2.0 / 15.3.0 中,这将更改为新的 HasRuntimeOutput
属性)。
此处的解决方法是将 TargetFramework
更改为 net*
或 netcoreapp*
的某个版本,具体取决于您要 运行 测试的框架。
我在 Visual Studio 2017 Update 3 预览中有以下解决方案,它包括一个 Xamarin.Forms 项目作为 NetStandard1.4 和一个 NetStandard1.4 dotnet Core Services.API 项目和一个NetStandard1.6 dotnet Core单元测试项目。
单元测试项目仅引用服务项目。 csproj 文件看起来像这样,添加了用于单元测试的 MSTest 框架。但问题是测试从未被发现。
csproj
<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
<AssemblyName>FloatSink.Services.Api.Tests</AssemblyName>
<RootNamespace>FloatSink.Services</RootNamespace>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>full</DebugType>
<DebugSymbols>True</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0"></PackageReference>
<PackageReference Include="Microsoft.TestPlatform.TestHost" Version="15.0.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.1.17" />
<PackageReference Include="MSTest.TestFramework" Version="1.1.17" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Services.API\Services.API.csproj" />
</ItemGroup>
单元测试class
[TestClass]
public class AppBuilderTests
{
private const string DebugBuild = "Debug build";
private const string ReleaseBuild = "Release build";
/// <summary>
/// Test that the ToString() method returns the string we sent into the constructor
/// </summary>
[TestMethod]
public void ToStringReturnsStringFromCtor()
{
// Act
var build = new AppBuild(DebugBuild);
// Assert
Assert.AreEqual(DebugBuild, build.ToString());
}
}
最后这是我构建时的测试输出,select 运行 全部来自测试资源管理器(它是空的)。
[5/26/2017 6:38:23 PM Informational] ------ Load Playlist started ------
[5/26/2017 6:38:23 PM Informational] ========== Load Playlist finished (0:00:00.004501) ==========
[5/26/2017 6:38:24 PM Informational] ------ Discover test started ------
[5/26/2017 6:38:25 PM Informational] ========== Discover test finished: 0 found (0:00:00.5716028) ==========
为什么 MSTest 单元测试不显示在测试资源管理器中并允许我 discover/run 它们?
更新
我将调试类型从 Full 更改为 portable,我也用 xUnit 尝试过,但遇到了同样的问题。所以这不是 MSTest 特有的。
带 xUnit 的 csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
<AssemblyName>FloatSink.Services.Api.Tests</AssemblyName>
<RootNamespace>FloatSink.Services</RootNamespace>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>portable</DebugType>
<DebugSymbols>True</DebugSymbols>
</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>
<ItemGroup>
<ProjectReference Include="..\Services.API\Services.API.csproj" />
</ItemGroup>
</Project>
问题是您的 TargetFramework
设置为 .NET Standard 版本。
尽管测试项目看起来像库,但它们具有更多 运行 可用输出的特征 - 15.0.0 测试 sdk 甚至将输出类型更改为 Exe
以触发生成所需的资产运行 测试。 (在 2.0 / 15.3.0 中,这将更改为新的 HasRuntimeOutput
属性)。
此处的解决方法是将 TargetFramework
更改为 net*
或 netcoreapp*
的某个版本,具体取决于您要 运行 测试的框架。