Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit DataSource from a different class

I am trying to make a test project using N Unit. I have everything working and now but i was wondering if it is possible to split the test cases into a completely separate class or would the only feasible way would be to push the data to a file or a partial class? basically I would like the data in a separate file instead of having all the data and the tests in one file. Or maybe that more of the standard and create different classes for each rule test.

[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 = "" };
    }
like image 779
Jamie Babineau Avatar asked Sep 01 '25 16:09

Jamie Babineau


1 Answers

I believe NUnits TestCaseData will solve your problem:

[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 = "" });
    }
}
like image 72
nozzleman Avatar answered Sep 04 '25 05:09

nozzleman