0%

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
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. 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
    3. Test plans
    4. Introducing automatic tests and test suites
    5. Multiple test cases
    6. Organizing tests
    7. Introducing test-driven development and unit tests
    8. Test-driven development
    9. Test units
    10. Understanding integration and functional tests
    11. Integration tests
    12. Functional tests
    13. Understanding the testing pyramid and trophy
    14. The testing pyramid
    15. The testing trophy
    16. Testing distribution and coverage
    17. 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
    11. Using dependency injection frameworks
    12. 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
    3. Moving from e2e to functional
    4. Working with multiple suites
    5. Compile suite
    6. Commit tests
    7. Smoke tests
    8. Carrying out performance testing
    9. Enabling continuous integration
    10. Performance testing in the cloud
    11. Summary
  11. Section 2: PyTest for Python Testing
  12. Introduction to PyTest
    1. Technical requirements
    2. Running tests with PyTest
    3. Writing PyTest fixtures
    4. Using fixtures for dependency injection
    5. Managing temporary data with tmp_path
    6. Testing I/O with capsys
    7. Running subsets of the testsuite
    8. 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
    3. Writing the first test
    4. Getting feedback from the product team
    5. Making the test pass
    6. Using behavior-driven development
    7. Defining a feature file
    8. Declaring the scenario
    9. Running the scenario test
    10. Further setup with the And step
    11. Performing actions with the When step
    12. Assessing conditions with the Then step
    13. Making the scenario pass
    14. Embracing specifications by example
    15. Summary
  15. PyTest Essential Plugins
    1. Technical requirements
    2. Using pytest-cov for coverage reporting
    3. Coverage as a service
    4. Using pytest-benchmark for benchmarking
    5. Comparing benchmark runs
    6. Using flaky to rerun unstable tests
    7. Using pytest-testmon to rerun tests on code changes
    8. Running tests in parallel with pytest-xdist
    9. Summary
  16. Managing Test Environments with Tox
    1. Technical requirements
    2. Introducing Tox
    3. Testing multiple Python versions with Tox
    4. Using environments for more than Python versions
    5. Using Tox with Travis
    6. Summary
  17. Testing Documentation and Property-Based Testing
    1. Technical requirements
    2. Testing documentation
    3. Adding a code-based reference
    4. Writing a verified user guide
    5. Property-based testing
    6. Generating tests for common properties
    7. Summary
  18. Section 3: Testing for the Web
  19. Testing for the Web: WSGI versus HTTP
    1. Technical requirements
    2. Testing HTTP
    3. Testing HTTP clients
    4. Testing WSGI with WebTest
    5. Using WebTest with web frameworks
    6. Writing Django tests with Django's test client
    7. Testing Django projects with pytest
    8. Testing Django projects with Django's test client
    9. Summary
  20. End-to-End Testing with the Robot Framework
    1. Technical requirements
    2. Introducing the Robot Framework
    3. Testing with web browsers
    4. Recording the execution of tests
    5. Testing with headless browsers
    6. Testing multiple browsers
    7. Extending the Robot Framework
    8. Adding custom keywords
    9. Extending Robot from Python
    10. 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
3.133.108.241