Reference
Exporting TestResults
- IResultExport Interface
- Sample Modules
-
SaveToFile
XML Converter
Logger
- File References
-
Current directory in Start and Finish
Current directory in handlers of TestResult events
- Calling Export Modules
-
Custom parameters
IResultExport Interface
vbUnit can easily be customized to export test results to logfiles and databases, or to convert them into XML. This is done with Export Modules.
An export module should be located inside an ActiveX DLL and needs to implement the interface vbUnit3.IResultExport. This interface defines two methods:
IResultExport
Public Sub Start(ByVal testRunnerInfo As String, ByVal params As String, result As TestResult)
Public Sub Finish(result As TestResult)
The Start method is called just before the tests are run, so at this point there are no entries in the TestResult. The export object may keep a reference to the result to handle its events. An example of this is given in ResultExport.XMLConverter. The testRunnerInfo string gives a short description of the test run that is about to start. Params is a string of custom parameters that may be specified at the command line or in the options dialog (explained below).
The Finish method is called after all tests have finished. At this point the export module should process the completed result, for example to save it to a file or a database.
Sample Modules
The project vbUnit3\ResultExport\ResultExport.vbp contains sample modules that save the last result to a single file (ResultExport.SaveToFile), a logfile that is cumulatively grown (ResultExport.Logger), and an XML file (ResultExport.XMLConverter). These samples have no error handling and are only provided for demonstration purposes. You can use them as a base for your own customized export modules. If you write any new modules that might be of use to other developers as well, don't hesitate to send them to us and they will be put up on this site.
SaveToFile
The most simple example saves the last test result to a file (whose name is indicated by the "params" argument):
'ResultExport.SaveToFile 'simple example of how to save testresults to a file 'this could obviously be a lot more sophisticated and use 'generated filenames, temp folders, etc... Option Explicit Implements IResultExport Private m_fileName As String Private Sub IResultExport_Start(ByVal testRunnerInfo As String, _ ByVal params As String, result As vbUnit3.TestResult) m_fileName = params End Sub Private Sub IResultExport_Finish(result As TestResult) SaveStringToFile m_fileName, result.ToConsoleString End Sub
XML Converter
A slightly more complex example is the XMLConverter, which transforms the last result into an XML file like the following:
<TestResult numRunTests="3" numAssertions="4" numErrors="1" numFailures="3">
<Description>vbUnit3.05.00 - Running TestSuite: Unit Tests of the Sample Export Modules (TestResultExport.MainSuite)</Description>
<Suites count="1">
<Suite numTests="3">Unit Tests of the Sample Export Modules</Suite>
</Suites>
<Errors count="1">
<Error number="11" source="SampleTest.TestError" traceMsg="an error">Division by zero</Error>
</Errors>
<Failures count="3">
<Failure source="SampleTest.TestFail" assertMsg="hello" traceMsg="RunTest">Verify failed</Failure>
<Failure source="SampleTest.TestCompare" assertMsg="cmpLongs" traceMsg="RunTest">expected '5' but was '3'</Failure>
<Failure source="SampleTest.TestCompare" assertMsg="cmpStrings" traceMsg="RunTest">expected 'abc' but was 'xyz'</Failure>
</Failures>
</TestResult>
This module keeps a reference to the result object in the Start method, so that it can handle the events of TestResult, in this case to generate a list of the called suites.
Logger
This module is very similar to the SaveToFile module, but instead of always replacing the last file, it appends all runs to a single logfile and also inserts a timestamp before each entry. Logger accepts the following custom parameters:
ResultExport.Logger fileName [,[clear ] or [maxFileSize:n]]
The clear flag will always delete the logfile if it is set.
maxFileSize:n will ensure that the logfile will not grow larger than n characters.
File References
Current directory in Start and Finish
The current directory will be set to the location of the compiled export module before the calls to the methods Start and Finish. You can safely use relative paths here that refer to this location.
Current directory in handlers of TestResult events
However, while the tests are running, the current directory will be set to the location of the compiled test suites, and might even be changed by the tests. Hence, if your module handles some events of the TestResult, the event handlers should either not make any file access, or be aware that any relative references might be resolved to a different place than in Start and Finish. Further, if your test suites rely on relative file references, you should be careful to not change the current directory in the export module (once again, this only applies to event handlers, not to Start and Finish).
Calling Export Modules
To use an export module when running tests from the IDE, enter its ProgID in the vbUnit Options dialog (on the TestRunner tab). To use it on the command line, add the string "export ResultExport.ProgID" to the command line when calling the TestRunner.
On the options dialog, you can specify several export modules separated by line breaks or by the "|" character. For example, you can specify the following option to append the last result to a logfile, and to save it as a single new XML file (in vbUnit Options / TestRunner):
Call export modules: ResultExport.Logger | ResultExport.XMLConverter
(The output of these modules is located in the folder vbUnit3\ResultExport\Output).
The same thing can be achieved on the command line with the following example:
RunVBUnit MyTestSuite.ProgID export ResultExport.Logger export ResultExport.XMLConverter
Custom parameters
It is possible to specify a string to be passed to the Start method of the Export Module. This is anything between the first space following a ProgID and end of the line (or the next "|" character). The custom parameter string may be enclosed in quotes, and it may be prefixed by the "-" character.
Examples (Options Dialog):
ResultExport.Logger c:\output\vbunit.log, maxFileSize:250000
ResultExport.XMLConverter ..\log\lastResult.xml
On the command line, the syntax is slightly stricter. Here, the params string must be prefixed by a "-", and if it contains any spaces or hyphens, it must be enclosed in quotes.
Example (Command Line):
RunVBUnit MyTestSuite.ProgID export ResultExport.Logger - "c:\output\vbunit.log, clear"
Both in the options dialog and in the command line, the custom parameters should not contain the "|" character.
For another example, you should also look at the file vbUnit3\bin\runTestSuiteDemo.bat.