Thursday, December 20, 2007

SnTT : Unit Testing In Domino

I've had this database of code that is a framework for Unit testing in Domino/LotusScript. Its somewhat similar to JUnit. I wrote it so that I could double check code that was high risk and to give me another level of confidence that it would cope with the unexpected. I've been meaning to add a little polish to the database before I release it into the community. I've finally got around to it this week (and just in time for SnTT). It's not 100% complete, but might be of use to someone and rather than it sitting on my disk I've published it as a project on OpenNTF (here).

Follow this link to find out what is JUnit?

JUnit fits into a technique for Test Driven Development (TDD) where the basic premise is that you write tests for a unit (function, method etc) before writing the actual code. You then write your intended function (or method etc) until it passes all of your pre-written tests. In practice this is quite a large shift in mindset for some programmers. However, writing unit tests without adopting the whole TDD can still be beneficial (and I believe is quite common). For me it means that I can have a set of tests that I can check against code quickly, in a repeatable way and if I need to refactor my code I can quickly determine the impact of those changes by the success (or failures) of the existing tests.

The 'Domino Unit Framework' (DUF) is the Domino (or maybe that should be LotusScript - although LUF isn't quite as funny) take on this framework. The database contains example code in the one and only agent, but here are a few snippets so that you can see how it works.

Dim OutputStream As NotesDatabaseOutputStream
Set OutputStream = New NotesDatabaseOutputStream(session, "", "DominoUnitResults.nsf")

Dim testObject As New Test("Test Objects", Outputstream)
Call testObject.AssertEqual(session.currentdatabase,Null)

You can also check for True or False, in the example below I have just passed a True/False but typically you would include your code that returns a true or false.

Call testTrue.AssertTrue(True)
Call testFalse.AssertFalse(False)
Call test.AssertFalse(object.myMethodThyatIExcepectToReturnFalse)

You can also group up a set of tests into a 'suite of tests' which you can get an overall pass or fail or maybe just group them into logical functions.

Dim testSuite As New TestSuite("isMemberTests", OutputStream)
Call testSuite.Add(test1)
Call test1.AssertEqual(True,isMember(vArray2,vArray1))

So what objects does this support ?

Currently the following objects are supported as they were the ones that I needed - I might added more later or maybe someone else might like to (if you fancy contributing).

- integer
- long
- double
- currency
LS Date
Boolean (Integer)
- NotesDocument (based on UNID)
- NotesDatabase (based on Replica Id)

Can I test my Own classes ?

You can also unit test your own custom classes. You will need to implement an isEqual method which the framework expects.

Dim tony As New Person
Dim emp As New Person

Call tony.init("Tony","A","Palmer")
Call emp.init("Tony","A","Palmer")

Call Test101.AssertEqual(tony,emp)

Class Person
Function isEqual(people As Person) As Integer
' add in your own code that determines equality or not.
End Function
End Class

How can I have a quick look ?

Just download the DUF Database from OpenNTF and then create a copy (without Documents) in the root directory with a name of DUFResults.nsf. Then in the DUF database run the 'Development / Unit Test Agent'. There are some screen shots here of the database here.

Until Next Year.

I expect that this will be my last SnTT post until after LS08, I'm not going but will spending my time following all the news remotely.

Merry Xmas (Happy Holidays!) and Happy New Year.

No comments:

Post a comment