首页 文章

从未发现过单元测试

提问于
浏览
1

我在Visual Studio 2017 Update 3预览中有以下解决方案,其中包括作为NetStandard1.4的Xamarin.Forms项目和NetStandard1.4 dotnet Core Services.API项目以及NetStandard1.6 dotnet核心单元测试项目 .

enter image description here

单元测试项目仅引用服务项目 . 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());
    }
}

最后,这是我构建并从Test Explorer中选择Run All时的测试输出(为空) .

[5/26/2017 6:38:23 PM信息] ------加载播放列表开始------ [5/26/2017 6:38:23 PM信息] ====== ====加载播放列表完成(0:00:00.004501)========== [5/26/2017 6:38:24 PM信息] ------发现测试开始--- --- [5/26/2017 6:38:25 PM信息] ==========发现测试结束:0找到(0:00:00.5716028)==========

为什么MSTest单元测试不会出现在测试资源管理器中并允许我发现/运行它们?

更新

我将调试类型从Full更改为可移植,我也尝试使用xUnit并遇到同样的问题 . 所以这不是MSTest特有的 .

csproj与xUnit

<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>

1 回答

  • 2

    问题是您的 TargetFramework 设置为.NET标准版本 .

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

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

相关问题