Tests Everywhere - COBOL
Tags: test, cobol, cobolcheck, tutorial
Published: 2023-12-11 09:45:30

{% github rogervinas/tests-everywhere no-readme %}

[COBOL](https://en.wikipedia.org/wiki/COBOL) testing [this simple Hello World](https://dev.to/rogervinas/tests-tests-everywhere-1b6h) with [cobol-check](https://github.com/openmainframeproject/cobol-check)

- [Show me the code](#show-me-the-code)
  - [Implementation](#implementation)
  - [Test](#test)
- [Run this project using 🐳 docker](https://github.com/rogervinas/tests-everywhere/tree/main/cobol#run-this-project-using--docker)
- [Run this project locally](https://github.com/rogervinas/tests-everywhere/tree/main/cobol#run-this-project-locally)

## Show me the code

### Implementation

1) Create a program `HELLO-CONSOLE` in [HELLO-CONSOLE.CBL](https://github.com/rogervinas/tests-everywhere/blob/main/cobol/src/main/cobol/HELLO-CONSOLE.CBL):

- Declare an alpha-numeric parameter `MSG`
- Program will just display `MSG`

```cobol
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-CONSOLE.

DATA DIVISION.
LINKAGE SECTION.
01 MSG PIC A(20).

PROCEDURE DIVISION USING MSG.
    DISPLAY MSG.
```

2) Create a program `HELLO` in [HELLO.CBL](https://github.com/rogervinas/tests-everywhere/blob/main/cobol/src/main/cobol/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](https://www.tutorialspoint.com/cobol/cobol_program_structure.htm) for `HELLO-MESSAGE` and a [program](https://www.tutorialspoint.com/cobol/cobol_program_structure.htm) for `HELLO-CONSOLE` just to show two different options and how to mock them in the tests.

```cobol
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.
```

### Test

Following [A Brief Example](https://github.com/openmainframeproject/cobol-check/wiki/A-Brief-Example), [Mock a paragraph](https://github.com/openmainframeproject/cobol-check/wiki/Mock-a-paragraph-and-provide-fake-results) and [Mock a CALL statement](https://github.com/openmainframeproject/cobol-check/wiki/Mock-a-CALL-statement-and-provide-fake-results) guides ...

In [HELLO.cut](https://github.com/rogervinas/tests-everywhere/blob/main/cobol/src/test/cobol/HELLO/HELLO.cut) file:

1) Test `HELLO-MESSAGE` paragraph:

```cobol
TestSuite "HELLO-MESSAGE"

TestCase "SHOULD RETURN HELLO WORLD"
  MOVE "" TO MSG
  PERFORM HELLO-MESSAGE
  Expect MSG = "HELLO WORLD!"
```

2) Test `HELLO` paragraph mocking `HELLO-MESSAGE` and `HELLO-CONSOLE`:

```cobol
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
```

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
================================================
```

I have no idea how to unit test `HELLO-CONSOLE` program as it seems [cobol-check](https://github.com/openmainframeproject/cobol-check) does not support `PROCEDURE DIVISION USING X` sections so I created [this issue](https://github.com/openmainframeproject/cobol-check/issues/339) 🤞

Happy Testing! 💙