Getting Started

  1. Step 1: Create your dataset file
  2. Step 2: Create your test class
  3. Step 3: Implement your [Test] methods

Getting Started for release (alpha3)

Step 1: Create your dataset file

Your tests need some data to work with. DbUnit.NET handles this by inserting the data you provide within a "dataset". This "dataset" is an xml file that specifies the tables and columns, along with the latters values, that should be created within the database.

The xml file is what is termed a "flat" XML in that each child element to the files main element (which can be anything but <dataset> is suggested) specifies a single insert statement to be be executed. The elements name is the table name and the elements attributes specify the column names and values to be used.

The following is a sample of a flat XML dataset that specifies 2 inserts into the 'people' table:

<?xml version="1.0" encoding="utf-8" ?>
	<people id="1" given_names="Jo-Ann" surname="Smith" gender="F"/>
	<people id="2" given_names="Mary Jane" surname="Jones" gender="F"/>

This dataset should be placed in the same directory as the test class by which it will be used and should be named YourClassNameDataSet.xml. It must also be included in the generated assembly as an Embedded Resource such that it is embedded within the same namespace as the test class.

Step 2: Create your test class

Create an NUnit [TestFixture] class that extends DbUnitTestCase and provides the following:

  • A [TestFixtureSetUp] method that initializes the connection to the database, the PrimaryKeyFilter to use on tear down, and the IDatabaseHandler to use to insert/delete the actual data
  • A [SetUp] method that calls super.SetUp() and initializes your own test accordingly
  • A [TearDown] method that calls super.TearDown()
  • A [TestFixtureTearDown] method that closes the database connection

The PrimaryKeyFilter must be loaded with the details of all primary keys of all the tables specified in the dataset. In the example given the primary key is the "id" column (see the SetUpFixture() method in the code sample below).

The following is an example:

using System;
using System.Data;
using System.Data.SQLite;
using DbUnit.Framework;
using NUnit.Framework;

namespace Samples.Tests
	public class SampleTest : DbUnitTestCase
		private IDbConnection connection = null;

		public void SetUpFixture()
			connection = new SQLiteConnection("Data Source=sample.db");

			PrimaryKeyFilter pkFilter = new PrimaryKeyFilter();
			pkFilter.Add("people", new PrimaryKey("id"));
			base.PKFilter = pkFilter;

			base.DatabaseHandler = new GenericDatabaseHandler(this.connection);

		public void TearDownFixture()

		public void SetUpTestCase()

		public void TearDownTestCase()

Step 3: Implement your [Test] methods

Implement your [Test] methods as you normally would for NUnit. Your database is now initialized before and cleaned-up after each test method according to what you did in the previous steps.