Skip to content

Automated tests for IBM i applications

B2Bnetwork team · · 7 min

IBM i, RPG and mainframe

In brief

IBM i applications are tested automatically at four levels: unit tests of RPG procedures in service programs (for example in RPGUnit), tests of batch runs by comparing output data with an SQL query, tests of REST services exposed from IBM i, and tests of 5250 screens where there is no other way. Data comparison usually gives the most, because it needs no changes to old code. Tests run in a separate library with a copy of the data, selected through the job's library list.

Why many companies have no tests

IBM i applications were often built in times when a test meant running the program on test data and checking the result on the screen. The logic is mixed with screen handling, the programs are long, and the test data is a copy of production from several years ago.

The effect shows with every change: a price list fix needs a week of manual testing, and still it happens that after deployment the nightly run calculates something differently. Automated tests do not have to cover the whole system right away. It is enough to start with the processes whose change costs the most nerves. We describe how to choose what to test first in the article Risk-based testing: how to decide what to test first.

The environment: libraries and the library list

IBM i has a mechanism that makes testing much easier: the job's library list. A program looks for files and other programs in libraries in the order of this list, so the same code can work on test data if the test library sits higher in the list than the production one, or replaces it.

A typical setup:

LibraryContents
Test programs libraryPrograms compiled from a git branch
Test data libraryA copy of tables with data prepared for testing
Results libraryOutput data from a run, to be compared
Baseline libraryExpected results from the previous, verified version

Before each test run, the data is restored from the baseline, for example with the CRTDUPOBJ command or by saving and restoring the library (SAVLIB, RSTLIB). This way every run starts from the same state. Test data should not contain real personal data, as we describe in the article Test data and the GDPR: anonymization, masking, and synthetic data.

Four levels of tests

1. Unit tests of procedures

A procedure in a service program that takes parameters and returns a result can be tested like a function in any other language. For RPG there are open libraries from the RPGUnit family: a test is a procedure in a test program that calls the code and checks the result with an assertion. Unit tests make sense for new and rewritten code, for example for a discount calculation procedure, tax ID (NIP) validation, or document numbering.

A good set of unit tests checks not only the typical case but also edges and refusals. For a discount procedure with a threshold of 1,000 PLN, these will be at least: an amount just below the threshold, exactly at the threshold, above the threshold, zero, and a negative value. For tax ID validation: a correct number, a wrong checksum, too few digits, spaces, and hyphens. For years, such cases surfaced only in production, because a manual test checked one typical invoice.

2. Comparing data after a run

For old code that cannot easily be split into procedures, a black box test works best: prepared input data, running the process, comparing the results with a baseline. The comparison is done by one SQL query:

(SELECT 'tylko nowe' AS roznica, n.*
   FROM testwyn.faktury n
 EXCEPT
 SELECT 'tylko nowe', w.*
   FROM testwzr.faktury w)
UNION ALL
(SELECT 'tylko wzorzec', w.*
   FROM testwzr.faktury w
 EXCEPT
 SELECT 'tylko wzorzec', n.*
   FROM testwyn.faktury n);

An empty result means that after the change the run calculates the same. Each row shows a difference to be explained. When a table has columns that change with every run, for example timestamps, the query lists only the columns to compare instead of an asterisk.

3. REST service tests

Services exposed from IBM i through IWS or Node.js are tested like any API: with an API testing tool, using valid, edge-case, and invalid data. The tests run outside IBM i, so they can be written in tools the team already knows. How to check that a change to a service will not break its clients is described in the article API contract testing.

4. 5250 screen tests

Green screens can be tested automatically through an emulator driven by a script that sends keystrokes and reads fields at specific positions. Such tests are brittle and slow, so it is worth limiting them to a few of the most important paths and testing the logic at a lower level.

Tests in CI

When the sources are in git, tests can run automatically on every change. The CI server connects to IBM i over SSH, compiles the changed sources into a test library, restores the data from the baseline, runs the unit tests and the batch runs, and then the comparison queries. The result goes into the code review. Code for IBM i and build tools such as Better Object Builder help with compilation.

A full nightly run can take too long for every change. In that case, code review triggers the unit tests and a short data set, and the full comparison runs once a day. We discuss planning regression for frequent releases in the article Regression testing with weekly releases: how to plan it, and an example of regression automation in a mature system is shown in the scenario regression automation for a policy system.

How to start in an existing system

The first tests are worth building in a few weeks, not planning for a quarter.

  1. Choose one high-risk process, for example discount calculation or the daily close.
  2. Prepare a library with test data and a baseline of results from the current version.
  3. Write the comparison query and run it after the run.
  4. Add unit tests for the procedures you are changing in this process.
  5. Hook everything into CI and extend it to further processes.

After a few processes, the team has a safety net that lets it change code without a week of manual testing. The same set is useful during modernization, described in the article IBM i modernization: refactoring, APIs, or migration.

Questions and answers

Is it possible to write unit tests in RPG?

Yes. Open libraries from the RPGUnit family let you write tests as RPG procedures with assertions and run them with a command or from the editor. The condition is code split into procedures in service programs that take parameters and return a result. An old program in which the logic is mixed with the screen cannot be unit tested without extracting that logic.

How do you test old code that we do not want to change?

With a black box test on data: prepare input data, run the program or batch process, and compare the result with a baseline using an SQL query with EXCEPT. Such tests need no changes to the code and protect well against regression. The baseline is created from the current version, verified by the business, and refreshed after every deliberate change of results.

Where do you get test data on IBM i?

Most often from a copy of production after anonymization, or from data prepared by hand for specific cases. A copy gives realistic cases but requires removing personal data and is large. Hand-prepared data is small and predictable but may miss unusual records. In practice, both sources are combined and kept in the baseline library.

Can IBM i tests be run in GitHub Actions or Jenkins?

Yes. The CI server connects to IBM i over SSH, which runs in the PASE environment, and runs the compilation, tests, and comparison queries on it. The results return to CI as in any other project. You need a user profile with permissions only for the test libraries, so that CI has no access to production data.

What it looks like in a project

Developing a system on IBM i or mainframe? Talk to an RPG engineer.

Let's talk

Read more: IBM i, RPG and mainframe