举例说明 .Net Core 单元测试中 xUnit 的 [Theory] 属性的用法

在这篇文章中,我们探讨了如何使用 xUnit 的 [Theory] 属性来运行参数化测试。通过示例展示了如何使用 [InlineData]、[MemberData] 和 [ClassData] 提供不同的数据源,从而简化测试代码并提高测试覆盖率。这些方法有助于在 .NET 应用程序中进行更有效的单元测试。

示例 1:使用 [InlineData]

这是直接在属性中提供数据的最简单方法。

csharp 复制代码
using Xunit;

public class MathTests
{
    [Theory]
    [InlineData(1, 1, 2)]
    [InlineData(2, 3, 5)]
    [InlineData(10, -5, 5)]
    public void Add_ReturnsCorrectSum(int a, int b, int expectedSum)
    {
        // Arrange
        var math = new Math();

        // Act
        var result = math.Add(a, b);

        // Assert
        Assert.Equal(expectedSum, result);
    }
}

public class Math
{
    public int Add(int x, int y) => x + y;
}

示例 2:使用 [MemberData]

这允许你引用返回 IEnumerable<object[]> 的方法或属性。

csharp 复制代码
using Xunit;
using System.Collections.Generic;

public class MathTests
{
    public static IEnumerable<object[]> AddData =>
        new List<object[]>
        {
            new object[] { 1, 1, 2 },
            new object[] { 2, 3, 5 },
            new object[] { 10, -5, 5 }
        };

    [Theory]
    [MemberData(nameof(AddData))]
    public void Add_ReturnsCorrectSum(int a, int b, int expectedSum)
    {
        // Arrange
        var math = new Math();

        // Act
        var result = math.Add(a, b);

        // Assert
        Assert.Equal(expectedSum, result);
    }
}

public class Math
{
    public int Add(int x, int y) => x + y;
}

示例 3:使用 [ClassData]

这对于更复杂的数据设置非常有用。你可以创建一个实现 IEnumerable<object[]> 的类。

csharp 复制代码
using Xunit;
using System.Collections;
using System.Collections.Generic;

public class MathTests
{
    [Theory]
    [ClassData(typeof(AddTestData))]
    public void Add_ReturnsCorrectSum(int a, int b, int expectedSum)
    {
        // Arrange
        var math = new Math();

        // Act
        var result = math.Add(a, b);

        // Assert
        Assert.Equal(expectedSum, result);
    }
}

public class AddTestData : IEnumerable<object[]>
{
    public IEnumerator<object[]> GetEnumerator()
    {
        yield return new object[] { 1, 1, 2 };
        yield return new object[] { 2, 3, 5 };
        yield return new object[] { 10, -5, 5 };
    }

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

public class Math
{
    public int Add(int x, int y) => x + y;
}

总结

通过本文的介绍,我们了解了如何使用 xUnit 的 [Theory] 属性来进行参数化测试。无论是使用 [InlineData] 直接提供数据,还是通过 [MemberData] 和 [ClassData] 引用外部数据源,这些方法都能帮助我们编写更灵活和高效的单元测试。掌握这些技巧,可以显著提升 .NET 应用程序的测试覆盖率和代码质量,为开发过程带来更多便利和保障。

相关推荐
2302_805771071 天前
Day24:DDT+EXCEL数据驱动
功能测试·单元测试·自动化·测试用例·需求分析·web自动化
孟陬2 天前
不用盲调性能参数 Vitest 5.0 新推出 `vitest doctor` 一键诊断测试慢卡点,自动给出最优提速配置
性能优化·单元测试·vite
上位机妹子4 天前
EF Core 两种继承映射:每层次一张表(TPH)与每类型一张表(TPT)
数据库·c#·.netcore·ef core
网络毒刘6 天前
端到端:用 Cursor Agent 完成「小功能 + 单测 + PR 描述」并附人工验收清单
单元测试·agent·ai编程·cursor·工具实践
mont evergreen6 天前
RTMP 连接单元测试之旅
单元测试·音视频·源码解读
我就是不信7 天前
CuTest:轻量级 C 语言单元测试框架实战指南
c语言·单元测试·log4j
虫无涯11 天前
Coverity 如何结合 GJB8114-2013使用?
git·单元测试·嵌入式测试·coverity·静态扫描
茉莉玫瑰花茶13 天前
GO [ 单元测试 ]
golang·单元测试·log4j
测试员周周14 天前
【jev应用到测试】94 条 UI 报错交给 Jev,三档判对 85.1%,开单闸门定在 0.6
人工智能·python·测试工具·单元测试·测试用例
ebiobiz15 天前
STM32F4 Unity 单元测试与多次测试失败率统计详细步骤
stm32·unity·单元测试