NU1701 的 Xunit 测试失败
Xunit test fails with NU1701
我尝试让我的 Xamarin 单元测试达到 运行,但未能使其正常工作。我将以下 nuget 包添加到我的项目中:
- MSTest.TestAdapter
- MSTest.TestFramework
- xunit
我可以在测试列表中看到我的单元测试。但是当我开始 运行 单个测试时,我收到以下消息:
No test matches the given testcase filter FullyQualifiedName= ...
经过一番研究后,我在 Build 部分找到了这条消息:
Warning NU1701: Package 'MSTest.TestAdapter 1.4.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
如何获得测试 运行ning?
编辑:我从项目中卸载了 MSTest Nuget 并得到以下信息:
No test matches the given testcase filter
编辑 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 不兼容。
您几乎自己找到了解决方案。实际上,测试未 运行ning 的原因是因为 netstandard
目标框架不可执行。 netstandard
是一个契约,但它需要 运行 时间,例如 netcoreapp
、.NET Framework(例如 net472
)、Xamarin 或其他。测试是通过静态分析发现的,而不是通过执行发现的。
人们为他们的代码编写测试的通常方式是将测试放在一个单独的项目中。这也意味着当您将应用程序发送给客户时,他们安装的 dll 没有永远不会 运行 的测试代码,只会无缘无故地增大下载量。
所以,创建一个xunit测试项目。理想情况下,它的目标时间与您的应用程序 运行 相同的 运行 时间,但我从未进行过任何 Xamarin 开发,所以我不知道这是否可行,所以也许 netcoreapp2.1
或者 netcoreapp2.2
就足够了。如果您使用 Visual Studio、add a project reference,否则编辑 csproj 并添加 <ProjectReference Include="../path/to/project.csproj" />
.
如果您的生产代码有内部 类 而您不想创建 public,则将 InternalsVisibleTo
attribute 添加到您的生产程序集,它将仅用于你的测试程序集。
我尝试让我的 Xamarin 单元测试达到 运行,但未能使其正常工作。我将以下 nuget 包添加到我的项目中:
- MSTest.TestAdapter
- MSTest.TestFramework
- xunit
我可以在测试列表中看到我的单元测试。但是当我开始 运行 单个测试时,我收到以下消息:
No test matches the given testcase filter FullyQualifiedName= ...
经过一番研究后,我在 Build 部分找到了这条消息:
Warning NU1701: Package 'MSTest.TestAdapter 1.4.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
如何获得测试 运行ning?
编辑:我从项目中卸载了 MSTest Nuget 并得到以下信息:
No test matches the given testcase filter
编辑 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 不兼容。
您几乎自己找到了解决方案。实际上,测试未 运行ning 的原因是因为 netstandard
目标框架不可执行。 netstandard
是一个契约,但它需要 运行 时间,例如 netcoreapp
、.NET Framework(例如 net472
)、Xamarin 或其他。测试是通过静态分析发现的,而不是通过执行发现的。
人们为他们的代码编写测试的通常方式是将测试放在一个单独的项目中。这也意味着当您将应用程序发送给客户时,他们安装的 dll 没有永远不会 运行 的测试代码,只会无缘无故地增大下载量。
所以,创建一个xunit测试项目。理想情况下,它的目标时间与您的应用程序 运行 相同的 运行 时间,但我从未进行过任何 Xamarin 开发,所以我不知道这是否可行,所以也许 netcoreapp2.1
或者 netcoreapp2.2
就足够了。如果您使用 Visual Studio、add a project reference,否则编辑 csproj 并添加 <ProjectReference Include="../path/to/project.csproj" />
.
如果您的生产代码有内部 类 而您不想创建 public,则将 InternalsVisibleTo
attribute 添加到您的生产程序集,它将仅用于你的测试程序集。