Line or Branch Coverage: Which Type of Coverage is Right for You? - Codecov (2024)

Line or Branch Coverage: Which Type of Coverage is Right for You? - Codecov (1)

One of the best ways to track testing on your team is with code coverage. But, there are many coverage types: line, branch, and function to name a few. Although most modern coverage tools output both line and branch coverage, it can be confusing to determine which one to use.

In this article, we’ll answer

  • what line and branch coverage are,
  • how to fully cover branches,
  • and which type of coverage to use (trick question, it’s both!)

What is line coverage?

Line coverage is a basic version of test coverage. When a test suite is run, code coverage records which lines of code were hit. Line coverage, thus, is the total number of lines run divided by the number of lines in the codebase.

Take, for example, this simple calculator in python.

def add(a, b): return a + bdef divide(a, b): if b == 0: return "undefined" return a/b

We have two functions, add and divide. Both take in two numbers a and b. But in the divide function, notice that we check to see if b is zero. This makes sense as we don’t want to divide by 0.

Let’s mock out two tests, one for each function

def test_add(): assert add(1, 2) == 3def test_divide(): assert divide(1, 2) == 0.5

If we run the tests above, we would expect the following green lines to be covered

def add(a, b): return a + bdef divide(a, b): if b == 0: return "undefined" return a/b

Notice that the if statement is run, but the return below is not run. This is due to the line not being run.

In our scenario, the coverage percentage would be 83.3% since 5 lines are run out of the 6 total.

What is branch coverage?

Branches typically on if statements, when there are 2 paths to take from an evaluation. Branch coverage, thus, measures the number of branches taken over the total number of branches. Let’s revisit our example from above.

In our code, we only have one if statement that has two branches: true and false. Either

b == 0

or

b != 0

Our tests only account for the second case, so we have 50% branch coverage.

What would happen if we wanted to increase our branch coverage to 100%? We would need to run the b == 0 case. Let’s see what our tests would look like.

def test_add(): assert add(1, 2) == 3def test_divide(): assert divide(1, 2) == 0.5 assert divide(1, 0) == "undefined"

Now, we will be running by branches and our branch coverage would be 100%. Consequently, our line coverage would also be 100%.

Covering branches

Our example above looks at a simple if statement. But not all of them have only 2 branches. For a statement like A && (B || C), coverage calculation tests each possible combination of results.

ABCResult
TrueTrueTrueTrue
TrueTrueFalseTrue
TrueFalseTrueTrue
TrueFalseFalseFalse
FalseTrueTrueFalse
FalseTrueFalseFalse
FalseFalseTrueFalse
FalseFalseFalseFalse

However, many tools will do lazy calculations as covering all cases can be cumbersome and unnecessary. In these cases, it will overlook ambiguous values. For example, our previous example would shrink down to

ABCResult
TrueTrue?True
TrueFalseTrueTrue
TrueFalseFalseFalse
False??False

In this case, if 4 cases are written matching the above logic statements, we would see 100% branch coverage. This helps developers from having to explicitly write out all 8 paths. In this way, branch coverage can be a powerful way to account for edge cases.

Which type of coverage should you use?

As alluded to at the start, this is a trick question. If you want to get the most out of your code coverage, you should be using both line and branch coverage. Getting line coverage is important to track that all lines are being run. But tracking branch coverage helps to make sure that you aren’t missing edge cases.

Codecov will automatically merge both types of coverage if given the information. But it’s important to note that semi-covered branches are marked as partials and partials are not considered hits when calculating coverage.

If you are just starting with code coverage, it might be too challenging to invest in branch coverage immediately. But if you are comfortable with your line coverage, dive into branch coverage to really hone your testing practices.

Previous Post

Getting Started with the Codecov API v2

Next Post

Product Update: Codecov API V2, Building on Feedback, and Improved Repo Onboarding

Line or Branch Coverage: Which Type of Coverage is Right for You? - Codecov (2024)
Top Articles
Convert Pounds to Pence
How to Verify a PGP Signature: 14 Steps (with Pictures) - wikiHow
AMC Theatre - Rent A Private Theatre (Up to 20 Guests) From $99+ (Select Theaters)
How To Fix Epson Printer Error Code 0x9e
It's Official: Sabrina Carpenter's Bangs Are Taking Over TikTok
What spices do Germans cook with?
Trabestis En Beaumont
Bin Stores in Wisconsin
Ofw Pinoy Channel Su
South Park Season 26 Kisscartoon
oklahoma city for sale "new tulsa" - craigslist
Overzicht reviews voor 2Cheap.nl
Magic Mike's Last Dance Showtimes Near Marcus Cedar Creek Cinema
Prices Way Too High Crossword Clue
Seafood Bucket Cajun Style Seafood Restaurant in South Salt Lake - Restaurant menu and reviews
Wordle auf Deutsch - Wordle mit Deutschen Wörtern Spielen
Mlb Ballpark Pal
WWE-Heldin Nikki A.S.H. verzückt Fans und Kollegen
Fairy Liquid Near Me
Overton Funeral Home Waterloo Iowa
Playgirl Magazine Cover Template Free
Hell's Kitchen Valley Center Photos Menu
Second Chance Maryland Lottery
Mahpeople Com Login
Drago Funeral Home & Cremation Services Obituaries
Nordstrom Rack Glendale Photos
Van Buren County Arrests.org
Maxpreps Field Hockey
Lisas Stamp Studio
At&T Outage Today 2022 Map
Yugen Manga Jinx Cap 19
Crossword Help - Find Missing Letters & Solve Clues
Accuweather Minneapolis Radar
Renfield Showtimes Near Paragon Theaters - Coral Square
Gen 50 Kjv
Uncovering the Enigmatic Trish Stratus: From Net Worth to Personal Life
Myaci Benefits Albertsons
Vip Lounge Odu
Craigs List Tallahassee
Roto-Rooter Plumbing and Drain Service hiring General Manager in Cincinnati Metropolitan Area | LinkedIn
Myfxbook Historical Data
Tugboat Information
FREE - Divitarot.com - Tarot Denis Lapierre - Free divinatory tarot - Your divinatory tarot - Your future according to the cards! - Official website of Denis Lapierre - LIVE TAROT - Online Free Tarot cards reading - TAROT - Your free online latin tarot re
Memberweb Bw
Ehome America Coupon Code
Expendables 4 Showtimes Near Malco Tupelo Commons Cinema Grill
Take Me To The Closest Ups
Egg Inc Wiki
1Tamilmv.kids
Heat Wave and Summer Temperature Data for Oklahoma City, Oklahoma
De Donde Es El Area +63
Latest Posts
Article information

Author: Jonah Leffler

Last Updated:

Views: 6385

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Jonah Leffler

Birthday: 1997-10-27

Address: 8987 Kieth Ports, Luettgenland, CT 54657-9808

Phone: +2611128251586

Job: Mining Supervisor

Hobby: Worldbuilding, Electronics, Amateur radio, Skiing, Cycling, Jogging, Taxidermy

Introduction: My name is Jonah Leffler, I am a determined, faithful, outstanding, inexpensive, cheerful, determined, smiling person who loves writing and wants to share my knowledge and understanding with you.