Python Testing with pytest, Second Edition (Pragmatic Bookshelf)

PragmaticBookshelf
Create efficient, elegant software tests in pytest, Python's most powerful testing framework.

Brian Okken @brianokken

Edited by Katharine Dvorak @katied

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.


Brian Okken is the host of the Test & Code podcast, and co-host of the Python Bytes podcast. He’s also a lead software engineer.


Don’t forget you can get 35% off with your Devtalk discount! Just use the coupon code “devtalk.com" at checkout :+1:

16 1449 5

Latest Threads About This Book Top

samuelwhiskeyjohnson
@brianokken In Chapter 6 Markers pg 78, it says Should your passing xfail tests result in XFAIL? If yes, leave strict alone. If you wan...
0 155 0
New
WmoT
@brianokken “If we want to eliminate the test cases with ‘play’ or ‘create,’ we can further zoom in:” pytest -v -k “todo and not (play ...
0 299 0
New
jygh
@brianokken The exercises in Chapter 15 refer to the following two directories: pytest-skip-slow slow-full In the downloaded source th...
0 339 0
New
jygh
@brianokken In Chapter 14, exercise 3, there’s the following instruction: Re-run the tests with --n=auto. Using my installed version o...
0 363 0
New
jygh
@brianokken Exercise 1 in Chapter 14 contains the instructions: Head over to pypy.python.org with your favorite browser. Unfortunately...
0 336 0
New
jygh
@brianokken Python Testing with pytest, Second Edition: typo “one tox environment” (179) The following sentence appears to have a typo (...
0 375 0
New
pingwax
@brianokken Then we can use both empty_db and some_cards in a test: should be: Then we can use both non_empty_db and some_cards in a...
0 450 1
New
Timtonix
Hello @brianokken, I’m a french student interested in Pytest and I recently bought Python Testing With Pytest. I’m at Mock Chapter’s ex...
0 470 2
New
jeremytsang
@brianokken If I’m not mistaken, the sentence on page 68 “We’ve just changed the way pytest fills in the value for initial_state every t...
0 442 1
New
PocsGeza
@brianokken After installing the cards project card creation appears to be working but when I try to run the “card” command I get the f...
0 595 1
New

Most Active This Week Top

Most Active This Month Top

Most Active This Year Top

samuelwhiskeyjohnson
@brianokken In Chapter 6 Markers pg 78, it says Should your passing xfail tests result in XFAIL? If yes, leave strict alone. If you wan...
0 155 0
New

Most Active Last Three Years Top

Meliowant
Brian Okken @brianokken, The following flags require an extra dash as for pytest 7.1.2: --lf - for last failed; --ff - for first fai...
0 787 1
New
sentient6
p. 40, 2nd paragraph, final sentence: “–fixturues” will look nicer without the second ‘u’. Great book, by the way.
0 683 1
New
PocsGeza
@brianokken After installing the cards project card creation appears to be working but when I try to run the “card” command I get the f...
0 595 1
New
Meliowant
There is the following example, on the page 28: $ pytest -v --tb=no -k “(dict or ids) and not TestEquality” ==========================...
0 622 1
New
GreatBahram
@brianokken Please use the following format for the title of this thread (then simply delete/replace this text with the content for the ...
0 599 1
New
Meliowant
@brianokken pytest’s output on page 231 contains 4 items for test_everyone case (admin, author, editor, visitor), while fixture’s params...
0 630 1
New
Timtonix
Hello @brianokken, I’m a french student interested in Pytest and I recently bought Python Testing With Pytest. I’m at Mock Chapter’s ex...
0 470 2
New
jeremytsang
@brianokken If I’m not mistaken, the sentence on page 68 “We’ve just changed the way pytest fills in the value for initial_state every t...
0 442 1
New
pingwax
@brianokken Then we can use both empty_db and some_cards in a test: should be: Then we can use both non_empty_db and some_cards in a...
0 450 1
New
jygh
@brianokken Python Testing with pytest, Second Edition: typo “one tox environment” (179) The following sentence appears to have a typo (...
0 375 0
New
jygh
@brianokken The exercises in Chapter 15 refer to the following two directories: pytest-skip-slow slow-full In the downloaded source th...
0 339 0
New
jygh
@brianokken In Chapter 14, exercise 3, there’s the following instruction: Re-run the tests with --n=auto. Using my installed version o...
0 363 0
New
jygh
@brianokken Exercise 1 in Chapter 14 contains the instructions: Head over to pypy.python.org with your favorite browser. Unfortunately...
0 336 0
New
WmoT
@brianokken “If we want to eliminate the test cases with ‘play’ or ‘create,’ we can further zoom in:” pytest -v -k “todo and not (play ...
0 299 0
New

Most Active Over Three Years Top

leonW
I ran this command after installing the sample application: $ cards add do something --owner Brian And got a file not found error: Fil...
5 4323 7
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
0 3880 1
New
PragmaticBookshelf
Create efficient, elegant software tests in pytest, Python's most powerful testing framework. Brian Okken @brianokken Edited by Kat...
16 1449 5
New
brunogirin
When installing Cards as an editable package, I get the following error: ERROR: File “setup.py” not found. Directory cannot be installe...
0 1714 1
New
brunogirin
When running tox for the first time, I got the following error: ERROR: InterpreterNotFound: python3.10 I realised that I was running ...
0 1295 1
New
brian-m-ops
#book-python-testing-with-pytest-second-edition Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
4 1174 3
New
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
0 1101 1
New
leonW
Brian, I don’t think this file got updated properly when you uploaded the new version. The README.md file is still missing, and the fix ...
1 910 3
New
foxhunt72
Title: Python Testing with pytest, Second Edition page 120 $ cd /path/to/code $ pytest --cov=cards ch7 Doesn’t seem to work, ch7 folde...
0 856 2
New
brunogirin
What’s next paragraph: We’ve used them parametrize tests with a single parameter and simple values, like strings. Should the word the...
0 822 1
New
navneethc
This is from the PDF of the latest beta release as on 10th Feb, 2022. Page 13, section titled Writing Knowledge-Building Tests, second p...
0 764 1
New
evanhorn
@brianokken Brian, the top level README.md file refers to “tasks_proj” instead of “cards_proj”. Based on this I grepped for “tasks” and...
0 780 1
New
leonW
The way I have this formatted it is page 80. But when discussing the content of ch3/b/conftest.py you say: “the cards_db fixture has ca...
1 789 1
New
rhasti
Testing with Typer A great feature of Typer is that it proves a testing interface I am not a native english speaker but assume that pr...
0 736 1
New

Book Info

Publisher: Pragmatic Bookshelf

Are you this book's author? To be notified of all posts made about it, sign up an account then click here, then select 'watching' from the bell icon in the top right corner.

Get money off!

The Pragmatic Bookshelf

35% off any eBook

Simply use coupon code "devtalk.com" at checkout. See full details here.