来自不同类的NUnit DataSource

时间:2016-12-14 12:23:38

标签: 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 = "" };
    }

1 个答案:

答案 0 :(得分:3)

我相信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 = "" });
    }
}