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:
-
Use Studio to create a new class named Tests in the MyPackage package. Tests must extend %UnitTest.TestCaseOpens in a new tab.
-
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") }
-
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\.
-
In Studio, click Tools —> Export.
-
Click Add and Tests.cls.
-
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.
-
Enter a name for the new XML file; in this example, Tests.
-
Click OK.
-
The system creates Tests.xml file containing the test class in the directory C:\unittests\mytests\.
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.)