BookmarkSubscribeRSS Feed

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

Started ‎09-06-2020 by
Modified ‎09-07-2020 by
Views 4,708

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 SASUnit, a unit testing framework for SAS 9 code and visualize the results.

 

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

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

For all the five steps you need the following technical components: SAS 9, SAS 9 machine (Windows), Jenkins, Jenkins agent, SASUnit, Jenkins plugins: SASUnit, Blue Ocean, GitLab. 

The focus of the post is to configure SASUnit:

 

1500-SAS9-Test-Automation-Diagram-SASUnit-1024x625.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.

Introduction to SASUnit

SASUnit is a testing framework developed by a SAS partner. The code is open sourced on SourceForge and subject to a GNU General Public License.

 

There are nice features that accelerate testing and centralize testing results. SASUnit integrates well with Jenkins through the SASUnit plug-in.

 

SASUnit is not a SAS supported product and you should treat like any open source project.

 

SASUnit is a SAS macro based testing framework. It consists of controller macros, asserts, etc. For more info, please visit the SASUnit user guide and the Getting started with SASUnit page.

 

The tests can be organized in SASUnit in an hierarchy: Test Scenarios > Test Cases > Assertions.

 

A test scenario is a group of test cases. The scenario is successful if all the test cases pass.

 

Assertion: a test case can have several assertions. An assertion is a Boolean expression at a specific point in a program which will be true unless there is a bug in the program. A test assertion is defined as an expression, which encapsulates some testable logic specified about the unit under test. SASUnit comes with a number of pre-written asserts you can reuse.

Example

Here is a unit under test, the nobs.sas program.

 

1540-SAS9-Test-Automation-SASUnit-unit-under-test.png

 

The test scenarios, cases and asserts are defined in a pair file prefixed unit_test, e.g.: nobs_test.sas.

 

1545-SAS9-Test-Automation-SASUnit-test-scenario.png

 

How to work with test scenarios, cases and assertions will be described in a future post.

SASUnit Configuration

Before you work with SASUnit, you need to configure it. SASUnit comes with some out of the box examples, to get you started. The following configuration assumes you want to run SASUnit with the out of the box examples.

SASUnit System Requirements

Works with SAS versions 9.2 to 9.4 running on Windows 7 or 10 and Linux. For more information, see the SASUnit System Requirements.

Download SASUnit

In the following example, I will use SAS 9.4M6 installed on a Windows Server 2012 R2 machine. This Windows version is not in the system requirements, but it works well with SASUnit.

 

On the Windows machine, download the source files from Source forge. Choose the version. The latest, as I am writing is 2.0.2. Download sasunit.v2.0.2zip .

 

Extract to a folder, for example: C:\sasunit . This path will become the SASUNIT_ROOT path in the configuration.

Configure SASUnit batch files

Before you run SASUnit, you need to parametrize a few paths in the batch and configuration files. These files are found in the SASUNIT_ROOT\example\bin folder, e.g. C:\sasunit\example\bin . The \example folder provides out of the box examples.

 

Firstly, identify your SAS version and operating system: 9.4M6 on Windows.

 

Secondly, alter the following batch files. The first two can be triggered manually or, scheduled. The .ci. files are created for Jenkins.

  • C:\sasunit\example\bin\sasunit.9.4.windows.en.cmd
  • C:\sasunit\example\bin\sasunit.9.4.windows.en.overwrite.cmd
  • C:\sasunit\example\bin\ sasunit.9.4.windows.en.ci.cmd
  • C:\sasunit\example\bin\ sasunit.9.4.windows.en.overwrite.ci.cmd

In the above files, alter the path to SAS.exe:

echo "Starting SASUnit ..."
"C:\Program Files\SASHome2\SASFoundation\9.4\sas.exe"

 

In my environment it translates to:

echo "Starting SASUnit ..."
"C:\Program Files\SASHome\SASFoundation\9.4\sas.exe"

 

Change the path to the SASUnit installation folder:

SET SASUNIT_ROOT=..\.

 

becomes

SET SASUNIT_ROOT=C:\sasunit

Configure SASUnit config file

Open the configuration file C:\sasunit\example\bin\sasunit.9.4.windows.en.cfg .

 

Alter the path to SASV9.CFG and add the full paths to -sysin -log and -print parameters.

 

For example:

-CONFIG "C:\Program Files\SASHome\SASFoundation\9.4\nls\en\SASV9.CFG"
-sysin "C:\sasunit\example\saspgm\run_all.sas"
-log "C:\sasunit\example\doc\sasunit\en\run_all.log"
-print "C:\sasunit\example\doc\sasunit\en\run_all.lst"

Test the configuration

Open a command window:

  1. Change to bin folder cd C:\sasunit\example\bin\
  2. Run the SASUnit executable C:\sasunit\example\bin\sasunit.9.4.windows.en.overwrite.cmd
  3. You should see a window popping up:

1530-SAS9-Test-Automation-SASUnit-test-300x241.png

The test results

Go to C:\sasunit\example\doc\sasunit\en\log and open index.html. Check the main page:

 

1480-SAS9-Test-Automation-Jenkins-SASUnit-main-e1597712730253-1024x613 (1).png

 

The tests are organized in SASUnit in an hierarchy: Test Scenarios > Test Cases > Assertions.

 

A test scenario is a group of test cases.

 

1490-SAS9-Test-Automation-Jenkins-SASUnit-test-scenarios-1024x622 (1).png

 

The scenario is successful if all the test cases pass.

 

Looking at the unit under test nobs.sas. The following test cases inside nobs_test.sas were executed. The results are marked in red or green.

 

1510-SAS9-Test-Automation-Jenkins-SASUnit-test-case-details (1).png

 

Assertions: a test case can have several assertions.A test assertion is defined as an expression, which encapsulates testable logic about the unit under test.

 

1520-SAS9-Test-Automation-Jenkins-SASUnit-assertions (1).png

Conclusions

In the test automation example for SAS 9, with SASUnit, a testing framework and Jenkins, an automation server we looked at the basic configuration for SASUnit. We then explained the test scenarios cases and assertions and looked at reports generated.

Next

Navigate through the post series:

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

Recommended Resources

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

Version history
Last update:
‎09-07-2020 12:49 AM
Updated by:
Contributors

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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