BookmarkSubscribeRSS Feed

Automate SAS 9 Code Unit Tests in a DevOps Pipeline with Jenkins and SASUnit Part 3

Started ‎09-13-2020 by
Modified ‎09-13-2020 by
Views 2,527

To be truly CI/CD, you need to design and perform automated tests. Test automation reduces dramatically the time to release a feature by detecting errors early. Learn how to configure Jenkins to trigger automated tests with SASUnit, a unit testing framework for SAS 9 code.

 

The test automation topic can be broad. Let us break it down into several parts:

  1. Visualize tests results
  2. Setup SASUnit, a SAS 9 Testing Framework
  3. Setup Jenkins to work with SASUnit ⭢ this post
  4. Add a new unit (SAS code) and test in SAS Unit
  5. Towards Continuous Testing

For the whole process, you need the following technical components used: SAS 9, SAS 9 machine (Windows), Jenkins, Jenkins agent, SASUnit, Jenkins plugins: SASUnit, Blue Ocean, GitLab.

 

The focus is on Jenkins.

 

1400-SAS9-Test-Automation-Diagram-overall-1024x628.png

Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.

Jenkins Setup

 

Jenkins agent on the SAS 9 machine

To Run SASUnit you have to define a Jenkins agent on the machine on which SAS 9 is installed (and where SASUnit is running the tests). We discussed the role of the agent in previous posts. 

 

Obviously, Jenkins can run agents on Linux machines. And SASUnit can be configured on Linux machines where SAS 9 is installed.

 

Go to Build Executor Status (Jenkins Nodes):

 

1560-SAS9-Test-Automation-Jenkins-agent-1024x678.png

 

The Jenkins agent must be connected and running.

 

1570-SAS9-Test-Automation-Jenkins-agent-list.png

Install the SASUnit Jenkins plug-in

Go to Jenkins Plugin Manager > Available. Search for SASUNIT, select and install without restart. After the successful installation you should see:

 

1600-SAS9-Test-Automation-Jenkins-plugin-SASUnit-1024x274.png

Configure the SASUnit Jenkins plug-in

You can view the Jenkins SASUnit plug-in documentation. However, allow me to guide you through and save you the time you would search blindly.

 

Go to Manage Jenkins  > Global Tool Configuration. Under SASUnit, find SASUnit > SASUnit installations.

 

1610-SAS9-Test-Automation-Jenkins-Global-tool-configuration-SASUnit.png

 

Add SASUnit:

  • The name: SASUnit 2.0.2, like the version.
  • Point the Home Directory to / (this is the SASUNIT_ROOT variable you defined in the previous post).

1620-SAS9-Test-Automation-Jenkins-Global-tool-configuration-SASUnit-advanced.png

 

As a side note, you can configure an automatic installation of SASUnit, which I think is a great feature!  With an automatic installer you can script the installation steps, so that if you change the agent, SASUnit will be automatically installed before running.

Configure the location of SASUnit on the Jenkins agent

We need to tell Jenkins where SASUnit is installed on the agent, the SAS 9 machine. Go to Jenkins Nodes. Find your SAS 9 agent. Check Tool Locations (at the bottom).

 

1630-SAS9-Test-Automation-Jenkins-agent-tool-locations.png

 

1640-SAS9-Test-Automation-Jenkins-agent-tool-locations2.png

 

Choose SASUnit and point it to the SASUNIT_ROOT path on the SAS 9 Windows machine: C:\sasunit. This is the same location you had to alter in the previous post at  Configure SASUnit batch files.

Jenkins unit testing pipeline

Define the pipeline

Create a new pipeline. Restrict the run to the defined SAS 9 agent, where SASUnit is installed.

 

1650-SAS9-Test-Automation-Jenkins-pipeline1.png

 

Click Advanced under the agent label. Jenkins needs a workspace for the artifacts to run.

 

Fill in the location of the SASUnit batch commands, found in the \bin folder, C:\sasunit\example\bin . Because we want the artifacts from C:\sasunit\example\ folders, point the location one level up to C:\sasunit\example .

 

1660-SAS9-Test-Automation-Jenkins-pipeline2-1024x427.png

Execute SASUnit in a pipeline

Add a first step to navigate to the SASUNIT_ROOT folder C:\sasunit.

 

Add a second step ‘Execute SASUnit Test Suite’ to execute SASUnit via the batch file.

 

The step can be found in Jenkins because you installed the SASUnit plugin. The executable corresponds to the C:\sasunit\example\bin\sasunit9.4.windows.en.ci.cmd.

 

1670-SAS9-Test-Automation-Jenkins-pipeline-step1.png

 

Choose the sasunit9.4.windows.en.ci.cmd , ci stands for ‘continuous integration’ and is meant to be used with Jenkins.

 

The difference between sasunit9.4.windows.en.ci.cmd and sasunit.9.4.windows.en.overwrite.ci.cmd ? En.ci.cmd will only look at what changes in the testing unit folders, while overwrite.en.ci.cmd will redo all the tests from scratch.

Add a post build action to archive the artifacts

At execution time, artifacts such as .log or junit.xml files will be generated.

 

JUnit is one of those unit frameworks which were initially used by many Java applications as a Unit test framework. By default, JUnit tests generate simple report XML files for its test execution. These XML files can then be used to generate any custom reports as per the testing requirement.

 

1670-SAS9-Test-Automation-Jenkins-pipeline-step2.png

 

The file path is relative to the custom workspace C:\sasunit\example defined in the pipeline definition.

 

The artifacts are stored in the Jenkins Pipeline Workspace, at build time.

 

1680-SAS9-Test-Automation-Jenkins-workspace.png

How to display build and test results in Jenkins

By using the Jenkins post-build step "Publish JUnit test result report" you can display the result of your SASUnit build. The only thing left to do is telling Jenkins where to search for this file. **/*junit.xml will search all folders of the custom workspace folder C:\sasunit\example for a file called junit.xml:

 

1670-SAS9-Test-Automation-Jenkins-pipeline-step3.png

 

The health report amplification factor defines a threshold for a stable or unstable build.

SASUnit generates the junit.xml file

The file contains the number of passed tests (asserts) and you can get an overview of the test outcome.

 

The build status will automatically be determined by the created JUnit-XML. If there are no failed asserts the build is stable. If there are failed asserts the build is unstable.

 

SASUnit will create a JUnit-XML file if you specify o_junit=1 in the macro call of reportSASUnit:

 

%reportsasunit(
i_language =%upcase(%sysget(SASUNIT_LANGUAGE))
,o_html     =1
,o_junit    =1
);

 

The JUnit.xml file and the HTML documentation are created in the C:\sasunit\example\docsasunit\en\rep folder .

How to display SASUnit HTML reports in Jenkins

Displaying SASUnit documentation in Jenkins is easy. You can use the post build step "Publish HTML Reports":

 

1670-SAS9-Test-Automation-Jenkins-pipeline-step4.png

Test Results

Finally, when we run the pipeline we get the results, presented in the previous post, Visualize tests results.

 

1430-SAS9-Test-Automation-Jenkins-pipeline-Tests (1).png

Conclusions

We looked at how to set test automation in Jenkins and the configuration needed to call SASUnit, a testing framework and to display the test results.

Next

Navigate through the post series:

  1. Visualize tests results
  2. Setup SASUnit, a SAS 9 Testing Framework
  3. Setup Jenkins to work with SASUnit ⭢ this post
  4. Add a new unit (SAS code) and test in SAS Unit ⭢ next post
  5. Towards Continuous Testing

Recommended Resources

Thank you for your time reading this post. Please comment and share your experience with SASUnit, unit testing or SAS 9 and DevOps.

Version history
Last update:
‎09-13-2020 10:13 PM
Updated by:
Contributors

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started