Skip to main content

Example: Creating a Test Class

The class MyPackage.TestMe contains a method named Add that adds two integers. In this example you create and run a unit test to check that the Add method correctly adds two integers.

Create a test class that will contain the unit tests. Here's how:

  1. Use VS Code to create a new class named Tests in the MyPackage package. Tests must extend %UnitTest.TestCaseOpens in a new tab.

  2. Add the following method named TestAdd and compile Tests:

    Method TestAdd()
    {
     do $$$AssertEquals(##class(MyPackage.TestMe).Add(2,2),4, "Test Add(2,2)=4")
     do $$$AssertNotEquals(##class(MyPackage.TestMe).Add(2,2),5,"Test Add(2,2)'=5")
    }
    
  3. Copy the class file for Tests to a subdirectory within your unit test directory. For this example, copy the file to C:\unittests\mytests\cls\MyPackage.

Note:

As you will see in the next example, mytests is the name for a suite of tests and is also a child of the directory specified by ^UnitTestRoot. Running Manager.RunTest("mytests") runs all of the tests stored in the mytests directory.

Note:

You can also export the test class as an XML file from the Management Portal (System Explorer > Classes) into the unit test directory.

FeedbackOpens in a new tab