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

相关推荐
云动雨颤13 小时前
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
python·单元测试
Suresoft China17 小时前
软件测试|STATIC 代码静态验证工具 C/C++ 工具链设置指南
c++·单元测试·静态测试·测试覆盖率·static·代码覆盖率·工具链设置
somethingGoWay21 小时前
wpf .netcore 导出docx文件
wpf·.netcore
somethingGoWay1 天前
wpf .netcore 导出pdf文件
pdf·wpf·.netcore
itppxie2 天前
Simulink中使用Test sequence单元测试
单元测试
切糕师学AI2 天前
如何建立针对 .NET Core web 程序的线程池的长期监控
java·前端·.netcore
泛联新安3 天前
如何根据项目需求选择合适的软件测试工具?iUnit智能单元测试平台提供专业化解决方案
c++·测试工具·单元测试
csdn_aspnet3 天前
使用 MongoDB.Driver 在 C# .NETCore 中实现 Mongo DB 过滤器
mongodb·c#·.netcore
EndingCoder3 天前
单元测试:Jest 与 Electron 的结合
javascript·electron·单元测试·前端框架
tiancao2223 天前
安装3DS MAX 2026后,无法运行,提示缺少.net core的解决方案
.net·.netcore·3dsmax