【问题标题】:Unit Tests are never discovered单元测试从未被发现
【发布时间】: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


    【解决方案1】:

    问题在于您的 TargetFramework 设置为 .NET Standard 版本。

    虽然测试项目看起来像库,但它们具有更多可运行输出的特征 - 15.0.0 测试 sdk 甚至将输出类型更改为 Exe 以触发生成运行测试所需的资产。 (在 2.0 / 15.3.0 中,这将更改为新的 HasRuntimeOutput 属性)。

    这里的解决方法是将TargetFramework 更改为net*netcoreapp* 的某个版本,具体取决于您要在哪个框架上运行测试。

    【讨论】:

      猜你喜欢
      • 2014-10-07
      • 2018-06-06
      • 2017-10-30
      • 1970-01-01
      • 2011-12-05
      • 2015-06-16
      • 2017-08-09
      • 2015-06-25
      • 2011-03-18
      相关资源
      最近更新 更多