Crafting Test-Driven Software with Python

Book description

Get to grips with essential concepts and step-by-step explanations to apply TDD practices to your Python projects while keeping your test suite under control

Key Features

  • Build robust Python applications using TDD and BDD methodologies
  • Test Python web applications using WebTest and web frameworks
  • Leverage PyTest to implement stringent testing mechanisms to ensure fault-tolerant applications

Book Description

Test-driven development (TDD) is a set of best practices that helps developers to build more scalable software and is used to increase the robustness of software by using automatic tests. This book shows you how to apply TDD practices effectively in Python projects.

You'll begin by learning about built-in unit tests and Mocks before covering rich frameworks like PyTest and web-based libraries such as WebTest and Robot Framework, discovering how Python allows you to embrace all modern testing practices with ease. Moving on, you'll find out how to design tests and balance them with new feature development and learn how to create a complete test suite with PyTest. The book helps you adopt a hands-on approach to implementing TDD and associated methodologies that will have you up and running and make you more productive in no time. With the help of step-by-step explanations of essential concepts and practical examples, you'll explore automatic tests and TDD best practices and get to grips with the methodologies and tools available in Python for creating effective and robust applications.

By the end of this Python book, you will be able to write reliable test suites in Python to ensure the long-term resilience of your application using the range of libraries offered by Python for testing and development.

What you will learn

  • Find out how tests can make your life easier as a developer and discover related best practices
  • Explore PyTest, the most widespread testing framework for Python
  • Get to grips with the most common PyTest plugins, including coverage, flaky, xdist, and picked
  • Write functional tests for WSGI web applications with WebTest
  • Run end-to-end tests for web applications using Robot Framework
  • Understand what test-driven development means and why it is important
  • Discover how to use the range of tools available in Python
  • Build reliable and robust applications

Who this book is for

This book is for Python developers looking to get started with test-driven development and developers who want to learn about the testing tools available in Python. Developers who want to create web applications with Python and plan to implement TDD methodology with PyTest will find this book useful. Basic knowledge of Python programming is required.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Crafting Test-Driven Software with Python
  3. Dedication
  4. Contributors
    1. About the author
    2. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  6. Section 1: Software Testing and Test-Driven Development
  7. Getting Started with Software Testing
    1. Technical requirements
    2. Introducing software testing and quality control
      1. Test plans
    3. Introducing automatic tests and test suites
      1. Multiple test cases
      2. Organizing tests
    4. Introducing test-driven development and unit tests
      1. Test-driven development
      2. Test units
    5. Understanding integration and functional tests
      1. Integration tests
      2. Functional tests
    6. Understanding the testing pyramid and trophy
      1. The testing pyramid
      2. The testing trophy
      3. Testing distribution and coverage
    7. Summary
  8. Test Doubles with a Chat Application
    1. Technical requirements
    2. Introducing test doubles
    3. Starting our chat application with TDD
    4. Using dummy objects
    5. Replacing components with stubs
    6. Checking behaviors with spies
    7. Using mocks
    8. Replacing dependencies with fakes
    9. Understanding acceptance tests and doubles
    10. Managing dependencies with dependency injection
      1. Using dependency injection frameworks
    11. Summary
  9. Test-Driven Development while Creating a TODO List
    1. Technical requirements
    2. Starting projects with TDD
    3. Building applications the TDD way
    4. Preventing regressions
    5. Summary
  10. Scaling the Test Suite
    1. Technical requirements
    2. Scaling tests
      1. Moving from e2e to functional
    3. Working with multiple suites
      1. Compile suite
      2. Commit tests
      3. Smoke tests
    4. Carrying out performance testing
    5. Enabling continuous integration
      1. Performance testing in the cloud
    6. Summary
  11. Section 2: PyTest for Python Testing
  12. Introduction to PyTest
    1. Technical requirements
    2. Running tests with PyTest
    3. Writing PyTest fixtures
      1. Using fixtures for dependency injection
    4. Managing temporary data with tmp_path
    5. Testing I/O with capsys
    6. Running subsets of the testsuite
    7. Summary
  13. Dynamic and Parametric Tests and Fixtures
    1. Technical requirements
    2. Configuring the test suite
    3. Generating fixtures
    4. Generating tests with parametric tests
    5. Summary
  14. Fitness Function with a Contact Book Application
    1. Technical requirements
    2. Writing acceptance tests
      1. Writing the first test
      2. Getting feedback from the product team
      3. Making the test pass
    3. Using behavior-driven development
      1. Defining a feature file
      2. Declaring the scenario
      3. Running the scenario test
      4. Further setup with the And step
      5. Performing actions with the When step
      6. Assessing conditions with the Then step
      7. Making the scenario pass
    4. Embracing specifications by example
    5. Summary
  15. PyTest Essential Plugins
    1. Technical requirements
    2. Using pytest-cov for coverage reporting
      1. Coverage as a service
    3. Using pytest-benchmark for benchmarking
      1. Comparing benchmark runs
    4. Using flaky to rerun unstable tests
    5. Using pytest-testmon to rerun tests on code changes
    6. Running tests in parallel with pytest-xdist
    7. Summary
  16. Managing Test Environments with Tox
    1. Technical requirements
    2. Introducing Tox
    3. Testing multiple Python versions with Tox
      1. Using environments for more than Python versions
    4. Using Tox with Travis
    5. Summary
  17. Testing Documentation and Property-Based Testing
    1. Technical requirements
    2. Testing documentation
      1. Adding a code-based reference
      2. Writing a verified user guide
    3. Property-based testing
      1. Generating tests for common properties
    4. Summary
  18. Section 3: Testing for the Web
  19. Testing for the Web: WSGI versus HTTP
    1. Technical requirements
    2. Testing HTTP
      1. Testing HTTP clients
    3. Testing WSGI with WebTest
    4. Using WebTest with web frameworks
    5. Writing Django tests with Django's test client
      1. Testing Django projects with pytest
      2. Testing Django projects with Django's test client
    6. Summary
  20. End-to-End Testing with the Robot Framework
    1. Technical requirements
    2. Introducing the Robot Framework
    3. Testing with web browsers
      1. Recording the execution of tests
      2. Testing with headless browsers
      3. Testing multiple browsers
    4. Extending the Robot Framework
      1. Adding custom keywords
      2. Extending Robot from Python
    5. Summary
  21. About Packt
    1. Why subscribe?
  22. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Leave a review - let other readers know what you think

Product information

  • Title: Crafting Test-Driven Software with Python
  • Author(s): Alessandro Molina
  • Release date: February 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781838642655