Property-Based Testing with PropEr, Erlang, and Elixir

Book description

Property-based testing helps you create better, more solid tests with little code. By using the PropEr framework in both Erlang and Elixir, this book teaches you how to automatically generate test cases, test stateful programs, and change how you design your software for more principled and reliable approaches. You will be able to better explore the problem space, validate the assumptions you make when coming up with program behavior, and expose unexpected weaknesses in your design. PropEr will even show you how to reproduce the bugs it found. With this book, you will be writing efficient property-based tests in no time.

Most tests only demonstrate that the code behaves how the developer expected it to behave, and therefore carry the same blind spots as their authors when special conditions or edge cases show up. Learn how to see things differently with property tests written in PropEr.

Start with the basics of property tests, such as writing stateless properties, and using the default generators to generate test cases automatically. More importantly, learn how to think in properties. Improve your properties, write custom data generators, and discover what your code can or cannot do. Learn when to use property tests and when to stick with example tests with real-world sample projects. Explore various testing approaches to find the one that's best for your code. Shrink failing test cases to their simpler expression to highlight exactly what breaks in your code, and generate highly relevant data through targeted properties. Uncover the trickiest bugs you can think of with nearly no code at all with two special types of properties based on state transitions and finite state machines.

Write Erlang and Elixir properties that generate the most effective tests you'll see, whether they are unit tests or complex integration and system tests.

What You Need

  • Basic knowledge of Erlang, optionally Elixir
  • For Erlang tests: Erlang/OTP >= 20.0, with Rebar >= 3.4.0
  • For Elixir tests: Erlang/OTP >= 20.0, Elixir >= 1.5.0

Publisher resources

View/Submit Errata

Table of contents

  1.  Acknowledgments
  2.  Introduction
    1. Who Is This Book For
    2. Why Both Elixir and Erlang
    3. What’s in This Book
    4. How to Read This Book
    5. About the Code
    6. Online Resources
  3. Part I. The Basics
    1. 1. Foundations of Property-Based Testing
      1. Promises of Property-Based Testing
      2. Properties First
      3. Property-Based Testing in Your Project
      4. Running a Property
      5. Wrapping Up
    2. 2. Writing Properties
      1. Structure of Properties
      2. Default Generators
      3. Putting It All Together
      4. Wrapping Up
    3. 3. Thinking in Properties
      1. Modeling
      2. Generalizing Example Tests
      3. Invariants
      4. Symmetric Properties
      5. Putting It All Together
      6. Wrapping Up
    4. 4. Custom Generators
      1. The Limitations of Default Generators
      2. Gathering Statistics
      3. Basic Custom Generators
      4. Fancy Custom Generators
      5. Wrapping Up
  4. Part II. Stateless Properties in Practice
    1. 5. Responsible Testing
      1. The Specification
      2. Thinking About Program Structure
      3. CSV Parsing
      4. Filtering Records
      5. Employee Module
      6. Templating
      7. Plumbing It All Together
      8. Wrapping Up
    2. 6. Properties-Driven Development
      1. The Specification
      2. Writing the First Test
      3. Testing Specials
      4. Implementing Specials
      5. Negative Testing
      6. Wrapping Up
    3. 7. Shrinking
      1. Re-centering with ?SHRINK
      2. Dividing with ?LETSHRINK
      3. Wrapping Up
    4. 8. Targeted Properties
      1. Understanding Targeted Properties
      2. Targeted Properties in Practice
      3. Thinking Outside the Box
      4. Wrapping Up
  5. Part III. Stateful Properties
    1. 9. Stateful Properties
      1. Laying Out Stateful Properties
      2. How Stateful Properties Run
      3. Writing Properties
      4. Testing a Basic Concurrent Cache
      5. Testing Parallel Executions
      6. Wrapping Up
    2. 10. Case Study: Bookstore
      1. Introducing the Application
      2. Writing Generators
      3. Broad Stateful Testing
      4. Precise Stateful Modeling
      5. Refining the Tests
      6. Debugging Stateful Properties
      7. Parallel Tests
      8. Wrapping Up
    3. 11. State Machine Properties
      1. Laying Out State Machine Properties
      2. How State Machine Properties Run
      3. Writing Properties
      4. Testing a Circuit Breaker
      5. Modeling the Circuit Breaker
      6. Adjusting the Model
      7. Wrapping Up
  6. A1. Solutions
    1. Writing Properties
    2. Thinking in Properties
    3. Custom Generators
    4. Shrinking
    5. Stateful Properties
  7. A2. Elixir Translations
    1. Thinking in Properties
    2. Responsible Testing
    3. Stateful Properties
    4. Case Study: Bookstore
    5. State Machine Properties
  8. A3. Installing PostgreSQL
  9. A4. Generators Reference

Product information

  • Title: Property-Based Testing with PropEr, Erlang, and Elixir
  • Author(s): Fred Hebert
  • Release date: January 2019
  • Publisher(s): Pragmatic Bookshelf
  • ISBN: 9781680506549