DEV Community

Cover image for Tests Everywhere - COBOL
Roger Viñas Alcon
Roger Viñas Alcon

Posted on

Tests Everywhere - COBOL

GitHub logo rogervinas / tests-everywhere

🤠 Tests, Tests Everywhere!

COBOL testing this simple Hello World with cobol-check

Show me the code

Implementation

1) 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.
Enter fullscreen mode Exit fullscreen mode

2) Create a program HELLO in HELLO.CBL:

  • Declare an alpha-numeric variable MSG
  • Create HELLO-MESSAGE paragraph that sets a value to MSG variable
  • Create HELLO paragraph calling:
    • HELLO-MESSAGE paragraph
    • HELLO-CONSOLE program
  • Create MAIN paragraph just calling the HELLO 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.
Enter fullscreen mode Exit fullscreen mode

Test

Following A Brief Example, Mock a paragraph and Mock a CALL statement guides ...

In HELLO.cut file:

1) Test HELLO-MESSAGE paragraph:

TestSuite "HELLO-MESSAGE"

TestCase "SHOULD RETURN HELLO WORLD"
  MOVE "" TO MSG
  PERFORM HELLO-MESSAGE
  Expect MSG = "HELLO WORLD!"
Enter fullscreen mode Exit fullscreen mode

2) Test HELLO paragraph mocking HELLO-MESSAGE and HELLO-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
Enter fullscreen mode Exit fullscreen mode

3) 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
================================================
Enter fullscreen mode Exit fullscreen mode

I have no idea how to unit test HELLO-CONSOLE program as it seems cobol-check does not support PROCEDURE DIVISION USING X sections so I created this issue 🤞

Happy Testing! 💙

Top comments (0)