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 Studio (You may need to import it from your ^UnitTestRoot directory).

  2. Add the OnBeforeAllTests and OnAfterAllTests methods.

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

    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")
    }
    
    Copy code to clipboard

    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. Export Tests to c:\unittests\mytests overwriting the existing Tests.xml.

FeedbackOpens in a new window