NDbUnit is a .NET library, written in C#, which facilitates programmatically placing a database into a known state. This framework can be used to increase repeatability within developer tests that interact with a database by ensuring that the database’s state is consistent across the execution of tests.
Taking advantage of NDbUnit in VB.NET is a breeze, especially when combined with NUnit. For example, to use NDbUnit, all that’s required is:
- an XML seed file
- a corresponding schema file
- a database connection string
Using those three pieces of data, one can initialize NDbUnit via NUnit’s fixture mechanism:
<SetUp()> Public Sub init()
Me.fixture = New OleDbUnitTest(connection)
Me.fixture.ReadXmlSchema(schemaPath)
Me.fixture.ReadXml(xmlFilePath)
End Sub
Now tests can be written that depend on the data found in the XML seed file. For instance, the XML seed file defines the word pugnacious:
<word>
<WORD_ID>2</WORD_ID>
<SPELLING>pugnacious</SPELLING>
<PART_OF_SPEECH>adj</PART_OF_SPEECH>
</word>
<definition>
<DEFINITION_ID>1</DEFINITION_ID>
<WORD_ID>2</WORD_ID>
<DEFINITION>Combative in nature, belligerent.</DEFINITION>
</definition>
<synonym>
<SYNONYM_ID>1</SYNONYM_ID>
<WORD_ID>2</WORD_ID>
<SPELLING>belligerent</SPELLING>
</synonym>
To verify NDbUnit’s functionality, the following test case executes a CleanInsert operation (i.e. the data in the XML file is inserted into the database after each targeted table is cleared). After the CleanInsert, a different database connection is established and pugnacious is retrieved from the database and verified.
<Test()> Public Sub verifyWordInTable()
Me.fixture.PerformDbOperation(DbOperationFlag.CleanInsert)
Dim stmt As String =
"select spelling from word where word.word_id = 2"
Dim adapter As New OleDbDataAdapter(stmt, connection)
Dim dset As New DataSet
adapter.Fill(dset, "word")
Dim table As DataTable = dset.Tables("word")
Dim trow As DataRow
For Each trow In table.Rows
Assert.AreEqual("pugnacious", trow(0),
"word spelling is not pugnacious")
Next
End Sub
Note, NDbUnit isn’t a testing framework- it’s a database seeding utility to be utilized in concert with one. By allowing the state of the database to be controlled via NDbUnit, test cases can become repeatable, meaning they can run without human intervention in varying environments. For example, tests that utilize NDbUnit are easily run in continuous integration environments, such as CC.NET.
