I wrote my own NUnit test runner in VB .Net based on some of the following links:
Implement your own NUnit TestRunner - Part 1
Stack Overflow - How to run NUnit programmatically
Stack Overflow - Running NUnit Tests In a C# Console App
My code is a custom MSBuild task running in a Web Application. I have included my code below. It is intended to run all tests in the project and write the results out to an XML file. When I run this, the code seems to be running the tests and is able to write to the XML file, but it appears as though tests are failing as they are missing config settings like ConnectionStrings. When I run the same tests in this project through the NUnit GUI app, they work successfully, so I know that I have my config file in correct location for the GUI app. I'm assuming I need to either put my config file somewhere else or I need to set something else on my TestPackage for my code to properly pick up my config file. Any ideas?
Code:
Public Class RunTestTask
Inherits Task
Public Overrides Function Execute() As Boolean
Dim MyFileName As String = Assembly.GetExecutingAssembly().Location
Dim package As TestPackage = New TestPackage(MyFileName)
Dim runner As RemoteTestRunner = New RemoteTestRunner()
runner.Load(package)
Dim result As TestResult = runner.Run(New NullListener)
Dim writer As XmlResultWriter = New XmlResultWriter("c:\inetpub\wwwroot\webapp\NUnitLibrary\Test.xml")
writer.SaveTestResult(result)
Return True
End Function
Test.xml excerpt:
<test-suite type="TestFixture" name="CareerFairListTests" executed="True" result="Failure" success="False" time="0.000" asserts="0">
<results>
<test-case name="NUnitLibrary.CareerFairListTests.GetFairs" executed="True" result="Error" success="False" time="0.000" asserts="0">
<failure>
<message><![CDATA[System.NullReferenceException : Object reference not set to an instance of an object.]]></message>
<stack-trace><![CDATA[at Business.CareerFairList.GetFairs() in C:\Inetpub\wwwroot\webapp\Business\HR\CareerFairList.vb:line 12
at NUnitLibrary.CareerFairListTests.GetFairs() in C:\Inetpub\wwwroot\webapp\NUnitLibrary\Tests\HR\CareerFairListTests.vb:line 28
]]></stack-trace>
</failure>
</test-case>
</results>
</test-suite>