Enhancing the Test

As mentioned earlier, the passing test case does not currently run any meaningful tests on the source code, and so we are going to enhance the test so that it does. We are going to:
  • Add the required data file to the project;
  • Configure the test setup to open the file;
  • Add code to the test case to search for two airports within the file and calculate the distance between them;
  • Add assertions to the test case based on the distance calculated;
  • Configure the test teardown to close the file.
  1. Copy the original data file from the AirportConsoleApplication project to the TestAirportLinkLibrary project, then use an application configuration file to locate it at run time:
    1. In Solution Explorer, right-click the airports.dat file in the AirportConsoleApplication project, and select Copy.
    2. Right-click the project name TestAirportLinkLibrary and select Paste.

      The data file is added to the unit test project.

    3. Right-click the data file, select Rename, then type TestAirportLinkLibrary and press Enter.

      The data file is renamed.

    4. Right-click TestAirportLinkLibrary.dat and select Properties.
    5. In the Properties pane, ensure Copy to Output Directory is set to Copy if newer.

      This ensures that if the file is edited, the most up-to-date version is always available in the output directory, to be used at run time.

    6. In Solution Explorer, right-click the TestAirportLinkLibrary project, point to Add and select New Item.
    7. Select Application Configuration File, then click Add.

      The configuration file is added to the project.

    8. Double-click Application1.mfgcf.

      The Application Settings dialog box appears.

    9. Click Add, and then in the Variable field, type dd_airports, and in the Value field, type .\TestAirportLinkLibrary.dat, then press Return and OK.
  2. Ensure TestAIRCODE.cbl is open 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-TESTAIRCODE 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
                           by reference lnk-file-status
               end-call
    
               *> did we open the file?
               if lnk-file-status not equal "00"
                   call MFU-ASSERT-FAIL-Z using TEST-TESTAIRCODE & " failed to open file" & x"0"
                   exhibit named lnk-file-status
               end-if

    This code makes a call to the code under test, and if it cannot open the file, fails the test and outputs the failed file status.

  5. Now jump to the MFU-TC-PREFIX & TEST-TESTAIRCODE 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. Immediately after the assertions, add the following code:
            exhibit named distance-miles
            exhibit named distance-km

    This code outputs the results of the calculations to the Output pane, displayed in the Micro Focus Unit Testing window.

  8. Finally, position the cursor immediately after the test setup section (just before the InitializeLinkageData section), type testdown, press Tab, and then select TEST-TESTAIRCODE from the list of level-78 items offered (there is only one at this point).

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

  9. 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
                           by reference lnk-file-status
               end-call
    
               *> did we close the file?
               if lnk-file-status not equal "00"
                   call MFU-ASSERT-FAIL-Z using TEST-TESTAIRCODE & " failed to close file" & x"0"
                   exhibit named lnk-file-status
               end-if

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

  10. To run the test, in the Micro Focus Unit Testing window, click Run All.

    The test case fails, and outputs the following to the Test Results pane:

The next task is to debug the test case to find out why it failed.