Tuesday, June 10, 2008

Implement your own NUnit TestRunner - Part 1

I wanted to be able to run NUnit unit tests from my own test runner within a WinForms App. Easier said than done, not a lot of examples out there. I looked through the NUnit-Console code and the NUnit-Gui code and pieced together bits and pieces. Then I also stumbled across this blog post from a fellow german which helped with the last stumbling block:
http://achblah.blogspot.com/2007/04/monocov-and-my-contribution-to-it.html

The end result is this piece of VB.net code that will run any tests within your current EXE:

Imports System.Reflection
Imports NUnit.Core
Imports NUnit.Util


Public Class Form1

  Private Sub btnRunTests_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRunTests.Click

    Dim objTestRunner As TestRunner = Nothing

    objTestRunner = New RemoteTestRunner()

    Dim strMyFileName As String = Assembly.GetExecutingAssembly().Location
    Dim objPackage As TestPackage = New TestPackage(strMyFileName)

    objTestRunner.Load(objPackage)

    objTestRunner.Run(New NullListener)



  End Sub
End Class



Of course this does not show any test results or exceptions because of using the "NullListener". In Part 2 I will fix that.

No comments: