Skip to main content

Example: Adding Set-Up 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.

    Since you are overriding methods from %UnitTest.TestCaseOpens in a new tab, click Class —> Refactor —> Override on the menu bar and then click the method names in the inherited method list (use Ctrl-Click for the second name). This adds the existing implementations to the class definition.

    Modify them so they look like the following:

    Method OnBeforeAllTests() As %Status
       Do ##class(MyPackage.Contact).Populate(1)
       Quit $$$OK
    Method OnAfterAllTests() As %Status
       Do ##class(MyPackage.Contact).%KillExtent()
       Quit $$$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. Export Tests to c:\unittests\mytests overwriting the existing Tests.xml.

FeedbackOpens in a new tab