Xunit.Combinatorial 1.6.24

前缀已保留
dotnet add package Xunit.Combinatorial --version 1.6.24                
NuGet\Install-Package Xunit.Combinatorial -Version 1.6.24                
此命令打算在 Visual Studio 的包管理器控制台中使用,因为它使用了 NuGet 模块的 Install-Package 版本。
<PackageReference Include="Xunit.Combinatorial" Version="1.6.24" />                
对于支持 PackageReference 的项目,请将此 XML 节点复制到项目文件中以便引用包。
paket add Xunit.Combinatorial --version 1.6.24                
#r "nuget: Xunit.Combinatorial, 1.6.24"                
#r 指令可用于 F# Interactive 和 Polyglot Notebooks。将此复制到交互式工具或脚本的源代码中以便引用包。
// Install Xunit.Combinatorial as a Cake Addin
#addin nuget:?package=Xunit.Combinatorial&version=1.6.24

// Install Xunit.Combinatorial as a Cake Tool
#tool nuget:?package=Xunit.Combinatorial&version=1.6.24                

Xunit.Combinatorial

此项目允许您参数化您的 Xunit 测试方法,使它们运行多次,为测试方法的每个可能参数组合运行一次。您还可以通过使用成对策略来限制测试用例的数量,这通常提供了良好的测试覆盖率,但显著减少了在您有两个以上参数时可能会遇到的测试用例爆炸问题。

NuGet package Build Status

安装

此项目可作为 NuGet 包 提供。

示例

自动生成值

假设您有这个测试方法

[Fact]
public void CheckFileSystem(bool recursive)
{
    // verifications here
}

为了使您的测试方法调用两次,每次为其布尔参数的每个值调用一次,请更改以下属性:

[Theory, CombinatorialData]
public void CheckFileSystem(bool recursive)
{
    // verifications here
}

CombinatorialDataAttribute将为Xunit提供truefalse参数,以运行测试方法,并导致对您的测试方法进行两次调用,并且每个调用报告单独的结果。

自定义提供的值

要为每个参数提供自己要传入的值,请使用CombinatorialValuesAttribute

[Theory, CombinatorialData]
public void CheckValidAge([CombinatorialValues(5, 18, 21, 25)] int age)
{
    // verifications here
}

这将四次运行您的测试方法,每次使用规定的值。

组合效应

当然,没有多个参数的组合就不会是组合的。

[Theory, CombinatorialData]
public void CheckValidAge(
    [CombinatorialValues(5, 18, 21, 25)] int age,
    bool friendlyOfficer)
{
    // This will run with all combinations:
    // 5  true
    // 18 true
    // 21 true
    // 25 true
    // 5  false
    // 18 false
    // 21 false
    // 25 false
}

当您有超过两个参数时,测试用例的数量会急剧增长,以涵盖每个可能的组合。考虑这个有3个参数的测试,每个参数只取两个值。

[Theory, CombinatorialData]
public void CheckValidAge(bool p1, bool p2, bool p3)
{
    // Combinatorial generates these 8 test cases:
    // false false false
    // false false true
    // false true  false
    // false true  true
    // true  false false
    // true  false true
    // true  true  false
    // true  true  true
}

我们已经有8个测试用例了。随着参数数量或每个参数值数量的增加,测试用例的数量会迅速增长到一个非常大的数字。这可能会使您的测试运行时间过长。这种程度的彻底测试往往是不必要的,因为许多错误在两个参数具有特定值时就会出现。这称为“配对测试”,因为它生成的测试用例比组合测试少得多,因为它只确保有一个测试用例涵盖所有两个参数的组合,从而可以通过使每个测试用例测试多个对来“压缩”测试用例。

要使用配对测试,请使用PairwiseDataAttribute代替CombinatorialDataAttribute

[Theory, PairwiseData]
public void CheckValidAge(bool p1, bool p2, bool p3)
{
    // Pairwise generates these 4 test cases:
    // false false false
    // false true  true
    // true  false true
    // true  true  false
}

通过使用配对而不是组合,我们已将测试用例数量减半。在许多情况下,测试减少可以更大。请注意,尽管测试用例更少,您仍然可以找到覆盖任何两个参数值的测试用例(因此是配对)。

范围内的值

要运行具有参数值的测试,我们有CombinatorialRangeAttribute来生成整数区间的测试。

[Theory, CombinatorialData]
public void CombinatorialCustomRange(
    [CombinatorialRange(0, 5)] int p1,
    [CombinatorialRange(0, 3, 2)] int p2)
{
    // Combinatorial generates these test cases:
    // 0 0
    // 1 0
    // 2 0
    // 3 0
    // 4 0
    // 0 2
    // 1 2
    // 2 2
    // 3 2
    // 4 2
}

CombinatorialRangeAttribute有两个不同的构造函数。当提供两个整数fromcount时,Xunit将创建一个测试用例,其中参数等于from,并且它将参数的值在count个情况下增加。

在第二个构造函数中,CombinatorialRangeAttribute接受三个整数参数。在生成的用例中,参数值将从第一个整数增加到第二个整数,第三个整数指定增量间隔。

成员生成的值

可以使用CombinatorialMemberDataAttribute通过测试类上的静态成员为单个理论参数生成值。静态成员可以是字段、属性或方法。

这里使用了一个值生成方法

public static IEnumerable<int> GetRange(int start, int count)
{
    return Enumerable.Range(start, count);
}

[Theory, CombinatorialData]
public void CombinatorialMemberDataFromParameterizedMethods(
    [CombinatorialMemberData(nameof(GetRange), 0, 5)] int p1)
{
    Assert.True(true);
}

这里使用了一个值生成属性

public static IEnumerable<int> IntPropertyValues => GetIntMethodValues();

public static IEnumerable<int> GetIntMethodValues()
{
    for (int i = 0; i < 5; i++)
    {
        yield return Random.Next();
    }
}

[Theory, CombinatorialData]
public void CombinatorialMemberDataFromProperties(
    [CombinatorialMemberData(nameof(IntPropertyValues))] int p1)
{
    Assert.True(true);
}

一个值生成字段也有效

public static readonly IEnumerable<int> IntFieldValues = Enumerable.Range(0, 5).Select(_ => Random.Next());

[Theory, CombinatorialData]
public void CombinatorialMemberDataFromFields(
    [CombinatorialMemberData(nameof(IntFieldValues))] int p2)
{
    Assert.True(true);
}

随机生成的值

CombinatorialRandomDataAttribute可以应用于理论参数,以生成随机整数值。最小值、最大值和值数量都可以通过命名参数设置。

[Theory, CombinatorialData]
public void CombinatorialRandomValuesCount(
    [CombinatorialRandomData(Count = 10)] int p1)
{
    Assert.InRange(p1, 0, int.MaxValue);
}

[Theory, CombinatorialData]
public void CombinatorialRandomValuesCountMinMaxValues(
    [CombinatorialRandomData(Count = 10, Minimum = -20, Maximum = -5)] int p1)
{
    Assert.InRange(p1, -20, -5);
}
版本 兼容和额外的计算目标框架版本。
.NET net5.0 已计算。 net5.0-windows 已计算。 net6.0 已计算。 net6.0-android 已计算。 net6.0_ios 已计算。 net6.0-maccatalyst 已计算。 net6.0-macos 已计算。 net6.0-tvos 已计算。 net6.0-windows 已计算。 net7.0 已计算。 net7.0-android 已计算。 net7.0-ios 已计算。 net7.0-maccatalyst 已计算。 net7.0-macos 已计算。 net7.0-tvos 已计算。 net7.0-windows 已计算。 net8.0 已计算。 net8.0-android 已计算。 net8.0-browser 已计算。 net8.0-ios 已计算。 net8.0-maccatalyst 已计算。 net8.0-macos 已计算。 net8.0-tvos 已计算。 net8.0-windows 已计算。
.NET Core netcoreapp2.0 已计算。 netcoreapp2.1 已计算。 netcoreapp2.2 已计算。 netcoreapp3.0 已计算。 netcoreapp3.1 已计算。
.NET Standard netstandard2.0 支持。 netstandard2.1 已计算。
.NET Framework net461 已计算。 net462 兼容。 net463 已计算。 net47 已计算。 net471 已计算。 net472 已计算。 net48 已计算。 net481 已计算。
MonoAndroid monoandroid 已计算。
MonoMac monomac 已计算。
MonoTouch monotouch 已计算。
Tizen tizen40 已计算。 tizen60 已计算。
Xamarin.iOS xamarinios 已计算。
Xamarin.Mac xamarinmac 已计算。
Xamarin.TVOS xamarintvos 已计算。
Xamarin.WatchOS xamarinwatchos 已计算。
兼容的目标框架
包含的目标框架(在包中)
了解有关目标框架.NET Standard的更多信息。

NuGet包

该包未由任何NuGet包使用。

GitHub仓库 (36)

显示依赖Xunit.Combinatorial的前5个最受欢迎的GitHub仓库

仓库 星标
dotnet/roslyn
The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
dotnet/machinelearning
ML.NET is an open source and cross-platform machine learning framework for .NET.
DotNetAnalyzers/StyleCopAnalyzers
An implementation of StyleCop rules using the .NET Compiler Platform
dotnet/sdk
Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
dotnet/extensions
This repository contains a suite of libraries that provide facilities commonly needed when creating production-ready applications.
版本 下载 上次更新
1.6.24 526,022 8/29/2023
1.6.23-alpha 447 7/18/2023
1.6.12-alpha 4,483 11/9/2022
1.5.25 530,883 10/11/2022
1.5.7-beta 21,374 10/26/2021
1.5.2-beta 6,484 6/22/2021
1.4.1 981,082 8/3/2020
1.3.2 164,430 4/9/2020
1.2.7 635,063 7/16/2017
1.2.1 21,170 3/24/2017
1.1.20 12,389 5/15/2016
1.1.12 3,469 12/6/2015
1.1.12-g4ed6e8f5d0 839 12/6/2015
1.1.3 24,102 11/9/2015
1.0.15200-beta 1,705 7/19/2015
1.0.15199-beta2 1,251 7/19/2015
1.0.15199-beta 933 7/19/2015
1.0.0-beta-gee827b2da1 957 7/19/2015