【问题标题】:Xunit test fails with NU1701Xunit 测试失败,使用 NU1701
【发布时间】:2019-11-21 07:06:42
【问题描述】:

我尝试让 Xamarin 的单元测试运行,但未能使其正常工作。我在我的项目中添加了以下 nuget 包:

  • MSTest.TestAdapter
  • MSTest.TestFramework
  • xunit

我可以在测试列表中看到我的单元测试。但是当我开始运行单个测试时,我收到以下消息:

没有测试匹配给定的测试用例过滤器FullyQualifiedName= ...

经过一番研究,我在 Build 部分发现了这条消息:

警告 NU1701:使用“.NETFramework,Version=v4.6.1”而不是项目目标框架“.NETCoreApp,Version=v2.0”恢复包“MSTest.TestAdapter 1.4.0”。此软件包可能与您的项目不完全兼容。

如何让我的测试运行?

编辑:我从项目中卸载了 MSTest Nuget 并得到以下信息:

没有测试匹配给定的测试用例过滤器

编辑 2:测试如下所示:

using System;
using System.Collections.Generic;
using System.Text;
using App.Services;
using Xunit;

namespace App.Tests.Services
{
  public class HelperTest
  {
    [Fact]
    public void Encrypt_password_Test()
    {
      string password = "Helloworld";
      string expectedResult = "";
      string result = Helpers.Encrypt_password(password);

      Assert.Equal(expectedResult, result);
    }
  }
}

我预计测试会失败,因为是空字符串。

csproj 文件:

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <NeutralLanguage>en-GB</NeutralLanguage>
    <AssemblyName>App</AssemblyName>
    <RootNamespace>App</RootNamespace>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DebugType>pdbonly</DebugType>
    <DebugSymbols>true</DebugSymbols>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
    <PackageReference Include="SharpZipLib" Version="1.1.0" />
    <PackageReference Include="sqlite-net-pcl" Version="1.5.231" />
    <PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
    <PackageReference Include="Xamarin.Essentials" Version="1.2.0" />
    <PackageReference Include="Xamarin.Forms" Version="4.1.0.581479" />
    <PackageReference Include="Xamarin.Plugin.FilePicker" Version="2.1.18" />
    <PackageReference Include="xunit" Version="2.4.1" />
  </ItemGroup>
  <ItemGroup>
    <XliffResource Include="MultilingualResources\App.de.xlf" />
  </ItemGroup>
  <ItemGroup>
    <Compile Update="Localizations\AppResources.Designer.cs">
      <DesignTime>True</DesignTime>
      <AutoGen>True</AutoGen>
      <DependentUpon>AppResources.resx</DependentUpon>
    </Compile>
    <Compile Update="Views\Login\NewProfilePage.xaml.cs">
      <DependentUpon>NewProfilePage.xaml</DependentUpon>
    </Compile>
  </ItemGroup>
  <ItemGroup>
    <EmbeddedResource Update="Localizations\AppResources.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>AppResources.Designer.cs</LastGenOutput>
    </EmbeddedResource>
    <EmbeddedResource Update="Views\Settings\AppSettings.xaml">
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </EmbeddedResource>
    <EmbeddedResource Update="Views\Login\NewProfilePage.xaml">
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </EmbeddedResource>
    <EmbeddedResource Update="Views\Settings\ProfilelistPage.xaml">
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </EmbeddedResource>
    <EmbeddedResource Update="Views\Settings\ProfilePage.xaml">
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </EmbeddedResource>
    <EmbeddedResource Update="Views\ProjectPage.xaml">
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </EmbeddedResource>
    <EmbeddedResource Update="Views\Settings\SensorListPage.xaml">
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </EmbeddedResource>
    <EmbeddedResource Update="Views\SensorTestPage.xaml">
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </EmbeddedResource>
    <EmbeddedResource Update="Views\Settings\SettingsPage.xaml">
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </EmbeddedResource>
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Tests\Projectlist\" />
    <Folder Include="Tests\Settings\" />
  </ItemGroup>
</Project>

我还创建了一个示例 xunit 测试项目。在那里我注意到它使用 netcoreapp2.1 作为 TargetFramework。当我在我的应用程序中更改 TargetFramework 时,它会使其与 Xamarin 不兼容。

【问题讨论】:

  • IT 可能对您在项目中同时安装了 xunit 和 MSTest 这一事实感到困惑。它们是两个不同的测试框架。卸载一个,然后重试。
  • 我卸载了 MSTest Nuget。您可以在编辑中看到结果。
  • 您是否至少有 1 个方法具有 [Fact][Theory] 属性,并且它是公共类上的公共方法?我认为 xunit 不会发现私有方法或类。
  • 是的。该类是公开的,Fact 方法也是公开的。测试被发现,我可以在侧面的测试列表中看到它们。
  • 如果您可以创建和分享minimal example,那将非常有帮助。在这一点上,我唯一的猜测是测试运行程序与您的项目程序集不兼容。如果您的项目仅针对 x86 编译并且测试运行程序运行 x64,或者您的项目仅针对 ARM 进行编译,则测试运行程序无法运行它。虽然,在这种情况下,我希望看到不同的错误消息。

标签: unit-testing xamarin visual-studio-2017 nuget xunit


【解决方案1】:

您几乎自己找到了解决方案。实际上,测试未运行的原因是因为netstandard 目标框架不可执行。 netstandard 是一个契约,但它需要一个运行时,例如 netcoreapp、.NET Framework(例如 net472)、Xamarin 或其他。测试是通过静态分析而不是执行来发现的。

人们为他们的代码编写测试的通常方式是将测试放在一个单独的项目中。这也意味着当您将应用交付给客户时,他们安装的 dll 没有永远不会运行的测试代码,只会无缘无故地增大下载量。

所以,创建一个 xunit 测试项目。理想情况下,它以您的应用程序将要运行的相同运行时为目标,但我从未做过任何 Xamarin 开发,所以我不知道这是否可行,所以也许 netcoreapp2.1netcoreapp2.2 就足够了。如果你使用 Visual Studio,add a project reference,否则编辑 csproj 并添加 &lt;ProjectReference Include="../path/to/project.csproj" /&gt;

如果您的生产代码具有您不想公开的内部类,则将InternalsVisibleTo attribute 添加到您的生产程序集,它将仅对您的测试程序集可用。

【讨论】:

    猜你喜欢
    • 2020-03-23
    • 1970-01-01
    • 2019-09-04
    • 2022-06-30
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 2019-07-19
    • 2019-08-29
    相关资源
    最近更新 更多