Adding a test to a test case

After the z/OS® Automated Unit Testing Framework (ZUnit) test case wizard generates a test case template, you add tests to it by editing the file and adding code that calls the program under test, makes API calls to run the tests, and returns the test results to the test runner.

Before you begin

  • Create a test case template by using the New COBOL z/OS Automated Unit Testing Framework (ZUnit) Test Case Template wizard or the New PL/I z/OS Automated Unit Testing Framework (ZUnit) Test Case Template wizard.
  • Open the test case file in an editor.

About this task

For each test entry that you add to a ZUnit test case, the wizard generates a subprogram with the entry point name that you specify in the wizard. The following code block illustrates the subprogram that is created for an entry that is called TEST001 in a COBOL test case:
      *+---------------------------------------------------------------+
      *| TEST001                                                       |
      *|     A Test (supply more detail).                              |
      *|                                                               |
      *| @param TEST-CASE-PTR (input),                                 |
      *|     A pointer-by-value to an area maintained by the           |
      *|     ZUnit Test Runner that identifies the Test Case           |
      *|     and associated resources.                                 |
      *|                                                               |
      *| @param TEST-FIXTURE-PTR (input),                              |
      *|     A pointer-by-value to a user-defined structure,           |
      *|     established previously in the setup procedure, that       |
      *|     represents the Test Fixture.                              |
      *|                                                               |
      *| @param TEST-NAME-PTR (input),                                 |
      *|     A pointer-by-value to an area containing the name         |
      *|     of the Test for which a Test Fixture should be            |
      *|     allocated.                                                |
      *|                                                               |
      *| @param TEST-NAME-LEN (input),                                 |
      *|     A integer-by-value that specifies the length in           |
      *|     bytes of the value contained in parameter                 |
      *|     TEST-NAME-PTR.                                            |
      *+---------------------------------------------------------------+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. 'TEST001'.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       1 FAIL-MESSAGE-TXT PIC X(254).
       1 FAIL-MESSAGE-LEN PIC S9(9) COMP-5.
       LINKAGE SECTION.
       1 TEST-CASE-PTR    POINTER.
       1 TEST-FIXTURE-PTR POINTER.
       1 TEST-NAME-PTR    POINTER.
       1 TEST-NAME-LEN    PIC S9(9) COMP-5.
       1 TEST-NAME        PIC X(254).
       PROCEDURE DIVISION USING BY VALUE TEST-CASE-PTR
                          BY VALUE TEST-FIXTURE-PTR
                          BY VALUE TEST-NAME-PTR
                          BY VALUE TEST-NAME-LEN.
           SET ADDRESS OF TEST-NAME TO TEST-NAME-PTR
      * TODO: Modify Test program 'TEST001' until it passes.
           DISPLAY 'TEST001(' TEST-NAME(1:TEST-NAME-LEN) ')'
           CALL 'AZUASTFA' USING BY VALUE TEST-CASE-PTR
           .
       END PROGRAM 'TEST001'.

Procedure

To create a test, modify the following sections of this subprogram.

  • Program header: Modify the program header to describe the test to be run and the expected results.
    If you are testing the program that is called ANAGRAM, for example, which compares two strings and determines whether they are anagrams, you might create two tests: one that tests a valid anagram and one that tests an invalid anagram. For the valid anagram test, you might alter the program header as in the following example:
          *+---------------------------------------------------------------+
          *| TEST001                                                       |
          *|     Test a valid anagram.                                     |
          *|                                                               |
  • Procedure division: Alter the procedure division so that it calls the program under test.
    The following code block tests the ANAGRAM program.
           PROCEDURE DIVISION USING BY VALUE TEST-CASE-PTR
                              BY VALUE TEST-FIXTURE-PTR
                              BY VALUE TEST-NAME-PTR
                              BY VALUE TEST-NAME-LEN.
          *    display test name on entry
               SET ADDRESS OF TEST-NAME TO TEST-NAME-PTR
               DISPLAY TEST-NAME(1:TEST-NAME-LEN) ' Started...'
          *    establish addressability to test fixture
               SET ADDRESS OF ANAGRMIO TO TEST-FIXTURE-PTR
          *    test a valid anagram
               CALL 'ANAGRAM' USING ANAGRMIO
               IF NOT-ANAGRAM OR INPUT-ERROR
                  MOVE 1 TO FAIL-MESSAGE-LEN
                  STRING
                     'ANAGRAM failed to detect that "'
                     FIRST-WORD(1:FIRST-WORD-LEN)
                     '" is an anagram of "'
                     SECOND-WORD(1:SECOND-WORD-LEN) '"'
                     DELIMITED BY SIZE INTO FAIL-MESSAGE-TXT
                     WITH POINTER FAIL-MESSAGE-LEN
                  END-STRING
                  SUBTRACT 1 FROM FAIL-MESSAGE-LEN
          *       throw an assertion exception (ends test)
                  CALL 'AZUASTFM' USING BY VALUE TEST-CASE-PTR
                                  BY REFERENCE FAIL-MESSAGE-TXT
                                  BY VALUE FAIL-MESSAGE-LEN
               END-IF
          * display test name on exit
               DISPLAY TEST-NAME(1:TEST-NAME-LEN) ' Successful.'