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.
-
Before you edit the code, we will configure the project so that it can locate the required data file:
-
In
COBOL Explorer, select
TestAirportDemo
, then select
.
The
Properties for TestAirportDemo dialog box appears.
-
Click
.
-
On the Environment Variables page, click
Add, then add the following values and click
OK:
- Variable:
dd_airports
- Value:
..\..\AirportDemo\airports.dat
-
Click
OK.
Test cases within the unit test project will now use the original data file used by the AirportDemo application.
-
In
COBOL Explorer, double-click
DistanceTest.cbl.
The program appears in the editor.
-
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).
-
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.
-
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
-
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.
-
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.
An
MFU-TC-TEARDOWN-PREFIX & TEST-DISTANCETEST entry point is added to the program.
-
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.
-
To run the test case, in the
Micro Focus Unit Testing view, click
(Rerun Tests From Last Run).
The test case reruns, but should fail with the following output: