Skip to Content
Hypermodern Python Tooling
book

Hypermodern Python Tooling

by Claudio Jolowicz
June 2024
Intermediate to advanced
270 pages
6h 40m
English
O'Reilly Media, Inc.
Content preview from Hypermodern Python Tooling

Chapter 7. Measuring Coverage with Coverage.py

How confident in a code change are you when your tests pass?

If you look at tests as a way to detect bugs, you can describe their sensitivity and specificity.

The sensitivity of your test suite is the probability of a test failure when there’s a defect in the code. If large parts of the code are untested, or if the tests don’t check for expected behavior, you have low sensitivity.

The specificity of your tests is the probability that they will pass if the code is free of defects. If your tests are flaky (they fail intermittently) or brittle (they fail when you change implementation details), then you have low specificity. Invariably, people stop paying attention to failing tests. This chapter isn’t about specificity, though.

There’s a great way to boost the sensitivity of your tests: when you add or change behavior, write a failing test before the code that makes it pass. If you do this, your test suite will capture your expectations for the code.

Another effective strategy is to test your software with the various inputs and environmental constraints that you expect it to encounter in the real world. Cover the edge cases of a function, like empty lists or negative numbers. Test common error scenarios, not just the “happy path.”

Code coverage is a measure of the extent to which the test suite exercises your code. Full coverage doesn’t guarantee high sensitivity: if your tests cover every line in your code, you can still have bugs. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Python, 6th Edition

Learning Python, 6th Edition

Mark Lutz

Publisher Resources

ISBN: 9781098139575Errata Page