Engage Demo - Unit Testing
Setup Script Library and Agent
- Copy VoltScriptTesting.bss into a Script Library, calling it "VoltScriptTesting".
- Create an agent called "LotusScriptUnitTesting".
- Set the "Trigger" to "Agent list selection".
- Set the "Target" to "None".
- In "Options" add
Use VoltScriptTesting
.
Person Class
The Person class will contain firstName, lastName, age and gender. But gender will only be settable via a Property, and the Property will ensure a valid value is passed, otherwise generate an error.
- Add a class called "Person".
- Add three public properties:
- firstName as String
- lastName as String
- age as Integer
- Add a private property, pgender as String.
-
Add a property get for gender:
-
Add a property set for gender:
Test Successful Creation
This function will test creating a Person successfully, and validate the properties are as expected.
- Create a function called "testBasic",
Function testBasic(testRunner As TestRunner) As Boolean
. - Declare a variable
p
as a new Person. - Declare a TestSuite called
testSuite
with the name "Testing Basic Success". - Add testSuite to the testRunner.
- Add error handling to trigger
Call testSuite.addError("Error: " & Error & " on line " & Erl, Erl)
. - Add a test with the description "Test creating a person".
- Set firstName, lastName, age and a valid gender for
p
. - Call
Call testSuite.addResult(True, "")
. If we get here, the test was successful. - Add a test with the description "Test p is a Person".
- Add an assertion that
p
is a "PERSON". - Add a test to validate the Person's name.
- Add a test to validate the Person's age.
- Add a test to validate the Person's gender.
- Exit the function by calling
testBasic = testSuite.ranSuccessfully()
.
Completed Function
Function testBasic(testRunner As BaliTestRunner) As Boolean
Dim p As New Person
Dim testSuite As New TestSuite("Testing Basic Success")
Call testRunner.addTestSuite(Testsuite)
On Error GoTo logErr
Call testSuite.describe("Test creating a person")
p.firstName = "Paul"
p.lastName = "Withers"
p.age = 21
p.gender = "Male"
Call testSuite.addResult(True, "") 'Successfully created a person
Call testSuite.describe("Test p is a Person").assertIs("PERSON", p)
Call testSuite.describe("Test name is Paul Withers").assertEqualsString("Paul Withers", p.firstName & " " & p.lastName, False)
Call testSuite.describe("Test age is 21").assertEqualsInteger(21, p.age)
Call testSuite.describe("Test gender is male").assertTrue(p.gender = "Male")
testBasic = testSuite.ranSuccessfully()
Exit Function
logErr:
Call testSuite.addError("Error: " & Error & " on line " & Erl, Erl)
Exit Function
End Function
Test Gender Validation
Testing success is insufficient for proper unit testing. We also need to unit test passing bad values.
- Create a function called "testGenderValidation",
Function testGenderValidation(testRunner As TestRunner) As Boolean
. - Declare a variable
p
as a new Person. - Declare a TestSuite called
testSuite
with the name "Testing Basic Success". - Add testSuite to the testRunner.
- Add error handling with a label name
mainErr
to triggerCall testSuite.addError("Error: " & Error & " on line " & Erl, Erl)
, resuming next. - Add a test with the description "Test creating a person does not accept 'Mail'".
- Set the firstName, lastName and age of
p
. - Add on
On Error
statement to goto a label called "testErr". - Add a
testErr
block after the main error block. In the error block:- Call
Call testSuite.addResult(True, "")
. - Call
Resume otherChecks
.
- Call
- After the
On Error GoTo testErr
, set gender ofp
to "Mail". - Fail the test if it gets to this line with
Call testSuite.addResult(False, "Setting gender to Mail should error")
. - Add a label called
otherChecks
. We want to test all other properties after we passed the invalid gender. - Add another
On Error
statement to go to mainErr. If any other errors occur, we want to error the test. - Add a test with the description "Test p is a Person".
- Add an assertion that
p
is a "PERSON". - Add a test to validate the Person's name.
- Add a test to validate the Person's age.
- Add a test to validate the Person's gender is not "Male".
- Exit the function by calling
testGenderValidation = testSuite.ranSuccessfully()
.
Completed Function
Function testGenderValidation(testRunner As TestRunner) As Boolean
Dim p As New Person
Dim testSuite As New TestSuite("Testing Gender Validation")
Call testRunner.addTestSuite(Testsuite)
On Error GoTo mainErr
Call testSuite.describe("Test creating a person does not accept 'Mail'")
p.firstName = "Paul"
p.lastName = "Withers"
p.age = 21
On Error GoTo testErr
p.gender = "Mail"
Call testSuite.addResult(False, "Setting gender to Mail should error") 'Successfully created a person
otherChecks:
On Error GoTo mainErr
Call testSuite.describe("Test p is a Person").assertIs("PERSON", p)
Call testSuite.describe("Test name is Paul Withers").assertEqualsString("Paul Withers", p.firstName & " " & p.lastName, False)
Call testSuite.describe("Test age is 21").assertEqualsInteger(21, p.age)
Call testSuite.describe("Test gender is not male").assertFalse(p.gender = "Male")
testGenderValidation = testSuite.ranSuccessfully()
Exit Function
mainErr:
Call testSuite.addError("Error: " & Error & " on line " & Erl, Erl)
Resume Next
testErr:
Call testSuite.addResult(True, "")
Resume otherChecks
End Function
Sub Initialize
- Declare a variable
testRunner
with the name "Validating Person Class". - Declare a boolean variable called result.
- Set
result
to the result oftestBasic
, passing thetestRunner
variable. - Set
result
to the result ofresult
andtestGenderValidation
, passing thetestRunner
variable. - If
result
is true, print "All tests passed"
Completed
Success
You have successfully created unit tests for the Person object. Test by running the agent. The output will be written to "unit-test-reports" directory under the Notes program directory.
Tip
To test yourself further:
- Add unit tests for trying to pass a non-string value to
firstName
orlastName
. - Add a unit test for trying to pass a non-numeric value to age.
- An Integer can be a number that is not sensible for an age. Change age to a private variable and use a property to validate age is within reasonable bounds. Add tests for this.