COBOL testing with cobol-check
- Create a program
HELLO-CONSOLE
in HELLO-CONSOLE.CBL:
- Declare an alpha-numeric parameter
MSG
- Program will just display
MSG
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-CONSOLE.
DATA DIVISION.
LINKAGE SECTION.
01 MSG PIC A(20).
PROCEDURE DIVISION USING MSG.
DISPLAY MSG.
- Create a program
HELLO
in HELLO.CBL:
- Declare an alpha-numeric variable
MSG
- Create
HELLO-MESSAGE
paragraph that sets a value toMSG
variable - Create
HELLO
paragraph calling:HELLO-MESSAGE
paragraphHELLO-CONSOLE
program
- Create
MAIN
paragraph just calling theHELLO
paragraph
💡 We use a paragraph for HELLO-MESSAGE
and a program for HELLO-CONSOLE
just to show two different options and how to mock them in the tests.
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MSG PIC A(20).
PROCEDURE DIVISION.
MAIN.
PERFORM HELLO.
STOP RUN.
HELLO.
PERFORM HELLO-MESSAGE.
CALL 'HELLO-CONSOLE' USING MSG.
HELLO-MESSAGE.
MOVE 'HELLO WORLD!' TO MSG.
Following A Brief Example, Mock a paragraph and Mock a CALL statement guides ...
In HELLO.cut file:
- Test
HELLO-MESSAGE
paragraph:
TestSuite "HELLO-MESSAGE"
TestCase "SHOULD RETURN HELLO WORLD"
MOVE "" TO MSG
PERFORM HELLO-MESSAGE
Expect MSG = "HELLO WORLD!"
- Test
HELLO
paragraph mockingHELLO-MESSAGE
andHELLO-CONSOLE
:
TestSuite "HELLO"
TestCase "SHOULD DISPLAY HELLO MESSAGE"
* 2.1 Mock HELLO-MESSAGE
MOCK PARAGRAPH HELLO-MESSAGE
MOVE "HELLO TEST!" TO MSG
END-MOCK
* 2.2 Mock HELLO-CONSOLE
MOCK CALL 'HELLO-CONSOLE' USING MSG
CONTINUE
END-MOCK
* 2.3 Execute the paragraph we want to test
PERFORM HELLO
* 2.4 Verify MSG has the expected value
Expect MSG = "HELLO TEST!"
* 2.5 Verify HELLO-MESSAGE has been called once
VERIFY PARAGRAPH HELLO-MESSAGE HAPPENED ONCE
* 2.6 Verify HELLO-CONSOLE has been called once
VERIFY CALL 'HELLO-CONSOLE' USING MSG HAPPENED ONCE
- Test output should look like:
TESTSUITE:
HELLO-MESSAGE
PASS: 1. SHOULD RETURN HELLO WORLD
TESTSUITE:
HELLO
PASS: 2. SHOULD DISPLAY HELLO MESSAGE
PASS: 3. VERIFY EXACT 1 ACCESS TO PARAGRAPH HELLO-MESSAGE
PASS: 4. VERIFY EXACT 1 ACCESS TO CALL 'HELLO-CONSOLE'
4 TEST CASES WERE EXECUTED
4 PASSED
0 FAILED
0 CALLS NOT MOCKED
================================================
I have no idea how to unit test HELLO-CONSOLE
program as it seems cobol-check does not support PROCEDURE DIVISION USING X
section so I created this issue 🤞
Run this project using 🐳 docker
- Execute
./docker-run.sh
- Once inside the container:
- Test with
./cobolcheck -p HELLO
- Build
HELLO-CONSOLE.so
withcobc src/main/cobol/HELLO-CONSOLE.CBL
- Build
HELLO
withcobc -x src/main/cobol/HELLO.CBL
- Run with
./HELLO
- Test with
- Java 8 required to run cobol-check 0.2.8
- You can try to build GnuCOBOL from scratch or ...
- Install any pre-built GnuCOBOL package:
- Test with
./cobolcheck -p HELLO
- Build
HELLO-CONSOLE.so
withcobc src/main/cobol/HELLO-CONSOLE.CBL
- Build
HELLO
withcobc -x src/main/cobol/HELLO.CBL
- Run with
./HELLO
- Get cobol-check and unzip it locally
- If needed customize config.properties
- Remove samples and place your own files under
src/main/cobol
andsrc/test/cobol