pytest Quick Start Guide

Book description

Learn the pytest way to write simple tests which can also be used to write complex tests

Key Features

  • Become proficient with pytest from day one by solving real-world testing problems
  • Use pytest to write tests more efficiently
  • Scale from simple to complex and functional testing

Book Description

Python's standard unittest module is based on the xUnit family of frameworks, which has its origins in Smalltalk and Java, and tends to be verbose to use and not easily extensible.The pytest framework on the other hand is very simple to get started, but powerful enough to cover complex testing integration scenarios, being considered by many the true Pythonic approach to testing in Python.

In this book, you will learn how to get started right away and get the most out of pytest in your daily work?ow, exploring powerful mechanisms and plugins to facilitate many common testing tasks. You will also see how to use pytest in existing unittest-based test suites and will learn some tricks to make the jump to a pytest-style test suite quickly and easily.

What you will learn

  • Write and run simple and complex tests
  • Organize tests in fles and directories
  • Find out how to be more productive on the command line
  • Markers and how to skip, xfail and parametrize tests
  • Explore fxtures and techniques to use them effectively, such as tmpdir, pytestconfg, and monkeypatch
  • Convert unittest suites to pytest using little-known techniques
  • Use third-party plugins

Who this book is for

This book is for Python programmers that want to learn more about testing. This book is also for QA testers, and those who already benefit from programming with tests daily but want to improve their existing testing tools.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. pytest Quick Start Guide
  3. Packt Upsell
    1. Why subscribe?
  4. Foreword
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. 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
  7. Introducing pytest
    1. Why spend time writing tests? 
    2. A quick look at the unittest module
    3. Why pytest?
    4. Summary
  8. Writing and Running Tests
    1. Installing pytest
      1. pip and virtualenv
    2. Writing and running tests
      1. Running tests
      2. Powerful asserts
        1. Text differences
        2. Lists
        3. Dictionaries and sets
        4. How does pytest do it?
      3. Checking exceptions: pytest.raises
        1. Checking exception messages
      4. Checking warnings: pytest.warns
      5. Comparing floating point numbers: pytest.approx
    3. Organizing files and packages
      1. Tests that accompany your code
      2. Tests separate from your code
    4. Useful command-line options
      1. Keyword expressions: -k
      2. Stop soon: -x, --maxfail
      3. Last failed, failed first: --lf, --ff
      4. Output capturing: -s and --capture
        1. Disabling capturing with -s
        2. Capture methods with --capture
      5. Traceback modes and locals: --tb, --showlocals
        1. --tb=long
        2. --tb=short
        3. --tb=native
        4. --tb=line
        5. --tb=no
        6. --showlocals (-l)
      6. Slow tests with --durations
      7. Extra test summary: -ra
    5. Configuration: pytest.ini
      1. Additional command-line: addopts
      2. Customizing a collection
      3. Cache directory: cache_dir
      4. Avoid recursing into directories: norecursedirs
      5. Pick the right place by default: testpaths
      6. Override options with -o/--override
    6. Summary
  9. Markers and Parametrization
    1. Mark basics
      1. Creating marks
      2. Running tests based on marks
      3. Applying marks to classes
      4. Applying marks to modules
      5. Custom marks and pytest.ini
    2. Built-in marks
      1. @pytest.mark.skipif
        1. pytest.skip
        2. pytest.importorskip
      2. @pytest.mark.xfail
        1. pytest.xfail
    3. Parametrization
      1. Enter @pytest.mark.parametrize
      2. Applying marks to value sets
      3. Customizing test IDs
      4. Testing multiple implementations
    4. Summary
  10. Fixtures
    1. Introducing fixtures
      1. Enter fixtures
      2. Setup/teardown
      3. Composability
    2. Sharing fixtures with files
    3. Scopes
      1. Scopes in action
    4. Autouse
      1. @pytest.mark.usefixtures
    5. Parametrizing fixtures
    6. Using marks from fixtures
    7. An overview of built-in fixtures
      1. tmpdir
      2. tmpdir_factory
      3. monkeypatch
        1. How and where to patch
      4. capsys/capfd
        1. Binary mode
      5. request
    8. Tips/discussion
      1. When to use fixtures, as opposed to simple functions
      2. Renaming fixtures
      3. Prefer local imports in conftest files
      4. Fixtures as test-supporting code
    9. Summary
  11. Plugins
    1. Finding and installing plugins
      1. Finding plugins
      2. Installing plugins
    2. An overview of assorted plugins
      1. pytest-xdist
      2. pytest-cov
      3. pytest-faulthandler
      4. pytest-mock
      5. pytest-django
      6. pytest-flakes
      7. pytest-asyncio
      8. pytest-trio
      9. pytest-tornado
      10. pytest-postgresql
      11. docker-services
      12. pytest-selenium
      13. pytest-html
      14. pytest-cpp
      15. pytest-timeout
      16. pytest-annotate
      17. pytest-qt
      18. pytest-randomly
      19. pytest-datadir
      20. pytest-regressions
    3. Honorable mentions
    4. Summary
  12. Converting unittest suites to pytest
    1. Using pytest as a test runner
      1. Pytest features in unittest subclasses
    2. Converting asserts with unitest2pytest
    3. Handling setup/teardown
    4. Managing test hierarchies
      1. Reusing test code with fixtures
    5. Refactoring test utilities
    6. Migration strategy
    7. Summary
  13. Wrapping Up
    1. Overview of what we have learned
      1. Introduction
      2. Writing and running tests
      3. Marks and parametrization
      4. Fixtures
      5. Plugins
      6. Converting unittest suites to pytest
    2. The pytest community
      1. Getting involved
      2. 2016 Sprint
    3. Next steps
    4. Final summary
  14. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: pytest Quick Start Guide
  • Author(s): Bruno Oliveira
  • Release date: August 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789347562