RPG: Writing Unit Test Procedures for Testing Other RPG Code

Overview of RPG Unit Testing

Unit testing in RPG involves creating test procedures that test individual units or components of your code. These tests help to identify issues early in the development process and ensure that your code behaves as expected.

Step 1: Create a Test Harness

A test harness is an RPG program that runs your unit tests and reports the results. The harness should include:

  • A main procedure to execute the tests.
  • Subprocedures to call your unit tests and record the results.
  • A mechanism for reporting test results, such as displaying them on the console or writing them to a log file.

Here’s a simple test harness example:

**free
ctl-opt main(MainProcedure);

dcl-proc MainProcedure;
  dcl-s totalTests int(10) inz(0);
  dcl-s failedTests int(10) inz(0);

  TestProcedure1(totalTests, failedTests);
  TestProcedure2(totalTests, failedTests);
  // Add more tests as needed

  dsply ('Total tests: ' + %char(totalTests));
  dsply ('Failed tests: ' + %char(failedTests));
end-proc;

Step 2: Write Unit Test Procedures

Write unit test procedures to test specific functionality in your RPG code. Each test should:

  • Test one aspect of the code being tested.
  • Have a clear pass or fail outcome.
  • Not rely on other tests or external factors.

Here’s an example of a unit test procedure that tests the Add function:

dcl-proc TestProcedure1;
  dcl-pi *n;
    totalTests int(10) value;
    failedTests int(10) value;
  end-pi;

  dcl-s result int(10);

  totalTests += 1;
  result = Add(3, 4);

  if (result <> 7) then;
    failedTests += 1;
    dsply ('TestProcedure1 failed');
  endif;
end-proc;

Step 3: Write the Code to Be Tested

Write the RPG code that you want to test. It’s a good practice to write your code as modular, reusable subprocedures, which makes it easier to test and maintain.

Here’s an example of a simple addition function:

dcl-proc Add;
  dcl-pi *n int(10);
    a int(10) value;
    b int(10) value;
  end-pi;

  return a + b;
end-proc;

Step 4: Add More Unit Tests

As your application grows, add more unit tests to cover new functionality and edge cases. This ensures that your code remains reliable and robust as it evolves.

Here’s an example of another unit test procedure:

dcl-proc TestProcedure2;
  dcl-pi *n;
    totalTests int(10) value;
    failedTests int(10) value;
  end-pi;

  dcl-s result int(10);

  totalTests += 1;
  result = Add(-2, 2);

  if (result <> 0) then;
    failedTests += 1;
    dsply ('TestProcedure2 failed');
  endif;
end-proc;

Step 5: Run the Test Harness

Compile and run the test harness to execute your unit tests and review the results. Fix any issues identified by the tests and rerun the harness until all tests pass.

To compile and run the test harness:

CRTBNDRPG PGM(MYLIB/TESTHARNESS) SRCFILE(MYLIB/QRPGLESRC) SRCMBR(TESTHARNESS) DBGVIEW(*SOURCE)

CALL PGM(MYLIB/TESTHARNESS)

Conclusion

In this tutorial, we’ve shown you how to write RPG unit test procedures to test other RPG code effectively. By using a test harness and modular test procedures, you can ensure the accuracy and reliability of your RPG applications on the IBMi platform. As your application grows, continue to add more unit tests to cover new functionality and edge cases, ensuring your code remains robust and maintainable.

Leave a comment