pytest Quick Start Guide
eBook - ePub

pytest Quick Start Guide

Write better Python code with simple and maintainable tests

Bruno Oliveira

Buch teilen
  1. 160 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfügbar
eBook - ePub

pytest Quick Start Guide

Write better Python code with simple and maintainable tests

Bruno Oliveira

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

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.

Häufig gestellte Fragen

Wie kann ich mein Abo kündigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kündigen“ – ganz einfach. Nachdem du gekündigt hast, bleibt deine Mitgliedschaft für den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich Bücher herunterladen?
Derzeit stehen all unsere auf Mobilgeräte reagierenden ePub-Bücher zum Download über die App zur Verfügung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die übrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den Aboplänen?
Mit beiden Aboplänen erhältst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst für Lehrbücher, bei dem du für weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhältst. Mit über 1 Million Büchern zu über 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
Unterstützt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nächsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist pytest Quick Start Guide als Online-PDF/ePub verfügbar?
Ja, du hast Zugang zu pytest Quick Start Guide von Bruno Oliveira im PDF- und/oder ePub-Format sowie zu anderen beliebten Büchern aus Computer Science & Quality Assurance & Testing. Aus unserem Katalog stehen dir über 1 Million Bücher zur Verfügung.

Information

Jahr
2018
ISBN
9781789343823

Writing and Running Tests

In the previous chapter, we discussed why testing is so important and looked at a brief overview of the unittest module. We also took a cursory look at pytest's features, but barely got a taste of them.
In this chapter, we will start our journey with pytest. We will be pragmatic, so this means that we will not take an exhaustive look at all of the things it's possible to do with pytest, but instead provide you with a quick overview of the basics to make you productive quickly. We will take a look at how to write tests, how to organize them into files and directories, and how to use pytest's command line effectively.
Here's what is covered in this chapter:
  • Installing pytest
  • Writing and running tests
  • Organizing files and packages
  • Useful command-line options
  • Configuration: pytest.ini file
In the chapter, there are a lot of examples typed into the command line. They are marked by the λ character. To avoid clutter and to focus on the important parts, the pytest header (which normally displays the pytest version, the Python version, installed plugins, and so on) will be suppressed.
Let's jump right into how to install pytest.

Installing pytest

Installing pytest is really simple, but first, let's take a moment to review good practices for Python development.
All of the examples are for Python 3. They should be easy to adapt to Python 2 if necessary.

pip and virtualenv

The recommended practice for installing dependencies is to create a virtualenv. A virtualenv (https://packaging.python.org/guides/installing-using-pip-and-virtualenv/) acts like a complete separate Python installation from the one that comes with your operating system, making it safe to install the packages required by your application without risk of breaking your system Python or tools.
Now we will learn how to create a virtual environment and install pytest using pip. If you are already familiar with virtualenv and pip, you can skip this section:
  1. Type this in your Command Prompt to create a virtualenv:
λ python -m venv .env
  1. 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
Or on Windows:
λ .env\Scripts\activate
This will put the virtualenv Python in front of the $PATH environment variable, so Python, pip, and other tools will be executed from the virtualenv, not from your system.
  1. Finally, to install pytest, type:
λ pip install pytest
You can verify that everything went well by typing:
λ pytest --version
This is pytest version 3.5.1, imported from x:\fibo\.env36\lib\site-packages\pytest.py
Now, we are all set up and can begin!

Writing and running tests

Using pytest, all you need to do to start writing tests is to create a new file named test_*.py and write test functions that start with test:
 # 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
To execute this test, simply execute pytest, passing the name of the file:
λ pytest test_player_mechanics.py
If you don't pass anything, pytest will look for all of the test files from the current directory recursively and execute them automatically.
You might encounter examples on the internet that use py.test in the command line instead of pytest. The reason for that is historical: pytest used to be part of the py package, which provided several general purpose utilities, including tools that followed the convention of starting with py.<TAB> for tab completion, but since then, it has been moved into its own project. The old py.test command is still available and is an alias to pytest, but the latter is the recommended modern usage.
Note that there's no need to create classes; just simple functions and plain assert statements are enough, but if you want to use classes to group tests you can do so:
 class TestMechanics:

def test_player_hit(self):
...

def test_player_health_flask(self):
...
Grouping tests can be useful when you want to put a number of tests under the same scope: you can execute tests based on the class they are in, apply markers to all of the tests in a class (Chapter 3, Markers and Parametrization), and create fixtures bound to a class (Chapter 4, Fixtures).

Running tests

Pytest can run your tests in a number of ways. Let's quickly get into the basics now and, later on in the chapter, we will move on to more advanced options.
You can start by just simply executing the pytest command:
λ pytest
This will find all of the test_*.py and *_test.py modules in the current directory and below recursively, and will run all of the tests found in those files:
  • 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
The syntax used above is created internally by pytest, is unique to each test collected, and is called a node id or item id. It basically consists of the filename of the testing module, class, and functions joined together by the :: characters.
Pytest will show a more verbose output, which includes node IDs, with the -v flag:
 λ 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 ...

Inhaltsverzeichnis