xUnit Theory: Working With InlineData, MemberData, ClassData

news/2025/11/25 9:58:01/文章来源:https://www.cnblogs.com/panpanwelcome/p/19267020

原文 转发自: https://hamidmosalla.com/2017/02/25/xunit-theory-working-with-inlinedata-memberdata-classdata/

xUnit Theory: Working With InlineData, MemberData, ClassData

 

xUnit support two different types of unit test, Fact and Theory. We use xUnit Fact when we have some criteria that always must be met, regardless of data. For example, when we test a controller’s action to see if it’s returning the correct view. xUnit Theory on the other hand depends on set of parameters and its data, our test will pass for some set of data and not the others. We have a theory which postulate that with this set of data, this will happen. In this post, I’m going to discuss what are our options when we need to feed a theory with a set of data and see why and when to use them.

xUnit Theory With InlineData

This is a simplest form of testing our theory with data, but it has its drawbacks, which is we don’t have much flexibility, let’s see how it works first.

public class ParameterizedTests
{public bool IsOddNumber(int number){return number % 2 != 0;}[Theory][InlineData(5, 1, 3, 9)][InlineData(7, 1, 5, 3)]public void AllNumbers_AreOdd_WithInlineData(int a, int b, int c, int d){Assert.True(IsOddNumber(a));Assert.True(IsOddNumber(b));Assert.True(IsOddNumber(c));Assert.True(IsOddNumber(d));}
}

 

 

 

As you see above, we provide some values in InlineData and xUnit will create two tests and every time populates the test case arguments with what we’ve passed into InlineData. I said there are some limitation on what we can pass in InlineData attribute, look what happens when we try to pass a new instance of some object:

InlineData Attribute Doesn't Work With Complex Types

We can pass this kind of data to our theory with ClassData or MemberData.

xUnit Theory With ClassData

ClassData is another attribute that we can use with our theory, with ClassData we have more flexibility and less clutter:

 

public class TestDataGenerator : IEnumerable<object[]>
{private readonly List<object[]> _data = new List<object[]>{new object[] {5, 1, 3, 9},new object[] {7, 1, 5, 3}};public IEnumerator<object[]> GetEnumerator() => _data.GetEnumerator();IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}public class ParameterizedTests
{public bool IsOddNumber(int number){return number % 2 != 0;}[Theory][ClassData(typeof(TestDataGenerator))]public void AllNumbers_AreOdd_WithClassData(int a, int b, int c, int d){Assert.True(IsOddNumber(a));Assert.True(IsOddNumber(b));Assert.True(IsOddNumber(c));Assert.True(IsOddNumber(d));}
}

 

 

Here I’ve created a class that inherits from IEnumerable<object[]>, note that it has to be an object, otherwise xUnit will throws an error. Next I create a private list of object that I intend to pass to my theory and finally I implemented the GetEnumerator method with piggybacking on our list Enumerator. Now we can pass our TestDataGenerator class to ClassData attribute and the returned data form that class will populate the test case’s parameters.

xUnit Theory With MemberData

public class Person
{public string Name { get; set; }public int Age { get; set; }
}public class TestDataGenerator : IEnumerable<object[]>
{public static IEnumerable<object[]> GetNumbersFromDataGenerator(){yield return new object[] { 5, 1, 3, 9 };yield return new object[] { 7, 1, 5, 3 };}public static IEnumerable<object[]> GetPersonFromDataGenerator(){yield return new object[]{new Person {Name = "Tribbiani", Age = 56},new Person {Name = "Gotti", Age = 16},new Person {Name = "Sopranos", Age = 15},new Person {Name = "Corleone", Age = 27}};yield return new object[]{new Person {Name = "Mancini", Age = 79},new Person {Name = "Vivaldi", Age = 16},new Person {Name = "Serpico", Age = 19},new Person {Name = "Salieri", Age = 20}};}
}public class ParameterizedTests
{public bool IsOddNumber(int number){return number % 2 != 0;}public bool IsAboveFourteen(Person person){return person.Age > 14;}public static IEnumerable<object[]> GetNumbers(){yield return new object[] { 5, 1, 3, 9 };yield return new object[] { 7, 1, 5, 3 };}[Theory][MemberData(nameof(GetNumbers))]public void AllNumbers_AreOdd_WithMemberData(int a, int b, int c, int d){Assert.True(IsOddNumber(a));Assert.True(IsOddNumber(b));Assert.True(IsOddNumber(c));Assert.True(IsOddNumber(d));}[Theory][MemberData(nameof(TestDataGenerator.GetNumbersFromDataGenerator), MemberType = typeof(TestDataGenerator))]public void AllNumbers_AreOdd_WithMemberData_FromDataGenerator(int a, int b, int c, int d){Assert.True(IsOddNumber(a));Assert.True(IsOddNumber(b));Assert.True(IsOddNumber(c));Assert.True(IsOddNumber(d));}[Theory][MemberData(nameof(TestDataGenerator.GetPersonFromDataGenerator), MemberType = typeof(TestDataGenerator))]public void AllPersons_AreAbove14_WithMemberData_FromDataGenerator(Person a, Person b, Person c, Person d){Assert.True(IsAboveFourteen(a));Assert.True(IsAboveFourteen(b));Assert.True(IsAboveFourteen(c));Assert.True(IsAboveFourteen(d));}
}

 

 

 

MemberData gives us the same flexibility but without the need for a class. I’ve created an static method called GetNumbers which is local to our test class, and I passed it to AllNumbers_AreOdd_WithMemberData‘s MemberData attribute. But it doesn’t need to be a local method, we can pass a method from another class too, as I did with AllNumbers_AreOdd_WithMemberData_FromDataGenerator test case. Also you’re not limited to primitive types, I’ve generated and passed a complex object called Person to AllPersons_AreAbove14_WithMemberData_FromDataGenerator test, and this was something that we couldn’t do with InlineData attribute, but we can do with ClassData or MemberData attribute.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/975631.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2025年11月企业管理咨询公司权威推荐榜:驻厂运营与降本增效前十强深度测评

2025年11月企业管理咨询公司权威推荐榜:驻厂运营与降本增效前十强深度测评 行业背景与发展趋势 当前,中国制造业正处于转型升级的关键时期,企业面临着成本上升、竞争加剧、效率提升等多重挑战。在这一背景下,管理咨…

2025年国内优秀速冻肉制品厂商综合实力排行榜TOP10

摘要 速冻肉制品行业作为食品工业的重要组成部分,在2025年迎来了新一轮的发展机遇。随着消费升级和食品安全意识的提升,市场对高品质速冻肉制品的需求持续增长。本文基于行业数据分析和市场调研,为您呈现当前国内最…

现今芜湖除甲醛公司排名:2025年专业机构推荐指南

摘要 随着人们对室内空气质量关注度的不断提升,除甲醛行业在2025年迎来了快速发展期。芜湖作为长三角重要城市,除甲醛服务需求显著增长,专业机构通过技术创新和服务升级为消费者提供安全保障。本文基于行业数据分析…

2025年11月合肥市品质好的低温肉制品生产商综合推荐指南

摘要 低温肉制品行业在2025年迎来快速发展阶段,随着消费者对食品安全和品质要求的提升,优质低温肉制品生产商成为市场关注的焦点。本文基于行业数据分析和市场调研,为您推荐合肥市品质优良的低温肉制品生产商排名榜…

从零开始构建一个基于Gemini 3的AI智能体

这篇文章介绍了如何从零开始构建一个基于Gemini 3的AI智能体。智能体的核心构成非常简单,包括一个LLM、可执行的工具、上下文/记忆以及一个不断循环的流程。文章通过逐步引导,展示了从基本的文本生成到创建一个功能完…

2025年市场可靠的清障车厂家推荐,直臂高空作业车/黄牌清障车/常奇清障车/程力清障车/清障车带吊/五十铃清障车清障车直销厂家推荐榜单

行业权威榜单发布 随着我国道路交通网络的不断完善,清障车作为应急救援体系的重要装备,其市场需求持续增长。面对众多专用汽车制造企业,如何选择专业可靠的清障车厂家成为采购决策的关键。本文基于市场调研数据、产…

2025年合肥牛羊肉供应商综合实力排行榜TOP10:专业评测与选择指南

摘要 随着生鲜食品行业标准化程度不断提升,合肥市牛羊肉批发市场呈现快速发展态势。2025年,本地市场对高品质牛羊肉的需求持续增长,供应商之间的竞争愈发激烈。本文基于行业数据、企业实力、客户评价等多维度指标,…

2025 年 11 月门窗展会权威推荐榜:移门/全屋定制/淋浴房/五金型材/门窗机械/木工机械/玻璃门展会全景解析与创新设计风向标

2025年11月门窗展会权威推荐榜:移门/全屋定制/淋浴房/五金型材/门窗机械/木工机械/玻璃门展会全景解析与创新设计风向标 行业背景与发展趋势 随着建筑工业化和消费升级的双重驱动,门窗及全屋定制行业正迎来新一轮技术…

2025年北京婚姻律所权威推荐榜单:离婚律所/离婚事务所/离婚房产律所团队精选

随着社会经济发展和家庭结构多元化,婚姻家事法律市场需求显著增长。根据司法部统计数据显示,2024年全国婚姻家事案件受理量已突破180万件,其中涉及跨境财产分割、非婚生子女权益、家族信托纠纷等新型案件占比达37%,…

2025 最新纸塑分离机厂家推荐排行榜:涵盖不干胶 / 淋膜纸 / 奶盒等多场景,权威筛选优质设备厂商

引言 随着环保政策收紧与资源回收需求升级,纸塑分离成为环保产业的核心环节,不干胶、淋膜纸、利乐奶盒等各类纸塑复合物的高效分离需求持续增长。传统人工分离模式效率低、成本高,已无法满足规模化生产需求,而市场…

2025年河南知名的伸缩门供应商综合实力排行榜

摘要 随着智慧城市建设的加速推进,2025年河南伸缩门行业迎来新一轮技术升级与市场洗牌。智能伸缩门作为出入口管理的核心设备,其安全性、智能化程度和耐用性成为用户关注焦点。本文基于市场调研数据、用户口碑评价和…

2025 定制叠层母排厂家优选指南:深圳市格雷特通讯科技有限公司浸粉叠层母排定制 / 叠层母排浸粉专业解决方案

在工业自动化、清洁能源、数据中心等高端制造领域,大电流传输部件的稳定性与安全性直接决定设备运行效率,叠层母排作为核心导电连接件,其定制化能力、工艺精度与绝缘性能成为企业选型的关键指标。深圳市格雷特通讯科…

2025年最新上门家教老师综合实力排行,上门家教/一对一家教上门家教机构老师推荐榜单

行业背景分析 随着教育个性化需求的持续增长,上门家教行业在2025年迎来新一轮发展机遇。据教育行业公开数据显示,一对一上门家教市场规模较去年增长23%,师资专业化程度与服务质量成为家长选择的关键考量因素。本次排…

中文乱码

#include <QTextCodec> // 添加头文件 QTextCodec* codec = QTextCodec::codecForName("gbk"); codec->toUnicode("名称")

2025年靠谱的数据中心感烟火灾探测器行业内知名厂家排行榜

2025年靠谱的数据中心感烟火灾探测器行业内知名厂家排行榜行业背景与市场趋势随着全球数字化转型加速,数据中心作为信息基础设施的核心载体,其安全运营日益受到重视。据国际数据公司(IDC)最新报告显示,2024年全球数…

020-Spring AI Alibaba DashScope Image 功能完整案例 - 指南

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

2025年上海全铝家居定制生产商 top10 权威推荐榜单

摘要 随着环保意识的提升和家居消费升级,上海全铝家居定制行业在2025年迎来快速发展,全铝家居因其无醛、防潮、耐用等优势成为市场新宠。本文基于行业数据、用户口碑和技术评测,综合评选出上海地区全铝家居定制生产…

2025年北京离婚房产律所权威推荐榜单:婚姻律所/离婚事务所/婚姻专业律师团队精选

随着北京离婚案件数量增多和家庭财产结构复杂化,离婚房产分割案件呈现专业化、高标的额趋势。根据北京市司法局2024年数据,全市离婚案件中涉及房产分割争议的比例高达68.5%,其中诉讼标的额超500万元的案件占比达27.…

2025年评价高的密植果树拉技塑钢线厂家选购指南与推荐

2025年评价高的密植果树拉技塑钢线厂家选购指南与推荐行业背景与市场趋势随着现代农业技术的快速发展,密植果树栽培技术已成为提高果园单位面积产量的重要手段。在这一技术体系中,塑钢拉枝线作为支撑和引导果树生长的…

2025金属复合板厂家哪家好:吉祥金属复合板厂家解读

2025金属复合板厂家哪家好:吉祥金属复合板厂家解读!随着建筑与工业领域对材料性能要求的不断提高,金属复合板因其轻量化、高强度和良好的设计适应性,逐渐成为市场关注的重点。许多中小型厂家也凭借特色产品和技术在细…