c# - 来自不同类的 NUnit DataSource

标签 c# unit-testing nunit

我正在尝试使用 N Unit 制作一个测试项目。我现在一切正常,但我想知道是否可以将测试用例拆分为一个完全独立的类,或者唯一可行的方法是将数据推送到文件或部分类?基本上我希望将数据放在一个单独的文件中,而不是将所有数据和测试放在一个文件中。或者可能是更多的标准并为每个规则测试创建不同的类。

[Test, TestCaseSource("TenantsRules")]
    public void CheckDNQTenantsRules(DNQTenantData testData)
    {
        CoreServicesBroker.DNQCoreServiceBroker broker = new CoreServicesBroker.DNQCoreServiceBroker();
        string actualDNQReason = string.Empty;

        int actualReturnCode = broker.CheckDNQTenants(testData.FormCode, testData.StateCode, testData.EffectiveDate, testData.NumberOfTenants, ref actualDNQReason);

        Assert.AreEqual(testData.ExpectedDNQReturnCode, actualReturnCode);
        Assert.AreEqual(testData.ExpectedDNQReason, actualDNQReason);
    }

public static IEnumerable<DNQTenantData> TenantsRules()
    {
        yield return new DNQTenantData() { FormCode = 9, StateCode = "OH", EffectiveDate = DateTime.Now, NumberOfTenants = 7, ExpectedDNQReturnCode = 1, ExpectedDNQReason = "Number of Tenants exceeded." };
        yield return new DNQTenantData() { FormCode = 9, StateCode = "OH", EffectiveDate = DateTime.Now, NumberOfTenants = 5, ExpectedDNQReturnCode = 0, ExpectedDNQReason = "" };
    }

最佳答案

我相信 NUnits TestCaseData将解决您的问题:

[TestFixture]
public class YourTest
{
    [Test, TestCaseSource(typeof(YourTestCaseProvider), nameof(YourTestCaseProvider.TenantsRules)]
    public void CheckDNQTenantsRules(DNQTenantData testData)
    {
        CoreServicesBroker.DNQCoreServiceBroker broker = new CoreServicesBroker.DNQCoreServiceBroker();
        string actualDNQReason = string.Empty;

        int actualReturnCode = broker.CheckDNQTenants(testData.FormCode, testData.StateCode, testData.EffectiveDate, testData.NumberOfTenants, ref actualDNQReason);

        Assert.AreEqual(testData.ExpectedDNQReturnCode, actualReturnCode);
        Assert.AreEqual(testData.ExpectedDNQReason, actualDNQReason);
    }
}

public class YourTestCaseProvider
{
    public static IEnumerable TenantsRules()
    {
        yield return new TestCaseData(new DNQTenantData() { FormCode = 9, StateCode = "OH", EffectiveDate = DateTime.Now, NumberOfTenants = 7, ExpectedDNQReturnCode = 1, ExpectedDNQReason = "Number of Tenants exceeded." })
        yield return new TestCaseData(new DNQTenantData() { FormCode = 9, StateCode = "OH", EffectiveDate = DateTime.Now, NumberOfTenants = 5, ExpectedDNQReturnCode = 0, ExpectedDNQReason = "" });
    }
}

关于c# - 来自不同类的 NUnit DataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41142576/

相关文章:

c# - 如何避免使用多个构造函数重复代码?

c# - 如何获取 XmlElement 的字符位置?

c# - 基于动态资源的样式

c# - CSV 提供的数据源第一列中的奇数字符

c# - 流利的断言 : equivalence of sorted lists

linux - 从Visual Studio在Linux docker容器中调试nunit测试

c# - 如何使用两种不同形式的相同方法?

perl - 有没有一种简单的方法可以为 perl 测试设置一个本地的、独立的 Web 服务器?

symfony - 在单元测试中使用 FOS OAuth Bundle 授权 API 请求

c# - 使用最小起订量模拟第三方回调事件