Home Code Coverage Code coverage guide: Using it with Codacy (Part 2)

Code coverage guide: Using it with Codacy (Part 2)

Author

Date

Category

In our last article we told you everything you need to know about code coverage, explaining the basics and benefits of using coverage analysis in your projects, while also going over concepts like branch coverage, decision coverage and statement coverage. As we’ve learned, there are lots of reasons why developers and development teams would want to use software testing in their projects.

The second part of this series will be all about setting up code coverage in Codacy, and how to reap the benefits of this powerful feature. In a few quick and easy steps we’ll get you up and running in no time. Let’s get started!

A guide to code coverage, part 1: Code coverage explained

Step 1: Generating code coverage

The first step might also be the most important one. Without having generated code coverage on your projects, it doesn’t make sense to set up reporting for it through Codacy, either โ€” it wouldn’t show anything of interest.

The way you generate code coverage depends on the language of your project, and it often involves using the right tool or installing a library. There are tons of tools out there that can help you with this, but we have some recommendations:

Java – JaCoCo
JavaScript – Istanbul
PHP – PHPUnit
Python – Coverage.py
Scala – Scoverage
Ruby – SimpleCov

If these don’t cut it for you, this overview of 25 code coverage tools might have something you like.

Step 2: Adding code coverage to your repository

Now that your project is actually generating test coverage, it’s time to configure it to show the relevant info right in Codacy. Again, the instructions for adding a test suite vary per code language, so be sure to follow the right one.

Java

We support two formats for showing Java code coverage in Codacy โ€” JaCoCo and Cobertura. Then it’s just two simple steps:

  1. Run the latest .jar that’s published here.
  2. Run the following command:
$ java -jar codacy-coverage-reporter-assembly-<version>.jar report -l Java -r jacoco.xml

JavaScript, TypeScript and CoffeeScript

If you’re using node.js, it’s extremely easy to get started. All you need to do is add the latest version of the codacy-coverage tool to your package.json by running the following terminal command:

npm install codacy-coverage --save

If you’re running mocha, make sure to add mocha-lcov-reporter to your package.json by running this command:

npm install mocha-lcov-reporter --save

PHP

Using PHP code coverage with Codacy is only supported if you use PHP 5.3 or later and supply data in Clover XML, the format used by PHPUnit, or PHPUnit XML for older PHPUnit versions.

If that sounds about right, set up codacy-coverage with Composer by adding the following to composer.json:

{ "require-dev": { "codacy/coverage": "dev-master" } }

Make sure you download the dependencies by running Composer in the directory of composer.json:

# install $ php composer.phar install --dev # update $ php composer.phar update codacy/coverage --dev

Grab the codacy-coverage library over on Packagist.

Then, add the autoloader to your PHP script by adding

'require_once 'vendor/autoload.php'

Python

Installing code coverage for Python is a total breeze. Just run this command and youโ€™re done!

pip install codacy-coverage

Scala

Code coverage for Scala is supported through an sbt plugin. To get started, add the scoverage and Codacy sbt plugins to your plugins.sbt file:

`resolvers += "Typesafe Repository" at "https://repo.typesafe.com/typesafe/releases/"

addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.3.5")

addSbtPlugin("com.codacy" % "sbt-codacy-coverage" % "")`

Ruby

Code coverage for Ruby isn’t hard to implement. You just need to include the following gem in your project:

gem 'codacy-coverage', :require => false gem 'codacy-coverage', :require

Then, initialize the reporter in the first line of your spec_helper by including:

`require 'codacy-coverage'

Codacy::Reporter.start`

Unsupported languages

Even if your language of choice isn’t listed, we still support code coverage reporting to Codacy โ€” it just requires a little more work on your end. You’ll need to send the coverage data directly through the API via POST by using the endpoint in our API Docs โ€” we only require the report is sent in JaCoCo or Cobertura.

This is an example of what your JSON could look like:

{ "total": 23, "fileReports": [ { "filename": "src/Codacy/Coverage/Parser/CloverParser.php", "total": 54, "coverage": { "3": 3, "7": 1 } } ] }

Step 3: Updating Codacy with code coverage

Now you’ll need to update Codacy by using your API token. Find it in Project -> Settings -> Integrations -> Project, and change it by running the following command in your terminal, replacing the placeholder %Project_Token% with your token:

export CODACY_PROJECT_TOKEN=%Project_Token%

Always make sure you protect your API token at all costs, and don’t share it with anyone you don’t trust. It’s an extremely valuable piece of information that can give anyone ownership over your projects, and that’s something you definitely want to avoid.

Step 4: Ensuring your code coverage data is sent to Codacy

Depending on your project’s language, there are some extra steps to take before you’re able to view the code coverage data in Codacy. Follow the remaining steps for the language of your choice:

Java

JavaScript, CoffeeScript, TypeScript

PHP

Python

Scala

Ruby

Step 5: Using the code coverage widget in Codacy

If you’ve followed all the steps up until this point and everything is properly configured, great job! You’re now able to stay up to date on your project’s coverage metrics right from the Codacy interface. In the background, all source coverage reports are now being converted into JSON files of which the results are shown in the project dashboard.

And… that’s it! If something isn’t working, you’re unsure about one of the above steps or have feedback to share about this feature, please reach out to our Support team โ€” they’ll happily assist you with any problems you might run into.

See out how Codacy stacks up against SonarQube

 

 


Codacy is used by thousands of developers to analyze billions of lines of code every day!

Getting started is easy โ€“ and free! Just use yourย ย GitHub, Bitbucket or Google account toย sign up.

GET STARTED

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Recent posts

How does code quality fit into your CI/CD pipeline?

Continuous Integration and Continuous Deployment (CI/CD) are key for organizations wanting to deliver software at scale. CI/CD allows developers to automate...

How Stim uses Codacy to achieve high-quality code

We spoke with Tobias Sjรถsten, Head of Software Engineering at Stim, about how Codacy helps them guarantee code quality and standardization...

6 things developers should do to ship more secure code

Writing better, more secure source code is fundamental to prevent potential exploits and attacks that could undermine your software applications. However,...

Best practices for security code reviews

In today's interconnected world, where data breaches and cyber threats are increasingly common, one of your top priorities should be to...

April Product Update ๐Ÿš€

Hi there ๐Ÿ‘‹ It's been a whirlwind month, and we have big news to share: