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 calculates the distance between two airports, and then check that the correct distance is returned.
  1. Before you edit the code, we will configure the project so that it can locate the required data file:
    1. In COBOL Explorer, select TestAirportDemo , then select Project > Properties.

      The Properties for TestAirportDemo dialog box appears.

    2. Click Micro Focus > Run-time Configuration > Environment Variables.
    3. On the Environment Variables page, click Add, then add the following values and click OK:
      • Variable: dd_airports
      • Value: ..\..\AirportDemo\airports.dat
    4. Click OK.
    Test cases within the unit test project will now use the original data file used by the AirportDemo application.
  2. In COBOL Explorer, double-click DistanceTest.cbl.

    The program appears in the editor.

  3. The test case code will use a variable called errormessage, and so add the following to the working-storage section:
           01 errormessage pic x(200).
  4. Jump to the MFU-TC-SETUP-PREFIX & TEST-DISTANCETEST entry point, and then after the comment line that reads *> Add any other test setup code here, add the following code:
               move 4 to lnk-function
               call "aircode" using
                           by value lnk-function
                           by value lnk-airport1
                           by value lnk-airport2
                           by value lnk-prefix-text
                           by reference lnk-rec
                           by reference lnk-distance-result
                           by reference lnk-matched-codes-array                       
               end-call

    This code attempts to open the data file.

  5. Now jump to the MFU-TC-PREFIX & TEST-DISTANCETEST entry point, and add the following lines of code before the first call statement:
             move 2 to lnk-function
             move "LHR" to lnk-airport1 *> London Heathrow
             move "SEA" to lnk-airport2 *> Seattle
  6. Within the same section, after the comment line that reads *> Verify the outputs here, add the following code:
              *> 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

    These are the assertions that check that the correct values are calculated for the distance (in miles and km) between the two airports, and if the calculation is incorrect, the test fails.

  7. Finally, position the cursor immediately after the test setup section (just before the $end-region marker), type testteardown, press Ctrl+Space, press Enter, then select TEST-DISTANCETEST from the list of level-78 items offered.

    GUID-377B1360-F869-4492-A005-7D1C0249B82E-low.png

    An MFU-TC-TEARDOWN-PREFIX & TEST-DISTANCETEST entry point is added to the program.

  8. Within this section, add the following code:
               move 5 to lnk-function
               call "aircode" using
                           by value lnk-function
                           by value lnk-airport1
                           by value lnk-airport2
                           by value lnk-prefix-text
                           by reference lnk-rec
                           by reference lnk-distance-result
                           by reference lnk-matched-codes-array
               end-call

    This closes the data file after the test case has run.

  9. To run the test case, in the Micro Focus Unit Testing view, click GUID-A4B28D20-7C9D-4513-A75A-A97646269FDB-low.png (Rerun Tests From Last Run).

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

    GUID-5134EEE6-2BD2-460F-96FE-97861B15788B-low.png