【发布时间】:2017-10-27 23:18:47
【问题描述】:
我在 Visual Studio 2017 Update 3 预览版中有以下解决方案,它由一个作为 NetStandard1.4 的 Xamarin.Forms 项目和一个 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>
单元测试类
[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());
}
}
最后,这是我构建并从测试资源管理器中选择 Run All 时的测试输出(为空)。
[2017 年 5 月 26 日下午 6:38:23 信息] ------ 加载播放列表开始 ------
[2017 年 5 月 26 日下午 6:38:23 信息] ========== 加载播放列表已完成 (0:00:00.004501) ==========
[2017 年 5 月 26 日下午 6:38:24 信息] ------ 发现测试开始 ------
[2017 年 5 月 26 日下午 6:38:25 信息] ========== 发现测试已完成:0 找到 (0:00:00.5716028) ==========
为什么 MSTest 单元测试不会显示在测试资源管理器中并允许我发现/运行它们?
更新
我将调试类型从 Full 更改为可移植,并且我也尝试使用 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>
【问题讨论】:
标签: unit-testing .net-core mstest visual-studio-2017