Enhancing the Test

The passing test case does not currently run any meaningful tests on the source code, and so you are going to enhance the test so that it does. You will add code that runs the calculation of distance between two airports, and then check that the correct distance is returned.
  1. In Application Explorer view, double-click MFUWS_aircode.cpy.

    The copybook used for the working storage appears in the editor.

  2. The test case code will use a variable called errormessage, and so add the following to the copybook, and then save it:
           01 errormessage pic x(200).
  3. Double-click MFUPD_aircode.cpy to open the copybook used to add the test case to the Procedure Division of the program under test. Within MFUPD_aircode.cpy, replace the existing lines of code with the following:
               entry "MFUT_TESTAIRCODE".
               allocate lnk-airport1
               allocate lnk-airport2             
               allocate lnk-distance-result 
               move "LHR" to lnk-airport1 *> London Heathrow
               move "SEA" to lnk-airport2 *> Seattle
    
               perform distance-between-airports.            
    
              *> Assertions to check that the correct distance is returned
               if function numval(distance-miles) not equal 4787            
                   string "Incorrect distance in miles returned - " 
                           distance-miles delimited by size                 
                           x"0" delimited by size
                           into errormessage
                   end-string
                   call MFU-ASSERT-FAIL-Z using errormessage
               end-if
    
               if function numval(distance-km) not equal 4787
                   string "Incorrect distance in kilometers returned - " 
                           distance-km delimited by size
                           x"0" delimited by size
                           into errormessage
                   end-string
                   call MFU-ASSERT-FAIL-Z using errormessage
               end-if
    
               goback
           .

    This code makes a call to the code under test, which performs the distance-between-airports section with the given airport codes for Heathrow and Seattle. It then makes assertions based on the results of the perform operation.

  4. Now, in the same copybook, create a new entry point that serves as the test case setup, named MFU-TC-SETUP-PREFIX & TEST-TESTAIRCODE:
           entry MFU-TC-SETUP-PREFIX & "TESTAIRCODE"
               perform open-airfile.
               goback
           .

    This entry point will be run first during the test run, and will attempt to open the data file used for the test case.

  5. Finally, create a new entry point that serves as the test case teardown, named MFU-TC-TEARDOWN-PREFIX & TEST-TESTAIRCODE:
           entry  MFU-TC-TEARDOWN-PREFIX & "TESTAIRCODE"
               perform close-airfile.
               goback
           .

    This entry point will be run last during the test run, and will close the data file after the test case has run.

  6. Press Ctrl+S to save the changes.
  7. Right-click the TestAirportDemo project, and then click Run As > Run Configurations.

    This opens the Run Configurations dialog box.

  8. Select the TestAirportDemo run configuration, and then select the Environment tab.
  9. On the Environment page, click Add.

    This opens the Add Variable dialog box.

  10. In the Variable field, type dd_airports.
  11. In the Value field, type ../../AirportDemo/airports.dat.
  12. Click OK.

    Test cases within this unit test project will now use the original data file used by the AirportDemo application.

  13. Click Run.

    The test case reruns, but should fail with the following output:

    The next step is to debug the test, to find out why it failed.