Test applications, packages, and libraries large and small with pytest, Python's most powerful testing framework. pytest helps you write tests quickly and keep them readable and maintainable. In this fully revised edition, explore pytest's superpowers - simple asserts, fixtures, parametrization, markers, and plugins - while creating simple tests and test suites against a small database application. Using a robust yet simple fixture model, it's just as easy to write small tests with pytest as it is to scale up to complex functional testing. This book shows you how. pytest is undeniably the best choice for testing Python projects. It's a full-featured, flexible, and extensible testing framework. pytest's fixture model allows you to share test data and setup procedures across multiple layers of tests. The pytest framework gives you powerful features such as assert rewriting, parametrization, markers, plugins, parallel test execution, and clear test failure reporting - with no boilerplate code. With simple step-by-step instructions and sample code, this book gets you up to speed quickly on this easy-to-learn yet powerful tool. Write short, maintainable tests that elegantly express what you're testing. Speed up test times by distributing tests across multiple processors and running tests in parallel. Use Python's builtin assert statements instead of awkward assert helper functions to make your tests more readable. Move setup code out of tests and into fixtures to separate setup failures from test failures. Test error conditions and corner cases with expected exception testing, and use one test to run many test cases with parameterized testing. Extend pytest with plugins, connect it to continuous integration systems, and use it in tandem with tox, mock, coverage, and even existing unittest tests. Write simple, maintainable tests quickly with pytest. What You Need: The examples in this book were written using Python 3.9 and pytest 6. pytest 6 supports Python 3.5 and above. Cover 1 Table of Contents 7 Acknowledgments 13 Preface 15 Why pytest? 15 Learn pytest While Testing a Sample Application 16 How This Book Is Organized 16 What You Need to Know 17 Why a Second Edition? 18 Example Code and Online Resources 19 Part I—Primary Power 21 1. Getting Started with pytest 22 Installing pytest 22 Running pytest 23 Review 27 Exercises 28 What’s Next 29 2. Writing Test Functions 30 Installing the Sample Application 30 Writing Knowledge-Building Tests 32 Using assert Statements 35 Failing with pytest.fail() and Exceptions 38 Writing Assertion Helper Functions 39 Testing for Expected Exceptions 40 Structuring Test Functions 42 Grouping Tests with Classes 43 Running a Subset of Tests 44 Review 47 Exercises 48 What’s Next 49 3. pytest Fixtures 50 Getting Started with Fixtures 50 Using Fixtures for Setup and Teardown 52 Tracing Fixture Execution with –setup-show 54 Specifying Fixture Scope 55 Sharing Fixtures through conftest.py 57 Finding Where Fixtures Are Defined 58 Using Multiple Fixture Levels 59 Using Multiple Fixtures per Test or Fixture 61 Deciding Fixture Scope Dynamically 62 Using autouse for Fixtures That Always Get Used 64 Renaming Fixtures 65 Review 66 Exercises 67 What’s Next 67 4. Builtin Fixtures 68 Using tmp_path and tmp_path_factory 68 Using capsys 70 Using monkeypatch 73 Remaining Builtin Fixtures 77 Review 78 Exercises 78 What’s Next 79 5. Parametrization 80 Testing Without Parametrize 81 Parametrizing Functions 83 Parametrizing Fixtures 85 Parametrizing with pytest_generate_tests 86 Using Keywords to Select Test Cases 88 Review 90 Exercises 90 What’s Next 91 6. Markers 92 Using Builtin Markers 92 Skipping Tests with pytest.mark.skip 93 Skipping Tests Conditionally with pytest.mark.skipif 95 Expecting Tests to Fail with pytest.mark.xfail 96 Selecting Tests with Custom Markers 98 Marking Files, Classes, and Parameters 101 Using “and,” “or,” “not,” and Parentheses with Markers 104 Being Strict with Markers 105 Combining Markers with Fixtures 107 Listing Markers 111 Review 111 Exercises 113 What’s Next 114 Part II—Working with Projects 115 7. Strategy 116 Determining Test Scope 116 Considering Software Architecture 118 Evaluating the Features to Test 120 Creating Test Cases 122 Writing a Test Strategy 125 Review 126 Exercises 127 What’s Next 128 8. Configuration Files 129 Understanding pytest Configuration Files 129 Saving Settings and Flags in pytest.ini 130 Using tox.ini, pyproject.toml, or setup.cfg in place of pytest.ini 132 Determining a Root Directory and Config File 134 Sharing Local Fixtures and Hook Functions with conftest.py 135 Avoiding Test File Name Collision 135 Review 137 Exercises 137 What’s Next 138 9. Coverage 139 Using coverage.py with pytest-cov 139 Generating HTML Reports 143 Excluding Code from Coverage 145 Running Coverage on Tests 146 Running Coverage on a Directory 147 Running Coverage on a Single File 148 Review 150 Exercises 150 What’s Next 151 10. Mocking 152 Isolating the Command-Line Interface 152 Testing with Typer 154 Mocking an Attribute 155 Mocking a Class and Methods 156 Keeping Mock and Implementation in Sync with Autospec 158 Making Sure Functions Are Called Correctly 160 Creating Error Conditions 161 Testing at Multiple Layers to Avoid Mocking 162 Using Plugins to Assist Mocking 163 Review 164 Exercises 164 What’s Next 165 11. tox and Continuous Integration 166 What Is Continuous Integration? 166 Introducing tox 167 Setting Up tox 168 Running tox 169 Testing Multiple Python Versions 170 Running tox Environments in Parallel 171 Adding a Coverage Report to tox 171 Specifying a Minimum Coverage Level 172 Passing pytest Parameters Through tox 173 Running tox with GitHub Actions 174 Review 177 Exercises 177 What’s Next 178 12. Testing Scripts and Applications 179 Testing a Simple Python Script 180 Testing an Importable Python Script 182 Separating Code into src and tests Directories 184 Defining the Python Search Path 185 Testing requirements.txt-Based Applications 186 Review 189 Exercises 190 What’s Next 191 13. Debugging Test Failures 192 Adding a New Feature to the Cards Project 192 Installing Cards in Editable Mode 195 Debugging with pytest Flags 196 Re-Running Failed Tests 197 Debugging with pdb 199 Combining pdb and tox 202 Review 204 Exercises 205 What’s Next 206 Part III—Booster Rockets 207 14. Third-Party Plugins 208 Finding Plugins 208 Installing Plugins 209 Exploring the Diversity of pytest Plugins 209 Running Tests in Parallel 212 Randomizing Test Order 214 Review 215 Exercises 215 What’s Next 215 15. Building Plugins 216 Starting with a Cool Idea 216 Building a Local conftest Plugin 218 Creating an Installable Plugin 220 Testing Plugins with pytester 225 Testing Multiple Python and pytest Versions with tox 228 Publishing Plugins 229 Review 229 Exercises 230 What’s Next 231 16. Advanced Parametrization 232 Using Complex Values 232 Creating Custom Identifiers 234 Parametrizing with Dynamic Values 238 Using Multiple Parameters 238 Using Indirect Parametrization 240 Review 243 Exercises 244 What’s Next 244 A1. Virtual Environments 245 A2. pip 247 Index 251 – SYMBOLS – 251 – A – 251 – B – 251 – C – 251 – D – 252 – E – 253 – F – 253 – G – 253 – H – 253 – I – 253 – L – 253 – M – 253 – N – 254 – O – 254 – P – 254 – Q – 256 – R – 256 – S – 256 – T – 257 – U – 258 – V – 258 – W – 258 – X – 258 – Y – 258 "Test applications, packages, and libraries large and small with pytest, Python's most powerful testing framework. pytest helps you write tests quickly and keep them readable and maintainable. In this fully revised edition, explore pytest's superpowers - simple asserts, fixtures, parametrization, markers, and plugins - while creating simple tests and test suites against a small database application. Using a robust yet simple fixture model, it's just as easy to write small tests with pytest as it is to scale up to complex functional testing. This book shows you how. Pytest is undeniably the best choice for testing Python projects. It's a full-featured, flexible, and extensible testing framework. pytest's fixture model allows you to share test data and setup procedures across multiple layers of tests. The pytest framework gives you powerful features such as assert rewriting, parametrization, markers, plugins, parallel test execution, and clear test failure reporting - with no boilerplate code. With simple step-by-step instructions and sample code, this book gets you up to speed quickly on this easy-to-learn yet powerful tool. Write short, maintainable tests that elegantly express what you're testing. Speed up test times by distributing tests across multiple processors and running tests in parallel. Use Python's builtin assert statements instead of awkward assert helper functions to make your tests more readable. Move setup code out of tests and into fixtures to separate setup failures from test failures. Test error conditions and corner cases with expected exception testing, and use one test to run many test cases with parameterized testing. Extend pytest with plugins, connect it to continuous integration systems, and use it in tandem with tox, mock, coverage, and even existing unittest tests. Write simple, maintainable tests quickly with pytest. What you eed: the examples in this book were written using Python 3.10 and pytest 7. pytest 7 supports Python 3.5 and above"-- Amazon Test applications, packages, and libraries large and small with pytest, Python's most powerful testing framework. pytest helps you write tests quickly and keep them readable and maintainable. In this fully revised edition, explore pytest's superpowers - simple asserts, fixtures, parametrization, markers, and plugins - while creating simple tests and test suites against a small database application. Using a robust yet simple fixture model, it's just as easy to write small tests with pytest as it is to scale up to complex functional testing. This book shows you how. pytest is undeniably the best choice for testing Python projects. It's a full-featured, flexible, and extensible testing framework. pytest's fixture model allows you to share test data and setup procedures across multiple layers of tests. The pytest framework gives you powerful features such as assert rewriting, parametrization, markers, plugins, parallel test execution, and clear test failure reporting - with no boilerplate code. With simple step-by-step instructions and sample code, this book gets you up to speed quickly on this easy-to-learn yet powerful tool. Write short, maintainable tests that elegantly express what you're testing. Speed up test times by distributing tests across multiple processors and running tests in parallel. Use Python's builtin assert statements instead of awkward assert helper functions to make your tests more readable. Move setup code out of tests and into fixtures to separate setup failures from test failures. Test error conditions and corner cases with expected exception testing, and use one test to run many test cases with parameterized testing. Extend pytest with plugins, connect it to continuous integration systems, and use it in tandem with tox, mock, coverage, and even existing unittest tests. Write simple, maintainable tests quickly with pytest. What You Need: The examples in this book were written using Python 3.10 and pytest 7. pytest 7 supports Python 3.5 and above.