DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • What Do You Mean by Debugging in C?
  • Event-Driven Fractals
  • Easily Update and Reload SSL for a Server and an HTTP Client
  • Writing a Chat With Akka

Trending

  • Revolutionizing Software Testing
  • Agile Estimation: Techniques and Tips for Success
  • REST vs. Message Brokers: Choosing the Right Communication
  • Software Verification and Validation With Simple Examples
  1. DZone
  2. Coding
  3. Languages
  4. The best code coverage for Scala

The best code coverage for Scala

Rado Buranský user avatar by
Rado Buranský
·
Feb. 12, 14 · Tutorial
Like (1)
Save
Tweet
Share
33.24K Views

Join the DZone community and get the full member experience.

Join For Free

the best code coverage metric for scala is statement coverage . simple as that. it suits the typical programming style in scala best. scala is a chameleon and it can look like anything you wish, but very often more statements are written on a single line and conditional “ if ” statements are used rarely. in other words, line coverage and branch coverage metrics are not helpful.

java tools

scala runs on jvm and therefore many existing tools for java can be used for scala as well. but for code coverage it’s a mistake to do so.

one wrong option is to use tools that measure coverage looking at bytecode like jacoco . even though it gives you a coverage rate number, jacoco knows nothing about scala and therefore it doesn’t tell you which piece of code you forgot to cover.

another misfortune are tools that natively support line and branch coverage metrics only. cobertura is a standard in java world and xml coverage report that it generates is supported by many tools. some scala code coverage tools decided to use cobertura xml report format because of its popularity. sadly, it doesn’t support statement coverage.

statement coverage

why? because a typical scala statement looks like this (a single line of code):
def f(l: list[int]) = l.filter(_ > 0).filter(_ < 42).takewhile(_ != 3).foreach(println(_))

neither line nor branch coverage works here. when would you consider this single line as being covered by a test? if at least one statement of that line has been called? maybe. or all of them? also maybe.

where is a branch? yes, there are statements that are executed conditionally, but the decision logic is hidden in internal implementation of list. branch coverage tools are helpless, because they don't see this kind of conditional execution.

what we need to know instead is whether individual statements like _ > 0 , _ < 42 or println(_) have beed executed by an automated test. this is the statement coverage.

scoverage to the rescue!

luckily there is a tool named scoverage . it is a plugin for scala compiler. there is also a plugin for sbt . it does exactly what we need. it generates html report and also own xml report containing detailed information about covered statements.

scoverage plugin for sonarqube

recently i have implemented a plugin for sonar 4 so that statement coverage measurement can become an integral part of your team's continuous integration process and a required quality standard. it allows you to review overall project statement coverage as well as dig deeper into sub-modules, directories and source code files to see uncovered statements.

project dashboard with scoverage plugin:
01_dashboard

multi-module project overview:
multi-module project overview:

columns with statement coverage, total number of statements and number of covered statements:
columns with statement coverage, total number of statements and number of covered statements:

source code markup with covered and uncovered lines:
source code markup with covered and uncovered lines:

Code coverage Scala (programming language)

Published at DZone with permission of Rado Buranský, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • What Do You Mean by Debugging in C?
  • Event-Driven Fractals
  • Easily Update and Reload SSL for a Server and an HTTP Client
  • Writing a Chat With Akka

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: