This is the second post in my series on RPG and COBOL development using Visual Studio Code and Code for IBM i. Today’s topic is Code Coverage.
What is Code Coverage?
This stackoverflow answer explains precisely what Code Coverage is:
Code coverage is a measurement of how many lines/blocks/arcs of your code are executed while the automated tests are running.
A good tool will give you not only the percentage of the code that is executed, but also will allow you to drill into the data and see exactly which lines of code were executed during a particular test.
This is exactly what Code for IBM i can do with the Code Coverage extension.
How do I install it?
Code Coverage for Code for IBM i is currently in early access, available for those who support its development with Code for IBM i Pro subscriptions. After the early access waiting period, we will release it for free to all. It will also be available on GitHub!
You will need to do the following:
- Install code coverage on your IBM i. Believe it or not, I couldn’t find a homepage for IBM i Code Coverage from Googling. I did find this PDF from IBM containing a slide with the PTF requirements. See slide 28 on this PDF.
- If you’re new to Code for IBM i, read our introductory post for installation instructions.
- Install the Code Coverage for IBM i extension into Visual Studio Code.
When you’ve got everything installed into Visual Studio Code, you will see a new extension available in the left-most panel:
Before we get started, make sure that the
CODECOV command is on your library list. In any modern install of Code Coverage, it is usually installed into
QGPL, but sometimes it ends up in
QDEVTOOLS. Make sure the correct library is on your library list.
To make sure it works, let’s create a source member and then compile it:
**FREE Dcl-s anumber int(5); anumber = 5; if (anumber = 10); dsply 'The number is 10!'; else; if (anumber = 5); //This is the only DSPLY that should execute. dsply 'The number is 5!'; else; dsply 'The number is not 5!'; endif; endif; Return;
Creating and running a test
Since we have the Code Coverage extension installed, you can right-click on most types of sources to create a new Coverage Test. In this case, I called my source member
test1.rpgle, and right-clicking on it shows me I can create a new Coverage Test. Upon clicking that menu item, a new wizard will appear when you can enter information about the test—mostly prefilled. I will leave it as the defaults and save it:
If you head over to the Code Coverage extension tab, you will see your newly created test there. When you click on it, it will run the test in the background. As it runs you will see the spinning icon, which indicates it is running. When the test is finished running, it will show you all the results of the test. The results include a list of sources that the Coverage Test ran across and what percentage of that code was executed. You are able to click on a source to show the coverage line-by-line.
If you want to run the test again, you can use the rerun button, which has the refresh-styled icon. (This will rerun all open tests!)
What you have seen here were the very basics. Code Coverage is most powerful when used in combination with unit tests or functional tests. We could add many programs and service programs into the mix to make much larger tests.
You can find out more about IBM i, Visual Studio Code, and Code Coverage at the following links:
For training on this or any other IBM i development technology—including APIs or open source on IBM i—contact us.