How to Automate Your Code Testing Using SASUnit

Written by

in

Getting Started with SASUnit: A Beginner’s Tutorial SASUnit is an open-source, macro-based unit testing framework designed explicitly for validating SAS programs and macros. By embedding test scenarios directly into executable source code, SASUnit enables automated testing, quality control, and standardized HTML test reporting. This tutorial walks you through setting up your first project, writing a basic test, and analyzing the results. Key Concepts and Architecture

SASUnit structures testing hierarchically to keep your validation suits clean and trackable:

Test Scenarios: A collection of related test cases. The overall scenario passes only if every underlying test case succeeds.

Test Cases: Discrete units of validation targeting a specific program feature.

Assertions: Boolean logic checks embedded in the test case that compare actual outputs against expected results. Initial Setup and Installation

To begin working with the framework, follow these structural steps to set up your target directories:

Download the Suite: Head over to the official open-source distribution on the SASUnit SourceForge Files Page and download the unified distribution ZIP file.

Extract Components: Extract the framework files into a dedicated directory on your system where your SAS installation can interact with it.

Use the Startup Script: Run the built-in startup configuration utility to automatically provision your environment parameters and directory mappings. Writing Your First Test Scenario

SASUnit mirrors the industry-standard “Arrange-Act-Assert” pattern. In this simple example, we test a hypothetical macro called %add_numbers(a, b) which saves its result into a macro variable named &total..

Create a new SAS file named ts_math_validation.sas and use the following structural blueprint:

/1. Arrange & Initialize the Test Scenario / %initSASUnit(i_scenarioId=TS001, i_scenarioDesc=Validate Math Macro Output); / 2. Act: Call the unit under test / %add_numbers(a=5, b=10); / 3. Assert: Check if actual results match expectations / %assertMacroValue(i_expected=15, i_actual=&total., i_desc=Verify 5 plus 10 equals 15); / 4. Complete and close the scenario */ %endSASUnit(); Use code with caution. Common Assertion Macros

SASUnit provides specialized assertion tools tailored to unique data storage structures within SAS:

%assertMacroValue: Evaluates if the contents of a macro variable match a targeted target string or integer.

%assertColumns: Employs PROC COMPARE behavior to ensure column structural attributes align between datasets.

%assertLogMessage: Scans the underlying SAS log tracking text to identify expected notifications or capture unwanted system errors. Executing Tests and Viewing Results

You execute the tests in batch mode using the automated controller suite provided by the framework. Once finished, SASUnit outputs a comprehensive, interactive HTML log file report detailing execution times and precise coverage metrics. Green statuses denote passed scenarios, while red flags pinpoint exactly which assert blocks failed alongside links to the specific underlying SAS logs.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *