举例说明 .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 应用程序的测试覆盖率和代码质量,为开发过程带来更多便利和保障。

相关推荐
van久4 小时前
Day14: 搭建企业标准的DDD 简洁版四层架构
架构·.netcore
weixin_4331793310 小时前
Python -- 单元测试 unittest
python·单元测试
van久10 小时前
ADay15-1: 安装 EF Core 包 到 对应DDD(领域驱动设计)四层架构‌
.netcore
方安乐1 天前
单元测试之helper函数
前端·javascript·单元测试
测试老哥1 天前
白盒测试用例的设计
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
好大哥呀1 天前
单元测试自动化的流程
运维·单元测试·自动化
时光追逐者1 天前
C#/.NET/.NET Core技术前沿周刊 | 第 69 期(2026年4.01-4.12)
c#·.net·.netcore
ChaITSimpleLove2 天前
软件测试策略全面指南:从单元测试到混沌工程的多维度分析
渗透测试·单元测试·集成测试·压力测试·系统测试·test
willhuo3 天前
基于Playwright的抖音网页自动化浏览器项目使用指南
爬虫·c#·.netcore·webview
csdn_aspnet4 天前
在 .NET Core 8 中实现 CORS
.netcore·跨域·cors·.net8