Maik Gode · Dec 2, 2020

Trigger UnitTests programmatically in a cls file

Hey all,

I am currently working on a website (with Angular) for displaying the results of UnitTests.

Everything is working fine, except that I can not run a test inside a ClassMethod.


I have 2 classes: One for handling which test class should be invoked and then a test class that is extending %UnitTest.TestCase


The test class:

Class my.class.path.testing.EingangTest Extends %UnitTest.TestCase
    Method CreateEingangTest() As %Boolean
        Set eingangObj = ##class(My.class.path.Eingang).%New()
        if $$$ISOK(status) {
           set eingangObj.ezeichnung = "Testlager"
           set eingangObj.adresseStr = "Teststrasse"
           set eingangObj.adressePLZ = "20192"
           set eingangObj.adresseOrt = "Testhausen"
        set status = $$$AssertStatusOK(eingangObj.%Save(0), "true")
        return status


In the handler class I tried something like this:

set testCase = {
set testObj = ##class("my.class.path.testing.EingangTest").%New("")
set status = testObj.CreateEingangTest()
set testCase.testName = "EingangTest"
set testCase.success = status
write testCase.%ToJSON()
return testCase



The problem is that I am getting an "INVALID OREF"-Error pointing to the "$$$AssertStatusOK".


My second try was to use the way showed in the documentation. So I exported my EingangTest class and added this to the handler:

set ^UnitTestRoot="C:\My\Path"
set status = ##class(%UnitTest.Manager).RunTest("tests")


This solved the problem and the test was successful, but the respone from the server to my website failed with a JSON parse error.

Is there any way to run a test without using the terminal?


Thanks in advance


Product version: Ensemble 2018.1
0 113
Discussion (1)1
Log in or sign up to continue

I run tests programmatically like this:

ClassMethod runtest() As %Status
    set ^UnitTestRoot = basedir
    set sc = ##class(%UnitTest.Manager).RunTest(testdir, "/nodelete")
    quit sc

ClassMethod isLastTestOk() As %Boolean
    set in = ##class(%UnitTest.Result.TestInstance).%OpenId(^UnitTest.Result)
    for i=1:1:in.TestSuites.Count() {
        #dim suite As %UnitTest.Result.TestSuite
        set suite = in.TestSuites.GetAt(i)
        return:suite.Status=0 $$$NO
    quit $$$YES