Skip to main content

Example: Creating and Exporting a Test Class

The class MyPackage.TestMe contains a method named Add that adds two integers. (This class is included in the UnitTestExamples.xml file you imported for this tutorial.) 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 Studio 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. Export the class Tests to an XML file in your unit test directory. If you have not yet created a test directory please create one. This example uses C:\unittests\mytests\.

    1. In Studio, click Tools —> Export.

    2. Click Add and Tests.cls.

    3. Click Browse and navigate to your unit test directory; in this example, C:\unittests\mytests\. If this directory does not exist, please create it now.

    4. Enter a name for the new XML file; in this example, Tests.

    5. Click OK.

The system creates Tests.xml file containing the test class in the directory C:\unittests\mytests\.

Note:

Note that the directory name in this case 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:

To learn more about using Studio, read Using Studio.

FeedbackOpens in a new tab