Skip to main content

Assert Methods and Macros

The main testing actions of a unit test come from the AssertX methods and their associated macros. You will call the macros directly to test the output of methods. The macros test whether a method creates the desired output for a given input. Whenever an AssertX macro returns false (or ends in an error), the test that contains it fails.

As you create code, plan the unit tests you will create to test the code. In the example here, you have created a class called TestMe that contains a method called Add. You'd now like to test your new TestMe class to see if it works.

The following command runs the AssertEquals macro to test whether the input (2,2) to the Add method equals 4.

 Do $$$AssertEquals(##class(MyPackage.TestMe).Add(2,2),4, "Test Add(2,2)=4")
Copy code to clipboard

The AssertEquals macro compares two values and takes three arguments:

  1. ##class(MyPackage.TestMe).Add(2,2) — The first value is the method being tested with 2,2 as input.

  2. 4 — The second value.

  3. "Test Add(2,2)=4"— A text description that is written on the results page. (This argument does not affect the test. If there is no test description included, the class creates one using the expression evaluated.)

Here is an example of the AssertStatusOK macro being used to test that an object saves correctly.

 Do $$$AssertStatusOK(contact.%Save(),"Saving a Contact")
Copy code to clipboard

This AssertStatusOk macro evaluates a status returned by a method. If it is 1, the test passes.

  1. contact.%Save — An expression that returns a status code.

  2. "Saving a Contact" — A text description. This is documentation for the test report. It does not affect the test.

FeedbackOpens in a new window