CI / CD integration for Unit Tests

Unofficial Content

As discussed in the previous section Typical GeneXus development cycle, the software industry has evolved to enable a set of automation for boost quality and adopting DevOps practices. Continuous integration is a fundamental step to start working in a proper way with GXserver and GXtest. 

Continuous integration means that every time a developer makes a change in a centralized repository in GXserver KB, that code will trigger some automation in order to check that recent changes can successfully build a new version without conflicts with other developments. Also, it will check that there are no missing references to be committed and that the build succeeds in a totally different environment than it was developed. With this step in your process, you will be avoiding the typical dev phase like “it works/builds on my machine”. Theoretically, CI means to integrate every time a developer makes a commit, but sometimes in your methodology, you want to do this less often but still getting often (maybe daily) feedback regarding your app status. In other words, that is continuously integrated.

CI tools are usually agnostic and you can integrate GeneXus with any CI tools using msbuild.exe on windows platforms. For example purposes, we will be showing some examples using Jenkins CI.

In the same way that you will use GeneXus Tasks such as OpenKB, BuildAll, RebuildAll, etc. for handling Knowledge Base tasks and Team Development Tasks for handling GXServer, in order to run Unit tests you will need to call some extra task / step. You can take a look at documentation of this tasks here.

How-to Run All your Unit Tests in your KB

GXtest.msbuild: Let's assume that this is the filename of the MSbuild File that you are building to run Unit Tests.

At the beggining, you will need to import two msbuild projects that contain predefined tasks which you are going to use on your file. Those projects are: GeneXus.Tasks.targets and GXtest.targets. 

The following is an example of msbuild file:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">    
  <Import Project="$(GX_PROGRAM_DIR)\GeneXus.Tasks.targets"/>    
  <Import Project="$(GX_PROGRAM_DIR)\GXtest.targets"/>

  <Target Name="RunAllTests">
    <OpenKnowledgeBase Directory="$(KBPath)"/>
    <SetActiveEnvironment EnvironmentName="$(EnvironmentName)"/>
    <UpdateTestRefs Type="$(TestType)"/>
    <BuildOne BuildCalled="true" ObjectName="Runner"/>
    <RunTests Type="$(TestType)"/>
    <CloseKnowledgeBase/>
  </Target>
</Project>

Having this file configured properly, you can launch all unit tests using this command line:

MSBuild.exe /t:RunAllTests /p:KBPath="C:\Models\KbTests" /p:EnvironmentName="CSharpWeb" /p:TestType="Unit" $env:GX_PROGRAM_DIR"\GXtest.msbuild"

How-to Run a Test List (suite) of your KB

As well as the task Run All Tests, you will need to configure a msbuild file as is showed below: 

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">    
<Import Project="$(GX_PROGRAM_DIR)\GeneXus.Tasks.targets"/>    
<Import Project="$(GX_PROGRAM_DIR)\GXtest.targets"/>

<Target Name="RunTestsList">
    <OpenKnowledgeBase Directory="$(KBPath)"/>
    <SetActiveEnvironment EnvironmentName="$(EnvironmentName)"/>
    <UpdateTestRefs ExecutionDataFilePath="$(pathToJSON)"/>
    <BuildOne BuildCalled="true" ObjectName="Runner"/>
    <RunTestsList ExecutionDataFilePath="$(pathToJSON)"/>
    <CloseKnowledgeBase/>
</Target>
</Project>

Having this file configured properly, you can run a specific unit tests list using this command line:

MSBuild.exe /t:RunTestsList /p:KBPath="C:\Models\KbTests" /p:EnvironmentName="CSharpWeb" /p:pathToJSON="C:\Models\KBTests\TestList.json" $env:GX_PROGRAM_DIR"\GXtest.msbuild"