pytest Quick Start Guide
eBook - ePub

pytest Quick Start Guide

Write better Python code with simple and maintainable tests

Bruno Oliveira

Compartir libro
  1. 160 páginas
  2. English
  3. ePUB (apto para móviles)
  4. Disponible en iOS y Android
eBook - ePub

pytest Quick Start Guide

Write better Python code with simple and maintainable tests

Bruno Oliveira

Detalles del libro
Vista previa del libro
Índice
Citas

Información del libro

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.

Preguntas frecuentes

¿Cómo cancelo mi suscripción?
Simplemente, dirígete a la sección ajustes de la cuenta y haz clic en «Cancelar suscripción». Así de sencillo. Después de cancelar tu suscripción, esta permanecerá activa el tiempo restante que hayas pagado. Obtén más información aquí.
¿Cómo descargo los libros?
Por el momento, todos nuestros libros ePub adaptables a dispositivos móviles se pueden descargar a través de la aplicación. La mayor parte de nuestros PDF también se puede descargar y ya estamos trabajando para que el resto también sea descargable. Obtén más información aquí.
¿En qué se diferencian los planes de precios?
Ambos planes te permiten acceder por completo a la biblioteca y a todas las funciones de Perlego. Las únicas diferencias son el precio y el período de suscripción: con el plan anual ahorrarás en torno a un 30 % en comparación con 12 meses de un plan mensual.
¿Qué es Perlego?
Somos un servicio de suscripción de libros de texto en línea que te permite acceder a toda una biblioteca en línea por menos de lo que cuesta un libro al mes. Con más de un millón de libros sobre más de 1000 categorías, ¡tenemos todo lo que necesitas! Obtén más información aquí.
¿Perlego ofrece la función de texto a voz?
Busca el símbolo de lectura en voz alta en tu próximo libro para ver si puedes escucharlo. La herramienta de lectura en voz alta lee el texto en voz alta por ti, resaltando el texto a medida que se lee. Puedes pausarla, acelerarla y ralentizarla. Obtén más información aquí.
¿Es pytest Quick Start Guide un PDF/ePUB en línea?
Sí, puedes acceder a pytest Quick Start Guide de Bruno Oliveira en formato PDF o ePUB, así como a otros libros populares de Computer Science y Quality Assurance & Testing. Tenemos más de un millón de libros disponibles en nuestro catálogo para que explores.

Información

Año
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 ...

Índice