| 1 |
.. highlight:: python |
| 2 |
.. _`goodpractices`: |
| 3 |
|
| 4 |
Good Integration Practices |
| 5 |
================================================= |
| 6 |
|
| 7 |
Install package with pip |
| 8 |
------------------------------------------------- |
| 9 |
|
| 10 |
For development, we recommend you use :mod:`venv` for virtual environments and |
| 11 |
:doc:`pip:index` for installing your application and any dependencies, |
| 12 |
as well as the ``pytest`` package itself. |
| 13 |
This ensures your code and dependencies are isolated from your system Python installation. |
| 14 |
|
| 15 |
Create a ``pyproject.toml`` file in the root of your repository as described in |
| 16 |
:doc:`packaging:tutorials/packaging-projects`. |
| 17 |
The first few lines should look like this: |
| 18 |
|
| 19 |
.. code-block:: toml |
| 20 |
|
| 21 |
[build-system] |
| 22 |
requires = ["hatchling"] |
| 23 |
build-backend = "hatchling.build" |
| 24 |
|
| 25 |
[project] |
| 26 |
name = "PACKAGENAME" |
| 27 |
version = "PACKAGEVERSION" |
| 28 |
|
| 29 |
where ``PACKAGENAME`` and ``PACKAGEVERSION`` are the name and version of your package respectively. |
| 30 |
|
| 31 |
You can then install your package in "editable" mode by running from the same directory: |
| 32 |
|
| 33 |
.. code-block:: bash |
| 34 |
|
| 35 |
pip install -e . |
| 36 |
|
| 37 |
which lets you change your source code (both tests and application) and rerun tests at will. |
| 38 |
|
| 39 |
.. _`test discovery`: |
| 40 |
.. _`Python test discovery`: |
| 41 |
|
| 42 |
Conventions for Python test discovery |
| 43 |
------------------------------------------------- |
| 44 |
|
| 45 |
``pytest`` implements the following standard test discovery: |
| 46 |
|
| 47 |
* If no arguments are specified then collection starts from :confval:`testpaths` |
| 48 |
(if configured) or the current directory. Alternatively, command line arguments |
| 49 |
can be used in any combination of directories, file names or node ids. |
| 50 |
* Recurse into directories, unless they match :confval:`norecursedirs`. |
| 51 |
* In those directories, search for ``test_*.py`` or ``*_test.py`` files, imported by their `test package name`_. |
| 52 |
* From those files, collect test items: |
| 53 |
|
| 54 |
* ``test`` prefixed test functions or methods outside of class. |
| 55 |
* ``test`` prefixed test functions or methods inside ``Test`` prefixed test classes (without an ``__init__`` method). Methods decorated with ``@staticmethod`` and ``@classmethods`` are also considered. |
| 56 |
|
| 57 |
For examples of how to customize your test discovery :doc:`/example/pythoncollection`. |
| 58 |
|
| 59 |
Within Python modules, ``pytest`` also discovers tests using the standard |
| 60 |
:ref:`unittest.TestCase <unittest.TestCase>` subclassing technique. |
| 61 |
|
| 62 |
|
| 63 |
.. _`test layout`: |
| 64 |
|
| 65 |
Choosing a test layout |
| 66 |
---------------------- |
| 67 |
|
| 68 |
``pytest`` supports two common test layouts: |
| 69 |
|
| 70 |
Tests outside application code |
| 71 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 72 |
|
| 73 |
Putting tests into an extra directory outside your actual application code |
| 74 |
might be useful if you have many functional tests or for other reasons want |
| 75 |
to keep tests separate from actual application code (often a good idea): |
| 76 |
|
| 77 |
.. code-block:: text |
| 78 |
|
| 79 |
pyproject.toml |
| 80 |
src/ |
| 81 |
mypkg/ |
| 82 |
__init__.py |
| 83 |
app.py |
| 84 |
view.py |
| 85 |
tests/ |
| 86 |
test_app.py |
| 87 |
test_view.py |
| 88 |
... |
| 89 |
|
| 90 |
This has the following benefits: |
| 91 |
|
| 92 |
* Your tests can run against an installed version after executing ``pip install .``. |
| 93 |
* Your tests can run against the local copy with an editable install after executing ``pip install --editable .``. |
| 94 |
|
| 95 |
For new projects, we recommend to use ``importlib`` :ref:`import mode <import-modes>` |
| 96 |
(see which-import-mode_ for a detailed explanation). |
| 97 |
To this end, add the following to your configuration file: |
| 98 |
|
| 99 |
.. code-block:: toml |
| 100 |
|
| 101 |
# content of pytest.toml |
| 102 |
[pytest] |
| 103 |
addopts = ["--import-mode=importlib"] |
| 104 |
|
| 105 |
.. _src-layout: |
| 106 |
|
| 107 |
Generally, but especially if you use the default import mode ``prepend``, |
| 108 |
it is **strongly** suggested to use a ``src`` layout. |
| 109 |
Here, your application root package resides in a sub-directory of your root, |
| 110 |
i.e. ``src/mypkg/`` instead of ``mypkg``. |
| 111 |
|
| 112 |
This layout prevents a lot of common pitfalls and has many benefits, |
| 113 |
which are better explained in this excellent `blog post`_ by Ionel Cristian Mărieș. |
| 114 |
|
| 115 |
.. _blog post: https://blog.ionelmc.ro/2014/05/25/python-packaging/#the-structure> |
| 116 |
|
| 117 |
.. note:: |
| 118 |
|
| 119 |
If you do not use an editable install and use the ``src`` layout as above you need to extend the Python's |
| 120 |
search path for module files to execute the tests against the local copy directly. You can do it in an |
| 121 |
ad-hoc manner by setting the ``PYTHONPATH`` environment variable: |
| 122 |
|
| 123 |
.. code-block:: bash |
| 124 |
|
| 125 |
PYTHONPATH=src pytest |
| 126 |
|
| 127 |
or in a permanent manner by using the :confval:`pythonpath` configuration variable and adding the |
| 128 |
following to your configuration file: |
| 129 |
|
| 130 |
.. tab:: toml |
| 131 |
|
| 132 |
.. code-block:: toml |
| 133 |
|
| 134 |
[pytest] |
| 135 |
pythonpath = ["src"] |
| 136 |
|
| 137 |
.. tab:: ini |
| 138 |
|
| 139 |
.. code-block:: ini |
| 140 |
|
| 141 |
[pytest] |
| 142 |
pythonpath = src |
| 143 |
|
| 144 |
.. note:: |
| 145 |
|
| 146 |
If you do not use an editable install and do not use the ``src`` layout (``mypkg`` directly in the root |
| 147 |
directory) you can rely on the fact that Python by default puts the current directory in ``sys.path`` to |
| 148 |
import your package and run ``python -m pytest`` to execute the tests against the local copy directly. |
| 149 |
|
| 150 |
See :ref:`pytest vs python -m pytest` for more information about the difference between calling ``pytest`` and |
| 151 |
``python -m pytest``. |
| 152 |
|
| 153 |
.. seealso:: |
| 154 |
|
| 155 |
:doc:`packaging:discussions/src-layout-vs-flat-layout` |
| 156 |
The Python Packaging User Guide discusses the trade-offs between the ``src`` layout and ``flat`` layout. |
| 157 |
|
| 158 |
Tests as part of application code |
| 159 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 160 |
|
| 161 |
Inlining test directories into your application package |
| 162 |
is useful if you have direct relation between tests and application modules and |
| 163 |
want to distribute them along with your application: |
| 164 |
|
| 165 |
.. code-block:: text |
| 166 |
|
| 167 |
pyproject.toml |
| 168 |
[src/]mypkg/ |
| 169 |
__init__.py |
| 170 |
app.py |
| 171 |
view.py |
| 172 |
tests/ |
| 173 |
__init__.py |
| 174 |
test_app.py |
| 175 |
test_view.py |
| 176 |
... |
| 177 |
|
| 178 |
In this scheme, it is easy to run your tests using the :option:`--pyargs` option: |
| 179 |
|
| 180 |
.. code-block:: bash |
| 181 |
|
| 182 |
pytest --pyargs mypkg |
| 183 |
|
| 184 |
``pytest`` will discover where ``mypkg`` is installed and collect tests from there. |
| 185 |
|
| 186 |
Note that this layout also works in conjunction with the ``src`` layout mentioned in the previous section. |
| 187 |
|
| 188 |
|
| 189 |
.. note:: |
| 190 |
|
| 191 |
You can use namespace packages (PEP420) for your application |
| 192 |
but pytest will still perform `test package name`_ discovery based on the |
| 193 |
presence of ``__init__.py`` files. If you use one of the |
| 194 |
two recommended file system layouts above but leave away the ``__init__.py`` |
| 195 |
files from your directories, it should just work. From |
| 196 |
"inlined tests", however, you will need to use absolute imports for |
| 197 |
getting at your application code. |
| 198 |
|
| 199 |
.. _`test package name`: |
| 200 |
|
| 201 |
.. note:: |
| 202 |
|
| 203 |
In ``prepend`` and ``append`` import-modes, if pytest finds a ``"a/b/test_module.py"`` |
| 204 |
test file while recursing into the filesystem it determines the import name |
| 205 |
as follows: |
| 206 |
|
| 207 |
* determine ``basedir``: this is the first "upward" (towards the root) |
| 208 |
directory not containing an ``__init__.py``. If e.g. both ``a`` |
| 209 |
and ``b`` contain an ``__init__.py`` file then the parent directory |
| 210 |
of ``a`` will become the ``basedir``. |
| 211 |
|
| 212 |
* perform ``sys.path.insert(0, basedir)`` to make the test module |
| 213 |
importable under the fully qualified import name. |
| 214 |
|
| 215 |
* ``import a.b.test_module`` where the path is determined |
| 216 |
by converting path separators ``/`` into "." characters. This means |
| 217 |
you must follow the convention of having directory and file |
| 218 |
names map directly to the import names. |
| 219 |
|
| 220 |
The reason for this somewhat evolved importing technique is |
| 221 |
that in larger projects multiple test modules might import |
| 222 |
from each other and thus deriving a canonical import name helps |
| 223 |
to avoid surprises such as a test module getting imported twice. |
| 224 |
|
| 225 |
With :option:`--import-mode=importlib` things are less convoluted because |
| 226 |
pytest doesn't need to change ``sys.path``, making things much less |
| 227 |
surprising. |
| 228 |
|
| 229 |
|
| 230 |
.. _which-import-mode: |
| 231 |
|
| 232 |
Choosing an import mode |
| 233 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 234 |
|
| 235 |
For historical reasons, pytest defaults to the ``prepend`` :ref:`import mode <import-modes>` |
| 236 |
instead of the ``importlib`` import mode we recommend for new projects. |
| 237 |
The reason lies in the way the ``prepend`` mode works: |
| 238 |
|
| 239 |
Since there are no packages to derive a full package name from, |
| 240 |
``pytest`` will import your test files as *top-level* modules. |
| 241 |
The test files in the first example (:ref:`src layout <src-layout>`) would be imported as |
| 242 |
``test_app`` and ``test_view`` top-level modules by adding ``tests/`` to ``sys.path``. |
| 243 |
|
| 244 |
This results in a drawback compared to the import mode ``importlib``: |
| 245 |
your test files must have **unique names**. |
| 246 |
|
| 247 |
If you need to have test modules with the same name, |
| 248 |
as a workaround you might add ``__init__.py`` files to your ``tests`` directory and subdirectories, |
| 249 |
changing them to packages: |
| 250 |
|
| 251 |
.. code-block:: text |
| 252 |
|
| 253 |
pyproject.toml |
| 254 |
mypkg/ |
| 255 |
... |
| 256 |
tests/ |
| 257 |
__init__.py |
| 258 |
foo/ |
| 259 |
__init__.py |
| 260 |
test_view.py |
| 261 |
bar/ |
| 262 |
__init__.py |
| 263 |
test_view.py |
| 264 |
|
| 265 |
Now pytest will load the modules as ``tests.foo.test_view`` and ``tests.bar.test_view``, |
| 266 |
allowing you to have modules with the same name. |
| 267 |
But now this introduces a subtle problem: |
| 268 |
in order to load the test modules from the ``tests`` directory, |
| 269 |
pytest prepends the root of the repository to ``sys.path``, |
| 270 |
which adds the side-effect that now ``mypkg`` is also importable. |
| 271 |
|
| 272 |
This is problematic if you are using a tool like tox_ to test your package in a virtual environment, |
| 273 |
because you want to test the *installed* version of your package, |
| 274 |
not the local code from the repository. |
| 275 |
|
| 276 |
The ``importlib`` import mode does not have any of the drawbacks above, |
| 277 |
because ``sys.path`` is not changed when importing test modules. |
| 278 |
|
| 279 |
|
| 280 |
.. _`buildout`: http://www.buildout.org/en/latest/ |
| 281 |
|
| 282 |
.. _`use tox`: |
| 283 |
|
| 284 |
tox |
| 285 |
--- |
| 286 |
|
| 287 |
Once you are done with your work and want to make sure that your actual |
| 288 |
package passes all tests you may want to look into :doc:`tox <tox:index>`, the |
| 289 |
virtualenv test automation tool. |
| 290 |
``tox`` helps you to setup virtualenv environments with pre-defined |
| 291 |
dependencies and then executing a pre-configured test command with |
| 292 |
options. It will run tests against the installed package and not |
| 293 |
against your source code checkout, helping to detect packaging |
| 294 |
glitches. |
| 295 |
|
| 296 |
Do not run via setuptools |
| 297 |
------------------------- |
| 298 |
|
| 299 |
Integration with setuptools is **not recommended**, |
| 300 |
i.e. you should not be using ``python setup.py test`` or ``pytest-runner``, |
| 301 |
and may stop working in the future. |
| 302 |
|
| 303 |
This is deprecated since it depends on deprecated features of setuptools |
| 304 |
and relies on features that break security mechanisms in pip. |
| 305 |
For example 'setup_requires' and 'tests_require' bypass ``pip --require-hashes``. |
| 306 |
For more information and migration instructions, |
| 307 |
see the `pytest-runner notice <https://github.com/pytest-dev/pytest-runner#deprecation-notice>`_. |
| 308 |
See also `pypa/setuptools#1684 <https://github.com/pypa/setuptools/issues/1684>`_. |
| 309 |
|
| 310 |
setuptools intends to |
| 311 |
`remove the test command <https://github.com/pypa/setuptools/issues/931>`_. |
| 312 |
|
| 313 |
Checking with flake8-pytest-style |
| 314 |
--------------------------------- |
| 315 |
|
| 316 |
In order to ensure that pytest is being used correctly in your project, |
| 317 |
it can be helpful to use the `flake8-pytest-style <https://github.com/m-burst/flake8-pytest-style>`_ flake8 plugin. |
| 318 |
|
| 319 |
flake8-pytest-style checks for common mistakes and coding style violations in pytest code, |
| 320 |
such as incorrect use of fixtures, test function names, and markers. |
| 321 |
By using this plugin, you can catch these errors early in the development process |
| 322 |
and ensure that your pytest code is consistent and easy to maintain. |
| 323 |
|
| 324 |
A list of the lints detected by flake8-pytest-style can be found on its `PyPI page <https://pypi.org/project/flake8-pytest-style/>`_. |
| 325 |
|
| 326 |
.. note:: |
| 327 |
|
| 328 |
flake8-pytest-style is not an official pytest project. Some of the rules enforce certain style choices, such as using `@pytest.fixture()` over `@pytest.fixture`, but you can configure the plugin to fit your preferred style. |
| 329 |
|
| 330 |
.. _`strict mode`: |
| 331 |
|
| 332 |
Using pytest's strict mode |
| 333 |
-------------------------- |
| 334 |
|
| 335 |
.. versionadded:: 9.0 |
| 336 |
|
| 337 |
Pytest contains a set of configuration options that make it more strict. |
| 338 |
The options are off by default for compatibility or other reasons, |
| 339 |
but you should enable them if you can. |
| 340 |
|
| 341 |
You can enable all of the strictness options at once by setting the :confval:`strict` configuration option: |
| 342 |
|
| 343 |
.. tab:: toml |
| 344 |
|
| 345 |
.. code-block:: toml |
| 346 |
|
| 347 |
[pytest] |
| 348 |
strict = true |
| 349 |
|
| 350 |
.. tab:: ini |
| 351 |
|
| 352 |
.. code-block:: ini |
| 353 |
|
| 354 |
[pytest] |
| 355 |
strict = true |
| 356 |
|
| 357 |
See the :confval:`strict` documentation for the options it enables and their effect. |
| 358 |
|
| 359 |
If pytest adds new strictness options in the future, they will also be enabled in strict mode. |
| 360 |
Therefore, you should only enable strict mode if you use a pinned/locked version of pytest, |
| 361 |
or if you want to proactively adopt new strictness options as they are added. |
| 362 |
If you don't want to automatically pick up new options, you can enable options individually: |
| 363 |
|
| 364 |
.. tab:: toml |
| 365 |
|
| 366 |
.. code-block:: toml |
| 367 |
|
| 368 |
[pytest] |
| 369 |
strict_config = true |
| 370 |
strict_markers = true |
| 371 |
strict_parametrization_ids = true |
| 372 |
strict_xfail = true |
| 373 |
|
| 374 |
.. tab:: ini |
| 375 |
|
| 376 |
.. code-block:: ini |
| 377 |
|
| 378 |
[pytest] |
| 379 |
strict_config = true |
| 380 |
strict_markers = true |
| 381 |
strict_parametrization_ids = true |
| 382 |
strict_xfail = true |
| 383 |
|
| 384 |
If you want to use strict mode but are having trouble with a specific option, you can turn it off individually: |
| 385 |
|
| 386 |
.. tab:: toml |
| 387 |
|
| 388 |
.. code-block:: toml |
| 389 |
|
| 390 |
[pytest] |
| 391 |
strict = true |
| 392 |
strict_parametrization_ids = false |
| 393 |
|
| 394 |
.. tab:: ini |
| 395 |
|
| 396 |
.. code-block:: ini |
| 397 |
|
| 398 |
[pytest] |
| 399 |
strict = true |
| 400 |
strict_parametrization_ids = false |