NUnitExtension.DataDriven – an NUnit AddIn for data-driven testing

DataDriven Extension is an NUnit Addin that allows you to create test cases from any data sources (like database or plain text files etc.) and can used for parameterized test.

DataFactoryAttibute (like the FactoryAttribute in NUnit 2.5 alpha 3), indicate class of the test case factory. You can also set some arguments for creating test cases.

Test case factory class must have a defualt constructor and a method with IEnumerable as the return value for create test cases. And this method should be marked by the DataSourceAttribute (like the TestCaseFactory in NUnit 2.5 alpha 3).

TestCaseItem (like the TestCaseData in NUnit 2.5 alpha 3) is a constraint-oriented test case task class. You can set the arguments for parameterized test method or normal test method and set some constraints to check the return value or expected exception throwed by the test method.

You can download here:

NUnitExtension.DataDriven_0.1_alpha.zip

To get the source code or more information, please visit the project site:

http://code.google.com/p/nunit-extension-datadriven/

Request

  • NUnit 2.5 alpha 3 (May be compiled against other NUnit versions)
  • .NET Framework 2.0 (Not tested under .NET Framework 1.1 and mono)

Install

  1. Copy the NUnitExtension.DataDriven.AddIn.dll to “…\NUnit 2.5\net-2.0\addins\”.
  2. Add reference to NUnitExtension.DataDriven.dll and write your own data-driven tests.

Example

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using NUnit.Framework.Syntax.CSharp;
using NUnitExtension.DataDriven;

[TestFixture]
public class DataDrivenTest
{
    Random random = new Random();

    [DataFactory(typeof(RandomFactory), 10)]
    public double Add(double a, double b)
    {
        return a + b;
    }

    [DataFactory(typeof(RangeFactory))]
    public double NextDouble()
    {
        return random.NextDouble();
    }

    [DataFactory(typeof(ExceptionFactory), typeof(int))]
    public void ThrowException(Type exceptionType)
    {
        if (typeof(Exception).IsAssignableFrom(exceptionType))
            if (exceptionType.GetConstructor(new Type[0]) != null)
            {
                throw (Exception) Activator.CreateInstance(exceptionType);
            }
    }
}

public class RandomFactory
{
    [DataSource]
    public IEnumerable GetTestCases(int count)
    {
        Random random = new Random();
        for(int i = 0; i

Tags: , , , , , , ,

One Response to “NUnitExtension.DataDriven – an NUnit AddIn for data-driven testing”

  1. rene Says:

    Hi,

    i tried your sample with nunit 2.5.5.10112 and results are:

    Tests run: 0, Errors: 0, Failures: 0, Inconclusive: 0, Time: 0,01 seconds
    Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0

    Am I missing something? How do I use it?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: