Developer Zone

Advanced Software Development with MATLAB

Cov’ed Code All Throughout the Interwebs

You may remember that MATLAB can generate code coverage information in the Cobertura format now. Been able to since R2017b. This is great since this allows us to leverage a richer ecosystem of capabilities and tools that are out there.

One of these is the great codecov.io service. This service provides gorgeous views and, better yet, meaningful insight into your code coverage. Guess what? It supports the Cobertura format. Take a look at that last coverage post to remind yourself how we can create this Cobertura style coverage data from the MATLAB side. That's pretty much all you need. As it turns out, the good folks over at codecov.io seem to be hard at work making it super simple to get this going for your GitHub repos.

Once you have generated the coverage information, your CI system just needs one more build step to run their uploader script to codecov. This looks something like this as per their documentation:

bash <(curl -s https://codecov.io/bash)

There's not even a need to point to your coverage file or anything, codecov finds the Cobertura file you've generated in your workspace and automagically detects it and uploads it to boot.

Let's see this in action for a real repo. I took a quick peak around to find a good representative code repository, and I found and forked the Nonlinear Estimation Toolbox. I actually don't know much about what this toolbox does apart from the description they provide in the README.md file and on their Bitbucket site. However, me being me, I did notice that they have a nice collection of unit tests!

With nice unit tests usually comes nice coverage. Let's check it out for this repo with the help of Cobertura output and codecov.io. Step one is you need to create your account with codecov.

They allow doing this via GitHub, Bitbucket or GitLab:

From there you will want to add the repository you'd like to report your coverage on:

Once you add your desired repo (I made an unchanged fork of the Nonlinear Estimation Toolbox to get this to work for me), codecov.io will give you your API token for the coverage upload script.

Alright, now you have a token linked to your GitHub repo, and then all you need to do in your CI build is run that bash script as a build step after the Cobertura is generated. The way I did this was by using the EnvInject Jenkins plugin to create the CODECOV_TOKEN environment variable with the token value provide to me from the codecov site. remember, you don't want to share your token so this should be done privately as I've done below:

Then you just set up your bash script and run your job:

You've made it when you see some nice high quality ASCII art followed by some pleasant sounding (success implying) messages:

...and there we go. That's it! from now on you'll have all your coverage data up on codecov.io. For this repo, I was right that they have some pretty solid, 90.34% code coverage. What's more is that this tool makes it really easy to see areas not covered and add the appropriate testing. Check it out:

Does that seem as easy (and powerful) to you as it does to me? Are you using codecov.io or another coverage service out there? Interested to hear your experiences.

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.