pytest Quick Start Guide
Write better Python code with simple and maintainable tests
Bruno Oliveira
- 160 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
pytest Quick Start Guide
Write better Python code with simple and maintainable tests
Bruno Oliveira
About This Book
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 workflow, 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.
Frequently asked questions
Information
Writing and Running Tests
- Installing pytest
- Writing and running tests
- Organizing files and packages
- Useful command-line options
- Configuration: pytest.ini file
Installing pytest
pip and virtualenv
- Type this in your Command Prompt to create a virtualenv:
λ python -m venv .env
- This command will create a .env folder in the current directory, containing a full-blown Python installation. Before proceeding, you should activate the virtualenv:
λ source .env/bin/activate
λ .env\Scripts\activate
- Finally, to install pytest, type:
λ pip install pytest
λ pytest --version
This is pytest version 3.5.1, imported from x:\fibo\.env36\lib\site-packages\pytest.py
Writing and running tests
# contents of test_player_mechanics.py
def test_player_hit():
player = create_player()
assert player.health == 100
undead = create_undead()
undead.hit(player)
assert player.health == 80
λ pytest test_player_mechanics.py
class TestMechanics:
def test_player_hit(self):
...
def test_player_health_flask(self):
...
Running tests
λ pytest
- You can reduce the search to specific directories:
λ pytest tests/core tests/contrib
- You can also mix any number of files and directories:
λ pytest tests/core tests/contrib/test_text_plugin.py
- You can execute specific tests by using the syntax <test-file>::<test-function-name>:
λ pytest tests/core/test_core.py::test_regex_matching
- You can execute all of the test methods of a test class:
λ pytest tests/contrib/test_text_plugin.py::TestPluginHooks
- You can execute a specific test method of a test class using the syntax <test-file>::<test-class>::<test-method-name>:
λ pytest tests/contrib/
test_text_plugin.py::TestPluginHooks::test_registration
λ pytest tests/core -v
======================== test session starts ========================
...
collected 6 items
tests\core\test_core.py::test_regex_matching PASSED [ 16%]
tests\core\test_core.py::test_check_options FAILED [ 33%]
tests\core\test_core.py::test_type_checking FAILED [ 50%]
tests\core\test_parser.py::test_parse_expr PASSED [ 66%]
tests\core\test_parser.py::test_parse_num PASSED ...