Published on InterSystems Developer Community (https://community.intersystems.com)

Home > Trigger UnitTests programmatically in a cls file

Question
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 = {
   "testName": "",
   "success": "" 
}
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

Maik

#Testing #Ensemble
Product version: Ensemble 2018.1

Source URL:https://community.intersystems.com/post/trigger-unittests-programmatically-cls-file