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:
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:
Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.
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.
Here is a unit under test, the nobs.sas program.
The test scenarios, cases and asserts are defined in a pair file prefixed unit_test, e.g.: nobs_test.sas.
How to work with test scenarios, cases and assertions will be described in a future post.
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.
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.
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.
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.
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
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"
Open a command window:
Go to C:\sasunit\example\doc\sasunit\en\log and open index.html. Check the main page:
The tests are 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.
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.
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.
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.
Navigate through the post series:
Thank you for your time reading this post. Please comment and share your experience with DevOps for SAS 9, SASUnit and unit testing.
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!
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.