0%

Elixir offers new paradigms, and challenges you to test in unconventional ways. Start with ExUnit: almost everything you need to write tests covering all levels of detail, from unit to integration, but only if you know how to use it to the fullest - we'll show you how. Explore testing Elixir-specific challenges such as OTP-based modules, asynchronous code, Ecto-based applications, and Phoenix applications. Explore new tools like Mox for mocks and StreamData for property-based testing. Armed with this knowledge, you can create test suites that add value to your production cycle and guard you from regressions.

Write Elixir tests that you can be proud of. Dive into Elixir's test philosophy and gain mastery over the terminology and concepts that underlie good tests. Create and structure a comprehensive ExUnit test suite, starting from the basics, and build comprehensive test coverage that will provide safety for refactoring and confidence that your code performs as designed. Use tests to make your software more reliable and fault tolerant.

Explore the basic tool set provided by ExUnit and Mix to write and organize your test suite. Test code built around different OTP functionality. Isolate your code through dependency injection and by using Mox. Write comprehensive tests for Ecto projects, covering Ecto as a database tool as well as a standalone data validation tool. Test Phoenix channels from end to end, including authentication and joining topics. Write Phoenix controller tests and understand the concepts of integration testing in Elixir. Learn property-based testing with StreamData from the author who wrote the library.

Code with high confidence that you are getting the most out of your test suite, with the right tools that make testing your code a pleasure and a valuable part of your development cycle.

What You Need:

To get the most out of this book, you will need to have installed Elixir 1.8 or later and Erlang/OTP 21 or later. In order to complete the relevant chapters, you will also need Ecto 3.1 or later, EctoSQL 3.1 or later and Phoenix 1.3 or later.

Table of Contents

  1.  Acknowledgments
    1. Andrea Leopardi
    2. Jeffrey Matthias
  2.  Introduction
    1. Why Do We Need a Book for Testing in Elixir?
    2. Who This Book Is For
    3. How to Read This Book
    4. About the Code
    5. Online Resources
  3. 1. Unit Tests
    1. Defining the Unit in Unit Test
    2. Testing with ExUnit
    3. Organizing Your Tests
    4. Creating Comprehensive Test Coverage
    5. Testing Pure Functions
    6. Refactoring Toward Pure Functions
    7. Isolating Code
    8. Wrapping Up
  4. 2. Integration and End-to-End Tests
    1. What Is an Integration Test?
    2. Testing Against Real External Dependencies
    3. Dependency Doubles
    4. Interfacing to External Dependencies with Behaviours
    5. Test Doubles: Stubs, Mocks, and Fakes
    6. The Hidden Benefits of Dependency Doubles
    7. Testing the Actual Interaction with Services
    8. End-to-End Tests
    9. Wrapping Up
  5. 3. Testing OTP
    1. Testing a GenServer
    2. Controlling the Life Cycle of OTP Processes in Tests
    3. Testing Periodic Actions
    4. Testing Singleton Resources
    5. Testing Resiliency
    6. Wrapping Up
  6. 4. Testing Ecto Schemas
    1. Testing Your Schema Through Changesets
    2. Refactoring to Increase Test Maintainability
    3. Creating a SchemaCase for Shared Test Code
    4. Testing an Ecto Schema as a Data Validator
    5. Testing an Ecto Schema for Database Interactions
    6. Testing Your Schema Through Database Calls
    7. Setting Up Sandbox Mode
    8. Wrapping Up
  7. 5. Testing Ecto Queries
    1. Creating a Factory to Help with Setup
    2. Adding a DataCase to Help with Setup
    3. Testing Create
    4. Testing Read
    5. Testing Update
    6. Testing Delete
    7. Wrapping Up
  8. 6. Testing Phoenix
    1. The Role of Phoenix in Your Application
    2. Testing JSON-Based APIs
    3. Testing Server-Rendered HTML Applications
    4. Testing Phoenix Channels
    5. Wrapping Up
  9. 7. Property-Based Testing
    1. Property-Based Testing in Practice in the Elixir Standard Library
    2. Example-Based Tests
    3. Introducing Randomness and Property-Based Testing
    4. Data Generation
    5. Writing Properties
    6. Shrinking
    7. Strategies for Designing Properties
    8. Stateful Property-Based Testing
    9. Wrapping Up
  10. A1. When To Randomize Test Data
  11. A2. Test Life Cycle
    1. The Life Cycle of an ExUnit Suite
    2. Test Cases
    3. Executing Tests
    4. An Example and a Drawing
  12. A3. Test Coverage
    1. Built-In Test Coverage
    2. Coveralls and the Excoveralls Library
  13.  Bibliography
3.17.154.171