SE Radio 589: Zac Hatfield-Dodds on Property-Based Testing in Python

Topics covered
Popular Clips
Episode Highlights
Ghostwriting
Hypothesis offers a powerful feature known as ghostwriting, which automates the generation of test cases for Python functions. explains that this tool can inspect modules like NumPy and write test cases for hundreds of functions by analyzing their names and arguments 1. This process involves type inference and custom logic to generate suitable test inputs, even without type annotations.
We have this strategy which will generate instances of a type. And so we'll create that strategy, and then we'll look at its representation, which kind of tells you how it was defined, and we can just substitute that into the source code.
---
Compared to other tools like GitHub Copilot, ghostwriting in Hypothesis is more adept at understanding code semantics and finding rare edge cases 2.
Fuzz Testing
Fuzz testing in Hypothesis is a robust method for uncovering bugs by generating random inputs and observing their effects on code. Zac highlights that this technique is particularly effective for finding defects that require multiple conditions to be met 3. Unlike traditional fuzzers like AFL, Hypothesis's fuzzing mode is tailored for Python and integrates deeply with test suites, allowing for more efficient input mutations.
We say, say we have 1000 test functions, we don't need to run each of them on a separate cpu core. We actually run each of them for a few seconds at a time and measure how much progress we're making.
---
This approach maximizes bug discovery rates by dynamically optimizing test execution 4.
Explain Mode
Explain Mode in Hypothesis enhances debugging by providing insights into which parts of the code are executed during tests. Zac describes how this feature can pinpoint the exact lines of code responsible for test failures, aiding developers in understanding and resolving issues 5. This mode leverages coverage data to highlight code segments related to both passing and failing inputs.
Coverage is the idea that we can observe which lines of code were executed by our tests.
---
Additionally, Hypothesis mitigates common issues in randomized testing, such as flakiness, by saving test inputs in a local database for consistent re-testing 6.
Related Episodes


SE-Radio Episode 322: Bill Venners on Property Based Tests
Answers 383 questions

SE Radio 568: Simon Bennetts on OWASP Dynamic Application Security Testing Tool ZAP
Answers 383 questions

SE-Radio Episode 256: Jay Fields on Working Effectively with Unit Tests
Answers 383 questions

Episode 516: Brian Okken on Testing in Python with pytest
Answers 383 questions

SE Radio 572: Gregory Kapfhammer on Flaky Tests
Answers 383 questions

SE-Radio Episode 283: Alexander Tarlinder on Developer Testing
Answers 383 questions

Episode 474: Paul Butcher on Fuzz Testing
Answers 383 questions

SE Radio 637: Steve Smith on Software Quality
Answers 383 questions

SE-Radio Episode 324: Marc Hoffmann on Code Test Coverage Analysis and Tools
Answers 383 questions
SE Radio 633: Itamar Friedman on Automated Testing with Generative AI
Answers 383 questions

Episode 431: Ken Youens-Clark on Learning Python
Answers 383 questions

SE Radio 581: Zach Lloyd on Terminal Emulators
Answers 383 questions
SE Radio 632: Goran Petrovic on Mutation Testing at Google
Answers 383 questions
SE Radio 599: Jason C. McDonald on Quantified Tasks
Answers 383 questions














