ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CodeGo.net>如何在NUnit测试用例中传递字符串和字典?

2019-11-09 02:08:24  阅读:246  来源: 互联网

标签:unit-testing nunit testing c


我想对我的方法进行测试,并且可以传递2个字符串变量,但是我不知道如何传递Dictionary<,>.

看起来像这样:

[Test]
[TestCase("agr1", "askdwskdls", Dictionary<TypeMeasurement,double>)]
public void SendDataToAgregator_GoodVariables_ReturnsOn(string agrID,string devID, Dictionary<TypeMeasurement, double> measurement)
{

}

TypeMeasurement是枚举,我知道这不是您传递字典的方式,但是我不知道如何传递它,所以我将其放在那里,以便您知道我想做什么.

解决方法:

如果您有复杂的数据要用作测试用例,请使用TestCaseSourceAttribute代替TestCaseAttribute

TestCaseSourceAttribute is used on a parameterized test method to identify the property, method or field that will provide the required arguments

您可以使用以下构造函数之一:

TestCaseSourceAttribute(Type sourceType, string sourceName);
TestCaseSourceAttribute(string sourceName);

这是从documentation开始的移植:

If sourceType is specified, it represents the class that provides the
test cases. It must have a default constructor.

If sourceType is not specified, the class containing the test method
is used. NUnit will construct it using either the default constructor
or – if arguments are provided – the appropriate constructor for those
arguments.

因此,您可以像下面这样使用它:

[Test]
[TestCaseSource(nameof(MySourceMethod))]
public void SendDataToAgregator_GoodVariables_ReturnsOn(string agrID,string devID, Dictionary<TypeMeasurement, double> measurement)
{

}

static IEnumerable<object[]> MySourceMethod()
{
    var measurement = new Dictionary<TypeMeasurement, double>();
    // Do what you want with your dictionary

    // The order of element in the object my be the same expected by your test method
    return new[] { new object[] { "agr1", "askdwskdls", measurement }, };
};

标签:unit-testing,nunit,testing,c
来源: https://codeday.me/bug/20191109/2010894.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有