DevOps Your Skill: Code Coverage
DevOps Your Skill: Code Coverage
In this article, we discuss how to add code coverage with Codecov to your Alexa Skill pipeline and its importance in the day-to-day Alexa Skills development.
Join the DZone community and get the full member experience.Join For Free
Without a doubt, one of the aspects a developer should pay more attention to is trying to always generate understandable, maintainable, and clear code — in short, to generate clean code.
During the development of code (modules, libraries) it is important to integrate objective tools that measure the status of the code and provide the information to know its quality and thus be able to detect and prevent problems: duplicate functions, excessively complex methods, code low quality, non-standard coding style.
This code coverage report is automated in the continuous integration system (CircleCI) and are executed in each new version of the software.
Here you have the technologies used in this project
- ASK CLI - Install and configure ASK CLI.
- CircleCI Account - Sign up here.
- Codecov Account - Register here.
- Node.js v10.x.
- Visual Studio Code.
Codecov provides highly integrated tools to group, merge, archive and compare coverage reports. Whether your team is comparing changes in a pull request or reviewing a single commit, Codecov will improve the code review workflow and quality. Codecov supports the most commons CICD engine like the one we use, CircleCI.
As the official Codecov documentation says, here are the things you'll need or want to have in place before using Codecov:
- Sign up on codecov.io and link either your GitHub, GitLab, or Bitbucket account.
- Once linked, Codecov will automatically sync all the repositories to which you have access. You can click on an organization from your dashboard to access its repositories, or navigate directly to a specific repository using: https://codecov.io/\/<account-name>/<repo-name>. Example: https://codecov.io/gh/xavidop/alexa-nodej-lambda-helloworld.
- Get the token of this repo generated by Codecov that we will use in our pipeline.
Now, we can go back to our code to set up the project
After this, we can install
codecov Node.js uploader library using npm.
--save-dev is used to save the package for development purposes. Example: unit tests, minification
It is important to notice that the code coverage report will be generated by an external tool called
nyc that its output will be used by this uploader library in order to upload it to Codecov. Let's go deeper!
Once we have
codecov Node.js uploader library now we have to configure it.
In order to use the uploader library, we need to set an environment variable called
CODECOV_TOKEN with the value that Codecov has generated in the previous step.
After that, we have to create the
codecov.yml configuration file in the root of our repository with this content:
src/ subfolder. So we have to change the folder of the report to set the correct path starting from the root directory of the repo.
With this YAML file, Codecov will change all file paths from
lambda/custom/src. This change is needed if you want to navigate between files in the Codecov UI online. This is not a problem only with Codecov, I have the same issue with Coveralls tool for example.
Once we have everything configured, we have to set up the report we are going to use to check the Code Coverage.
We will use the npm package
nyc which is the CLI tool of the famous npm package
You can install
nyc using npm.
--save-dev is used to save the package for development purpose. Example: linters, minification.
This npm package has an option to set the output format of the report to a
.lcov type which is the type that
codecov Node.js uploader library needs to upload to Codecov website.
After the report has been updated to Codecov, this is how this report looks like:
Now, it is time to integrate it into our
package.json to use it in our pipeline with
npm run command!
So, in this file, we are going to add the following commands in the
script JSON node:
codecov: this command will execute the Code Coverage report and uploads it to Codecov:
nyc npm test && nyc report --reporter=text-lcov > coverage.lcov && codecov
Everything is fully installed, configured, and integrated, let's add it to our pipeline!
This job will execute the following tasks:
- Restore the code that we have downloaded in the previous step in
npm run codecovto execute the Code Coverage and then upload it to Codecov.
- Persist again the code that we will reuse in the next job
NOTE: Remember to set the
CODECOV_TOKEN environment variable in CircleCI.
- DevOps Wikipedia - Wikipedia reference
- Official Codecov Documentation - Codecov Documentation
- Official CircleCI Documentation - Official CircleCI Documentation
A minimum of quality and focus our efforts on testing is the most complicated and important parts of our business. Tools like Codecov help us to achieve these goals and are fundamental in continuous improvement environments.
That’s all, folks!
You can find the code in my GitHub.
I hope it will be useful! If you have any doubts or questions, do not hesitate to contact me or put a comment below.
Opinions expressed by DZone contributors are their own.