Skip to content

Writing Tests

Naming Requirements

Test Cases

In RPGUnit, a test case is an exported procedures that starts with the name test. This also means that you should not export procedures starting with test if they are not intended to be a test case.

Listed below are some example test case names that follow this naming convention:

  • test_getEmployeeDetail
  • test_getDeptDetail
  • testIsPalindrome
  • testFactorial

Test Suites

A test suite consists of one or more test cases in a file or source member. This test suite will be compiled into a service program in order to run the test cases. Although RPGUnit itself does not enforce an specific naming convention for test suites, the IBM i Testing extension does based on whether you are working on local files on your PC or source members on the IBM i.

  • Local Files: The file name must end in .test.rpgle, .test.sqlrpgle, .test.cblle, or .test.sqlcblle in order to be detected by the extension. File names are also not restricted to 10 characters as the extension will use the same rules that Source Orbit uses to handle long file names when compiling the test. Noteably, since all files must include .test, the resulting object name will start with T. These files can also be put in any directory.
  • Source Members: Source members can be named freely, but it is recommended to use some sort of prefix or suffix (ie. T, _T, T_, etc.) to avoid naming conflicts as the tests will be compiled into service programs. These source members can be put in any source file, but it is recommended to store them in a source file named QTESTSRC. By default this is where the extension will look for test suites in your library list. You can configure the extension to search other source files by going to the extension’s settings and searching for the Test Source Files setting. Test Source Files

Listed below are some example test suite names that follow this naming convention:

  • Local Files
    • employee.test.rpgle
    • department.test.rpgle
    • string.test.rpgle
    • math.test.rpgle
  • Source Members
    • QTESTSRC source file with members:
      • EMPLOYEET.RPGLE
      • DEPARTMENT.RPGLE
      • T_STRING.RPGLE
      • MATH_T.RPGLE

Generating Test Stubs

To simplify the process of writing tests, you can have the extension generate stubs for individual test cases or an entire test suite. To do this, place your cursor inside any export procedure and click the code action button (πŸ’‘). This will give you an option to generate a test case for the specific procedure you are in or to generate a test suite containing test cases for every procedure.

Generating Test Stubs

After selecting an option, a test suite will be created if it does not already exist with a scaffold that will include not only the test case stubs, but also the necessary includes and prototypes (if necessary). The generated test case stubs themselves will each be structured into the following sections: declarations, inputs, actual results, expected results, and assertions. As the developer, you will need to fill in the inputs and expected results.

Generated Test Stub

API Reference

In order to use the APIs provided by RPGUnit, you need to add the TESTCASE copybook to your test suite:

/include qinclude,TESTCASE

Setup and Teardown

Set Up Test Suite

Used to set up a test suite before the first test case is started. This procedure can be used to set up test data or allocate resources before the first test case is started.

Syntax

setUpSuite();

Example

dcl-proc setUpSuite export;
// Setup test suite code here
end-proc;

Assertions

Equality Assertion

Compares the given expected and actual values (can be a string, numeric, float, date, time, timestamp). The assertion fails, if both values are different. If the message parameter is specified, it is appeneded to the log.

Syntax

assertEqual( 'expected' : 'actual' [: 'message' );

Example

// βœ… Pass
assertEqual( 'Hello' : 'Hello' );
assertEqual( 123 : 123 );
// ❌ Fail
assertEqual( 123.45 : 123.46 );
assertEqual( D'2024-09-16' : D'2023-01-11' );
assertEqual( T'11.22.33' : T'22.33.44' );
assertEqual( Z'2024-09-16-11.22.33.123456' : Z'2024-01-11-11.22.33.123456' );

Utilities

Commonly Used APIs

Get Full Time Stamp

Returns the full current timestamp, without rounding the microseconds like %timestamp() does.

Syntax

getFullTimeStamp();

Return value

timestamp

Example

tmStmp = getFullTimeStamp();

Status Message APIs

Display Status Message

Displays a given status message in the status line at the bottom of the screen.

Syntax

displayStatusMessage( message );

Return value

void

Example

displayStatusMessage('Hello World!');

CL Command APIs

Clear Physical File

Uses CLRPFM to remove the data from member of file file. The file must be stored in *CURLIB.

Syntax

clrpfm( fileName [: memberName );

Return value

void

Example

clrpfm( 'MYFILE' : 'MYMEMBER' );

Source Type APIs

Get Member Type

Returns the source type of a given source member.

Syntax

getMemberType( sourceFile : library : member );

Return value

char(10)

Example

srcType = getMemberType('QINCLUDE': 'RPGUNIT': 'ASSERT');

Example Test Suites

To get started with some sample tests, check out these examples: