Determine programmatically if unit tests failed

I needed to know programmatically if last ran failed or not.

After some exploring, here's the code:

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
}

Comments

You could use sql too:

   &sql(select count(id) into :failures from %UnitTest_Result.TestSuite where Status = 0 and TestInstance = (select top(1) InstanceIndex from %UnitTest_Result.TestInstance order by DateTime desc))
    return 'failures

 

I thought there was a method to do just this in the TestInstance class, but it's actually in %UnitTest.Portal package:

USER>w ##class(%UnitTest.Portal.standardPage).GetTestStatus(^UnitTest.Result)
0

Up to you if you want to depend on an implementation detail of the portal. It is a public method that's been there since 2012.