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 Solution Explorer, right-click the name of the new project, then select Add > New Item.
  3. On the Add New Item dialog box, select Test Program, type a name for the test program, then click Add.
    Restriction: Do not use a test program name of mfunit, as this will not run within the Micro Focus Unit Testing Framework.

    A new test program is created, and within it, the stub-code for your first test case.

  4. This short example test uses a data file, and so add the following immediately after the line program-id. program-name:
           environment division.
           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).
  5. Add the following 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.

  6. Add the following 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.

  7. 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
  8. Add the following immediately after the line entry MFU-TC-TEARDOWN-PREFIX & TEST-program-name:
           close cust

    The teardown section tidies up the environment.

You should now build the project, then you can run it from the Micro Focus Unit Testing window.