Skip to main content

Example: Adding Setup and Tear Down Methods to a Test

In this example you add a test method named TestEditContact. This method verifies that the ContactType property of the MyPackage.Contact class is restricted to “Personal” or “Business”. You add an OnBeforeAllTests method that prepares the database before the test executes. You also add an OnAfterAllTests method that restores the database state after the test executes.

  1. Open MyPackage.Tests in VS Code.

  2. Add the OnBeforeAllTests and OnAfterAllTests methods.

    Method OnBeforeAllTests() As %Status
    {
       Do ##class(MyPackage.Contact).Populate(1)
       Return $$$OK
    }
    
    Method OnAfterAllTests() As %Status
    {
       Do ##class(MyPackage.Contact).%KillExtent()
       Return $$$OK
    }
    

    The OnBeforeAllTests method populates the database with a single Contact instance. The OnAfterAllTests method deletes all of the Contact instances from the database.

  3. Now add the TestEditContact test method to MyPackage.Tests:

    Method TestEditContact()
    {
       set contact=##class(MyPackage.Contact).%OpenId(1)
       set contact.Name="Rockwell,Norman"
       set contact.ContactType="Friend"
       Do $$$AssertStatusNotOK(contact.%Save(),"ContactType = Friend")
       Set contact.ContactType="Personal"
       Do $$$AssertStatusOK(contact.%Save(),"ContactType = Personal")
    }
    

    The method tests the status value returned by executing %Save on Contact in two situations: after assigning an invalid value to ContactType and after assigning a valid value to ContactType.

  4. Copy the Tests class file to C:\unittests\mytests\cls\MyPackage, overwriting the existing file.

FeedbackOpens in a new tab