ebook img

Python Testing with pytest: Simple, Rapid, Effective, and Scalable PDF

264 Pages·2022·8.865 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Python Testing with pytest: Simple, Rapid, Effective, and Scalable

Early praise for Python Testing with pytest, Second Edition The knowledge and experience Brian brings to these pages has made this the definitive pytest resource for me. New or experienced, this book will be your one- stop shop for all of your real-world pytest needs. ➤ Julian Sequeira Cofounder of PyBites If you are into coding books and Python, this is a great and fun way to learn and acquire testing skills like a pro, much faster than figuring it out on your own. ➤ Sebastián Ramírez Creator of FastAPI and Typer This is my pytest go-to book—thorough coverage, great code examples, and acces- sible. If you want to write great test code and become proficient in what I think is the best Python testing framework out there, study this book. ➤ Bob Belderbos Python coach and Cofounder of PyBites This book truly is an excellent resource on pytest. I’ve been recommending the first edition when people ask me for a book during my pytest trainings. While reading through the second edition, I sometimes thought, “I wish the pytest doc- umentation would explain this topic just as well.” ➤ Florian Bruhin Founder, Bruhin Software We've left this page blank to make the page numbers the same in the electronic and paper books. We tried just leaving it out, but then people wrote us to ask about the missing pages. Anyway, Eddy the Gerbil wanted to say “hello.” Python Testing with pytest, Second Edition Simple, Rapid, Effective, and Scalable Brian Okken The Pragmatic Bookshelf Raleigh, North Carolina Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. For our complete catalog of hands-on, practical, and Pragmatic content for software devel- opers, please visit https://pragprog.com. The team that produced this book includes: CEO: Dave Rankin COO: Janet Furlow Managing Editor: Tammy Coron Development Editor: Katharine Dvorak Copy Editor: Karen Galle Indexing: Potomac Indexing, LLC Layout: Gilson Graphics Founders: Andy Hunt and Dave Thomas For sales, volume licensing, and support, please contact [email protected]. For international rights, please contact [email protected]. Copyright © 2022 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. ISBN-13: 978-1-68050-860-4 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—February 2022 Contents Acknowledgments . . . . . . . . . . . xi Preface . . . . . . . . . . . . . . xiii Part I — Primary Power 1. Getting Started with pytest . . . . . . . . . 3 Installing pytest 3 Running pytest 4 Review 8 Exercises 9 What’s Next 10 2. Writing Test Functions . . . . . . . . . . 11 Installing the Sample Application 11 Writing Knowledge-Building Tests 13 Using assert Statements 16 Failing with pytest.fail() and Exceptions 19 Writing Assertion Helper Functions 20 Testing for Expected Exceptions 21 Structuring Test Functions 23 Grouping Tests with Classes 24 Running a Subset of Tests 25 Review 28 Exercises 29 What’s Next 30 3. pytest Fixtures . . . . . . . . . . . . 31 Getting Started with Fixtures 31 Using Fixtures for Setup and Teardown 33 Tracing Fixture Execution with –setup-show 35 Contents • vi Specifying Fixture Scope 36 Sharing Fixtures through conftest.py 38 Finding Where Fixtures Are Defined 39 Using Multiple Fixture Levels 40 Using Multiple Fixtures per Test or Fixture 42 Deciding Fixture Scope Dynamically 43 Using autouse for Fixtures That Always Get Used 45 Renaming Fixtures 46 Review 47 Exercises 48 What’s Next 48 4. Builtin Fixtures . . . . . . . . . . . . 49 Using tmp_path and tmp_path_factory 49 Using capsys 51 Using monkeypatch 54 Remaining Builtin Fixtures 58 Review 59 Exercises 59 What’s Next 60 5. Parametrization . . . . . . . . . . . . 61 Testing Without Parametrize 62 Parametrizing Functions 64 Parametrizing Fixtures 66 Parametrizing with pytest_generate_tests 67 Using Keywords to Select Test Cases 69 Review 71 Exercises 71 What’s Next 72 6. Markers . . . . . . . . . . . . . . 73 Using Builtin Markers 73 Skipping Tests with pytest.mark.skip 74 Skipping Tests Conditionally with pytest.mark.skipif 76 Expecting Tests to Fail with pytest.mark.xfail 77 Selecting Tests with Custom Markers 79 Marking Files, Classes, and Parameters 82 Using “and,” “or,” “not,” and Parentheses with Markers 85 Being Strict with Markers 86 Combining Markers with Fixtures 88 Contents • vii Listing Markers 92 Review 92 Exercises 94 What’s Next 95 Part II — Working with Projects 7. Strategy . . . . . . . . . . . . . . 99 Determining Test Scope 99 Considering Software Architecture 101 Evaluating the Features to Test 103 Creating Test Cases 105 Writing a Test Strategy 108 Review 109 Exercises 110 What’s Next 111 8. Configuration Files . . . . . . . . . . . 113 Understanding pytest Configuration Files 113 Saving Settings and Flags in pytest.ini 114 Using tox.ini, pyproject.toml, or setup.cfg in place of pytest.ini 116 Determining a Root Directory and Config File 118 Sharing Local Fixtures and Hook Functions with conftest.py 119 Avoiding Test File Name Collision 119 Review 121 Exercises 121 What’s Next 122 9. Coverage . . . . . . . . . . . . . 123 Using coverage.py with pytest-cov 123 Generating HTML Reports 127 Excluding Code from Coverage 129 Running Coverage on Tests 130 Running Coverage on a Directory 131 Running Coverage on a Single File 132 Review 134 Exercises 134 What’s Next 135 10. Mocking . . . . . . . . . . . . . 137 Isolating the Command-Line Interface 137 Contents • viii Testing with Typer 139 Mocking an Attribute 140 Mocking a Class and Methods 141 Keeping Mock and Implementation in Sync with Autospec 143 Making Sure Functions Are Called Correctly 145 Creating Error Conditions 146 Testing at Multiple Layers to Avoid Mocking 147 Using Plugins to Assist Mocking 148 Review 149 Exercises 149 What’s Next 150 11. tox and Continuous Integration . . . . . . . . 151 What Is Continuous Integration? 151 Introducing tox 152 Setting Up tox 153 Running tox 154 Testing Multiple Python Versions 155 Running tox Environments in Parallel 156 Adding a Coverage Report to tox 156 Specifying a Minimum Coverage Level 157 Passing pytest Parameters Through tox 158 Running tox with GitHub Actions 159 Review 162 Exercises 162 What’s Next 163 12. Testing Scripts and Applications . . . . . . . 165 Testing a Simple Python Script 166 Testing an Importable Python Script 168 Separating Code into src and tests Directories 170 Defining the Python Search Path 171 Testing requirements.txt-Based Applications 172 Review 175 Exercises 176 What’s Next 177 13. Debugging Test Failures . . . . . . . . . 179 Adding a New Feature to the Cards Project 179 Installing Cards in Editable Mode 182 Debugging with pytest Flags 183

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.