更新时间:2021-07-16 17:51:50
封面
Title Page
Copyright and Credits
pytest Quick Start Guide
Packt Upsell
Why subscribe?
PacktPub.com
Foreword
Contributors
About the author
About the reviewers
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Conventions used
Get in touch
Reviews
Introducing pytest
Why spend time writing tests?
A quick look at the unittest module
Why pytest?
Summary
Writing and Running Tests
Installing pytest
pip and virtualenv
Writing and running tests
Running tests
Powerful asserts
Text differences
Lists
Dictionaries and sets
How does pytest do it?
Checking exceptions: pytest.raises
Checking exception messages
Checking warnings: pytest.warns
Comparing floating point numbers: pytest.approx
Organizing files and packages
Tests that accompany your code
Tests separate from your code
Useful command-line options
Keyword expressions: -k
Stop soon: -x --maxfail
Last failed failed first: --lf --ff
Output capturing: -s and --capture
Disabling capturing with -s
Capture methods with --capture
Traceback modes and locals: --tb --showlocals
--tb=long
--tb=short
--tb=native
--tb=line
--tb=no
--showlocals (-l)
Slow tests with --durations
Extra test summary: -ra
Configuration: pytest.ini
Additional command-line: addopts
Customizing a collection
Cache directory: cache_dir
Avoid recursing into directories: norecursedirs
Pick the right place by default: testpaths
Override options with -o/--override
Markers and Parametrization
Mark basics
Creating marks
Running tests based on marks
Applying marks to classes
Applying marks to modules
Custom marks and pytest.ini
Built-in marks
@pytest.mark.skipif
pytest.skip
pytest.importorskip
@pytest.mark.xfail
pytest.xfail
Parametrization
Enter @pytest.mark.parametrize
Applying marks to value sets
Customizing test IDs
Testing multiple implementations
Fixtures
Introducing fixtures
Enter fixtures
Setup/teardown
Composability
Sharing fixtures with conftest.py files
Scopes
Scopes in action
Autouse
@pytest.mark.usefixtures
Parametrizing fixtures
Using marks from fixtures
An overview of built-in fixtures
tmpdir
tmpdir_factory