Example - Creating a Test Case

The following example creates a simple test case that includes setup code, a simple test that writes to a data file, and a teardown that tidies up the test at the end. You can enhance this test further by including your own test assertions in the main test case section.
  1. Create a unit test project (see link below).
  2. In COBOL Explorer, ensure that the project is selected, then click File > New > COBOL Unit Test.

    The New COBOL Unit Test dialog box appears.

  3. In the Containing project field, ensure it states the name of your unit test project.
  4. In the New file name field, type the name of your test fixture program.
  5. Select Create Unit Test from Template, then select Micro Focus template.
  6. Click Finish.

    The test fixture file is created within the unit test project, and includes the skeleton code required for one test case.

  7. This short example test uses a data file, and so add the following to the configuration section:
    input-output section.
           file-control.
           select cust assign to 'cust.txt'
           organization is line sequential.

    And add the following immediately after the line data division:

           file section.
           fd cust.
           01 cust-file.
              03 customer-id    pic 9(5).
              03 customer-info  pic x(65).
  8. Add the following statement immediately after the line entry MFU-TC-SETUP-PREFIX & TEST-program-name.
           open output cust

    This sets up the test by opening the file, ready for any file operations performed in the test case itself.

    The MFU-TC-METADATA-SETUP-PREFIX & TEST-program-name entry point contains some details about the test case.

  9. Edit the first statement of the MFU-TC-METADATA-SETUP-PREFIX & TEST-program-name entry point to read:
    move "This is a simple test to write to a data file" to MFU-MD-TESTCASE-DESCRIPTION
  10. Add the following code within the test case, which is immediately after the line entry MFU-TC-PREFIX & TEST-program-name:
           move 0 to customer-id
           perform 100 times
            add 1 to customer-id
            move "A customer" to customer-info
            write cust-file
           end-perform.

    The test case performs a simple write operation on the file. if this fails, the test case is marked as failed. This is also the section in which you would add your own test assertions; see Determining a test outcome for more information.

  11. Add the following statement immediately after the line entry MFU-TC-TEARDOWN-PREFIX & TEST-program-name:
           close cust.

    The teardown section tidies up the environment.

    The test case is now complete, ready to be run; refer to Running Unit Tests.