首页 > 解决方案 > 如何在 C# 中引用 UnitTesting DataSource 中的对象定义?

问题描述

我正在使用 Microsoft UnitTesting 套件 ( Microsoft.VisualStudio.TestTools.UnitTesting;) 编写单元测试。有了这个,我创建了一个对象用作数据源:

private static readonly object[] ValidLogRangesIntSource =
{
    new object[] { 1, 2, 1, new List<int> {1} } ,
    new object[] { int.MaxValue, 1, 1, new List<int> {int.MaxValue} } ,
    new object[] { int.MaxValue, 1, 2, new List<int> {int.MaxValue, 1} } ,
    new object[] { 1, 1000, 10,
        new List<int> { 1, 2, 5, 10, 22, 46, 100, 215, 464, 1000} },
    new object[] { 1000, 1, 10,
        new List<int> { 1, 2, 5, 10, 22, 46, 100, 215, 464, 1000} }
};

我试图在我的单元测试中引用这些:

[TestMethod]
[DataSource("ValidLogRangesIntSource")]
public void TestLogRangeWithValidIntValues(int start, int end, int steps, List<int> result)
{
    // MathUtil.LogRange(start, end, steps).Should().BeEquivalentTo(result);
    CollectionAssert.AreEquivalent(result, MathUtil.LogRange(start, end, steps));
}

但是,运行时,我收到错误消息

在测试配置设置中找不到数据源“ValidLogRangesIntSource”。

调查时,DataSource 的文档告知名称应该在app.config; 但是我没有这个文件。

此外,在 NUnit 中,数据源运行良好[TestCaseSource("ValidLogRangesIntSource")]- 我正在寻找相同的实现。

标签: c#unit-testingmstest

解决方案


推荐阅读