Unit Tests: Quality of your ObjectScript Code
Tags: coding, testing, programming, beginners
Published: 2024-01-28 15:53:55


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uql9s02o9nm7hwozswfn.png)
<p>In the main software development methodologies there is always a chapter dedicated to testing. It is a mandatory approach to achieving quality in deliveries on an ongoing basis.</p>
<p>There are two types of test:</p>
<ol>
<li><strong>White Box Test</strong>: These are tests that examine the quality of the source code and application functionality. In this type of test we have:
<ol>
<li><strong>Static analysis</strong>: static analysis solutions are used (there is no functionality execution at the time of testing) of the source code, where naming patterns, indentation, declared and unused variables, coupling index between components, among other defined criteria are evaluated within analysis solutions. Generally, within the development environment, lines of source code with quality problems are pointed out, allowing the developer to act to solve the problem.</li>
<li><strong>Unit test</strong>: test classes (Test Unit) are created, one for each functionality topic (Maintain Customers, Transaction Collection, etc.), grouped in Test Suites (set of Test Units), one suite for each application module (Registration Module, Sales Module, etc.) or for the application as a whole. Generally, an HTML Report is issued with the results found.</li>
</ol></li>
<li><strong>Black Box Test:</strong> tests performed on the application's binary, without access to the application's source code. In this type we have:
<ol>
<li><strong>Performance Test</strong>: Data load and execution scripts are defined for the main functionalities to assess whether the application and execution environment will support the amount of transactions and users expected.</li>
<li><strong>Penetration Test - PenTest</strong>: vulnerability analysis solutions are used for the IP addresses and ports of the application, the products that host the solution (web server, application and database) and the application endpoints (API, Web Services, File Exchange Directories and other interaction points). Usually, a PDF report is issued with the vulnerabilities found.</li>
<li><strong>Functional Testing</strong>: Test Analysts and testers write and execute several test scenarios from the application's visual interfaces, aiming to identify functional flaws (functionality with error from the end-user's point of view). Today there are solutions that also automate these tests from the visual interfaces.</li>
</ol></li>
</ol>
<p>An important objective of the Testing Discipline is to find the sweet spot of test coverage (features covered by tests). In the projects I'm involved in, the goal is to achieve at least 75% of the functionality. The big secret is to focus your tests on the business layer of the application, because that's where the business rules and the implementation of the functional requirements delivered to the end user are. Different visual interfaces and integration points always end up requiring the business layer. In this way, when automating the tests of the functional layer, the coverage of the tests will have a relevant rate.</p>
<p>This article presents how to automate the unit tests of the business layer of your IRIS application, in order to demonstrate how to achieve good test coverage rates and good quality in IRIS applications delivered to the end user.</p>
<h2>Download the app to be tested</h2>
<p>Go to https://openexchange.intersystems.com/package/global-mindmap and click Github buttion. Follow these steps:</p>
<ol>
<li>Clone the project</li>
</ol>
<pre><code>$ git clone https://github.com/yurimarx/global-mindmap.git
</code></pre>
<ol start="2">
<li>Do docker build inside in the project root folder:</li>
</ol>
<pre><code>$ docker-compose build
</code></pre>
<ol start="3">
<li>Execute the Docker Container:</li>
</ol>
<pre><code>$ docker-compose up -d 
</code></pre>
<h2>See the app in action</h2>
<ul>
<li>Local env: http://localhost:3000</li>
<li>On YouTube: https://www.youtube.com/watch?v=M6EupvAATro</li>
</ul>
<h2>Now, go to the tests</h2>
<h3>Business Class to be tested</h3>

Business Class Target for testing</div>

```
Class dc.globalmindmap.GlobalMindMapService
{
 
/// Store mindmap node
ClassMethod StoreMindmapNode(data As %DynamicObject) As %Status
{
    Try {
 
        Set ^mindmap(data.id) = data.id
        Set ^mindmap(data.id, "topic") = data.topic
        Set ^mindmap(data.id, "style", "fontSize") = data.style.fontSize
        Set ^mindmap(data.id, "style", "color") = data.style.color
        Set ^mindmap(data.id, "style", "background") = data.style.background
        Set ^mindmap(data.id, "parent") = data.parent
        Set ^mindmap(data.id, "tags") = data.tags.%ToJSON()
        Set ^mindmap(data.id, "icons") = data.icons.%ToJSON()
        Set ^mindmap(data.id, "hyperLink") = data.hyperLink
 
        Return 1
 
    } Catch err {
        write !, "Error name: ", ?20, err.Name,
            !, "Error code: ", ?20, err.Code,
            !, "Error location: ", ?20, err.Location,
            !, "Additional data: ", ?20, err.Data, !
        Return 0
    }
}
 
ClassMethod GetMindmap(Output Nodes As %DynamicArray) As %Status
{
    Try {
     
      Set Nodes = []
 
      Set Key = $ORDER(^mindmap(""))
      Set Row = 0
     
      While (Key '= "") {
        Do Nodes.%Push({})
        Set Nodes.%Get(Row).style = {}
        Set Nodes.%Get(Row).id = Key
        Set Nodes.%Get(Row).hyperLink = ^mindmap(Key,"hyperLink")
        Set Nodes.%Get(Row).icons = ^mindmap(Key,"icons")
        Set Nodes.%Get(Row).parent = ^mindmap(Key,"parent")
        Set Nodes.%Get(Row).style.background = ^mindmap(Key,"style", "background")
        Set Nodes.%Get(Row).style.color = ^mindmap(Key,"style", "color")
        Set Nodes.%Get(Row).style.fontSize = ^mindmap(Key,"style", "fontSize")
        Set Nodes.%Get(Row).tags = ^mindmap(Key,"tags")
        Set Nodes.%Get(Row).topic = ^mindmap(Key,"topic")
        Set Row = Row + 1
       
        Set Key = $ORDER(^mindmap(Key))
      }
     
      Return 1
   
    } Catch err {
      Write !, "Error name: ", ?20, err.Name,
          !, "Error code: ", ?20, err.Code,
          !, "Error location: ", ?20, err.Location,
          !, "Additional data: ", ?20, err.Data, !
      Return 0
    }
}
 
/// Delete mindmap node
ClassMethod DeleteMindmapNode(id As %String) As %Status
{
    Try {
     
      Kill ^mindmap(id)
     
      Return 1
 
    } Catch err {
      write !, "Error name: ", ?20, err.Name,
          !, "Error code: ", ?20, err.Code,
          !, "Error location: ", ?20, err.Location,
          !, "Additional data: ", ?20, err.Data, !
      Return 0
  }
}
 
/// Has content: 1 - yes, 0 - no
ClassMethod HasContent(Output Result As %String) As %Status
{
    Try {
     
        Set key = $ORDER(^mindmap(""))
     
        If key = "" {
            Set Result = "0"
        } Else {
            Set Result = "1"
        }
 
      Return 1
   
    } Catch err {
      Write !, "Error name: ", ?20, err.Name,
          !, "Error code: ", ?20, err.Code,
          !, "Error location: ", ?20, err.Location,
          !, "Additional data: ", ?20, err.Data, !
      Return 0
    }
}
 
}
```

<p>The business class has 3 features to be tested:</p>
<ol>
<li><strong>StoreMindmapNode</strong>: functionality used to record data from a mind map node in the database in the form of globals.</li>
<li><strong>GetMindmap</strong>: functionality used to return all the nodes of the mind map stored in globals, that is, the mind map as a whole.</li>
<li><strong>DeleteMindmapNode</strong>: functionality used to delete a mental map node from the database (deletes the global node that stores the node).</li>
</ol>
<h3>Creating the test suite - A set of Test Cases</h3>
<p>Just create a Directory at the root of the src directory, with the name of the test suite, in this example the name created was UnitTests.</p>
<p>Creating Test Suite Test Cases</p>
<p>You must create test classes that inherit from %UnitTest.TestCase. The class name, as a good practice, should be TestClassNameToBeTested + Test. In our example the class is called GlobalMindMapServiceTest. See the content of the class:</p>

**Test case class**

```
Class UnitTests.GlobalMindMapServiceTest Extends %UnitTest.TestCase
{
 
Method TestStoreMindmapNode()
{
    Set data = {}
    Set data.id = "TestId"
    Set data.topic = "TestTopic"
    Set data.parent = ""
    Set data.tags = []
    Set data.icons = []
    Set data.style = {}
    Set data.style.background = "black"
    Set data.style.fontSize = "arial"
    Set data.style.color = "white"
    Set data.style.hyperLink = "intersystems.com"
    Do ##class(dc.globalmindmap.GlobalMindMapService).StoreMindmapNode(data)
    Set Key = ^mindmap(data.id)
    Do $$$AssertEquals(data.id, Key)
    Do ##class(dc.globalmindmap.GlobalMindMapService).DeleteMindmapNode(data.id)
}
 
Method TestDeleteMindmapNode() As %Status
{
    Set data = {}
    Set data.id = "TestDelete"
    Set data.topic = "TestDelete"
    Set data.parent = ""
    Set data.tags = []
    Set data.icons = []
    Set data.style = {}
    Set data.style.background = "black"
    Set data.style.fontSize = "arial"
    Set data.style.color = "white"
    Set data.style.hyperLink = "intersystems.com"
    Do ##class(dc.globalmindmap.GlobalMindMapService).StoreMindmapNode(data)
    Do ##class(dc.globalmindmap.GlobalMindMapService).DeleteMindmapNode(data.id)
    Set Key = $ORDER(^mindmap(data.id))
    Do $$$AssertEquals("", Key)
}
 
Method TestGetMindmap()
{
    Set data = {}
    Set data.id = "TestGet"
    Set data.topic = "TestGet"
    Set data.parent = ""
    Set data.tags = []
    Set data.icons = []
    Set data.style = {}
    Set data.style.background = "black"
    Set data.style.fontSize = "arial"
    Set data.style.color = "white"
    Set data.style.hyperLink = "intersystems.com"
    Do ##class(dc.globalmindmap.GlobalMindMapService).StoreMindmapNode(data)
    Do ##class(dc.globalmindmap.GlobalMindMapService).GetMindmap(.Result)
    Set Count = Result.%Size()
    Do $$$AssertEquals(Count, 1)
    Do ##class(dc.globalmindmap.GlobalMindMapService).DeleteMindmapNode(data.id)
}
 
}
```

<p>Notice that for each functionality to be tested, we have a Method with the following naming convention: Test +NameOfMethodToBeTested. It is important that each test method does everything necessary for the test to run. In the Delete test, for example, a record is first created, which will be used to delete and thus test if the deletion is working.</p>
<p>For each Method there must be at least one call to $$$Assert.... This macro is the execution and recording of the test condition itself. In our example, we use $$$AssertEquals in all methods. In this case, if the test condition is equal to the result of executing the business method, the test is marked as successful, otherwise it is marked as unsuccessful. There are several Assert options, see more details at https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls?KEY=TUNT_AssertX.</p>
<p>It is also possible to use the OnBeforeAllTests Method to create the data and conditions necessary for the tests and use OnAfterAllTests to clean the data and environments to their original state.</p>
<h3>Executing the tests</h3>
<p>With the suite and test cases ready, it's time to perform the tests. For this you need to access the Terminal, in the namespace where the classes are located, see the steps for the example in this article:</p>
<ol>
<li>On Terminal, namespace IRISAPP, it is necessary to go to the namespace that contains the business and test classes:</li>
</ol>
<pre><code>USER&gt;zn "IRISAPP"
</code></pre>
<ol start="2">
<li>It is necessary to point to the folder where the Test Suite folder is located (in the example it is the UnitTests folder inside src)</li>
</ol>
<pre><code>IRISAPP&gt;Set ^UnitTestRoot="/opt/irisbuild/src"
</code></pre>
<ol start="3">
<li>Execute the unit tests</li>
</ol>
<pre><code>IRISAPP&gt;do ##class(%UnitTest.Manager).RunTest("UnitTests")
</code></pre>
<ol start="4">
<li>See the results in <a href="http://localhost:52773/csp/sys/%25UnitTest.Portal.Indices.cls?Index=1&amp;%24NAMESPACE=IRISAPP" rel="nofollow">http://localhost:52773/csp/sys/%25UnitTest.Portal.Indices.cls?Index=1&amp;$NAMESPACE=IRISAPP</a></li>
</ol>
<h3>Analyzing the Results</h3>
<p>The report from the previous step returns in this layout:</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhtlfwa8oyz75ojgn6jy.png)
<p>It is possible to know what passed (green) and what failed (red). By clicking on red, it is possible to know where the error occurred:</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ejwqwxdsu7nvod3q4ffb.png)
<p>Now just act on the fixes.</p>
<p>If you liked the sample application, vote for it in the Global Contest.</p>
<p>&nbsp;</p>