2020-07-17 20:59:35 +08:00
.. highlight :: python
2016-01-20 23:35:27 +08:00
.. _`goodpractices`:
2010-10-14 07:25:09 +08:00
2015-06-22 17:34:00 +08:00
Good Integration Practices
2010-10-14 01:30:00 +08:00
=================================================
2018-08-24 00:07:28 +08:00
Install package with pip
-------------------------------------------------
2021-08-26 22:05:03 +08:00
For development, we recommend you use :mod: `venv` for virtual environments and
2021-10-22 20:47:57 +08:00
:doc: `pip:index` for installing your application and any dependencies,
2019-02-22 00:27:12 +08:00
as well as the `` pytest `` package itself.
2019-02-21 23:50:49 +08:00
This ensures your code and dependencies are isolated from your system Python installation.
2018-08-24 00:07:28 +08:00
2022-09-01 18:55:41 +08:00
Create a `` pyproject.toml `` file in the root of your repository as described in
:doc: `packaging:tutorials/packaging-projects` .
The first few lines should look like this:
2018-08-24 00:07:28 +08:00
2021-10-22 05:01:58 +08:00
.. code-block :: toml
2019-08-07 04:25:54 +08:00
2021-10-22 05:01:58 +08:00
[build-system]
2022-09-01 18:55:41 +08:00
requires = ["hatchling"]
build-backend = "hatchling.build"
2021-10-22 05:01:58 +08:00
2022-10-12 23:51:26 +08:00
[project]
2022-09-01 18:55:41 +08:00
name = "PACKAGENAME"
2021-10-22 05:01:58 +08:00
where `` PACKAGENAME `` is the name of your package.
You can then install your package in "editable" mode by running from the same directory:
2018-08-24 00:07:28 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: bash
2022-09-01 18:55:41 +08:00
pip install -e .
2018-08-24 00:07:28 +08:00
which lets you change your source code (both tests and application) and rerun tests at will.
2015-12-02 08:27:27 +08:00
.. _`test discovery`:
.. _`Python test discovery`:
Conventions for Python test discovery
-------------------------------------------------
`` pytest `` implements the following standard test discovery:
2015-12-03 11:01:34 +08:00
* If no arguments are specified then collection starts from :confval: `testpaths`
(if configured) or the current directory. Alternatively, command line arguments
can be used in any combination of directories, file names or node ids.
2016-11-26 22:04:52 +08:00
* Recurse into directories, unless they match :confval: `norecursedirs` .
* In those directories, search for `` test_*.py `` or `` *_test.py `` files, imported by their `test package name`_ .
* From those files, collect test items:
2022-12-02 23:53:04 +08:00
* `` test `` prefixed test functions or methods outside of class.
* `` test `` prefixed test functions or methods inside `` Test `` prefixed test classes (without an `` __init__ `` method). Methods decorated with `` @staticmethod `` and `` @classmethods `` are also considered.
2015-12-02 08:27:27 +08:00
2021-03-13 06:57:53 +08:00
For examples of how to customize your test discovery :doc: `/example/pythoncollection` .
2015-12-02 08:27:27 +08:00
Within Python modules, `` pytest `` also discovers tests using the standard
:ref: `unittest.TestCase <unittest.TestCase>` subclassing technique.
2013-12-10 21:54:13 +08:00
Choosing a test layout / import rules
2017-03-10 07:41:33 +08:00
-------------------------------------
2013-12-10 21:54:13 +08:00
2014-01-18 19:31:33 +08:00
`` pytest `` supports two common test layouts:
2013-12-10 21:54:13 +08:00
2017-03-10 07:41:33 +08:00
Tests outside application code
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2013-12-10 21:54:13 +08:00
2017-03-10 07:41:33 +08:00
Putting tests into an extra directory outside your actual application code
might be useful if you have many functional tests or for other reasons want
2019-08-07 07:20:06 +08:00
to keep tests separate from actual application code (often a good idea):
2013-12-10 21:54:13 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: text
2021-10-22 05:01:58 +08:00
pyproject.toml
2022-09-01 18:55:41 +08:00
src/
mypkg/
__init__.py
app.py
view.py
2013-12-10 21:54:13 +08:00
tests/
test_app.py
2017-03-10 07:41:33 +08:00
test_view.py
2013-12-10 21:54:13 +08:00
...
2019-01-10 08:42:18 +08:00
This has the following benefits:
* Your tests can run against an installed version after executing `` pip install . `` .
* Your tests can run against the local copy with an editable install after executing `` pip install --editable . `` .
2013-12-10 21:54:13 +08:00
2022-09-01 18:55:41 +08:00
For new projects, we recommend to use `` importlib `` :ref: `import mode <import-modes>`
(see which-import-mode_ for a detailed explanation).
To this end, add the following to your `` pyproject.toml `` :
2013-12-10 21:54:13 +08:00
2022-09-01 18:55:41 +08:00
.. code-block :: toml
2019-08-07 04:25:54 +08:00
2022-09-01 18:55:41 +08:00
[tool.pytest.ini_options]
addopts = [
"--import-mode=importlib",
]
2017-03-14 18:59:01 +08:00
2022-09-01 18:55:41 +08:00
.. _src-layout:
2020-06-13 22:29:01 +08:00
2022-09-01 18:55:41 +08:00
Generally, but especially if you use the default import mode `` prepend `` ,
it is **strongly** suggested to use a `` src `` layout.
Here, your application root package resides in a sub-directory of your root,
i.e. `` src/mypkg/ `` instead of `` mypkg `` .
2017-03-14 18:59:01 +08:00
2022-09-01 18:55:41 +08:00
This layout prevents a lot of common pitfalls and has many benefits,
which are better explained in this excellent `blog post`_ by Ionel Cristian Mărieș.
2020-06-13 22:29:01 +08:00
2022-09-01 18:55:41 +08:00
.. _blog post: https://blog.ionelmc.ro/2014/05/25/python-packaging/#the-structure>
2020-06-13 22:29:01 +08:00
2022-10-08 00:03:13 +08:00
.. note ::
If you do not use an editable install and use the `` src `` layout as above you need to extend the Python's
search path for module files to execute the tests against the local copy directly. You can do it in an
ad-hoc manner by setting the `` PYTHONPATH `` environment variable:
.. code-block :: bash
PYTHONPATH=src pytest
or in a permanent manner by using the :confval: `pythonpath` configuration variable and adding the
following to your `` pyproject.toml `` :
.. code-block :: toml
[tool.pytest.ini_options]
pythonpath = "src"
.. note ::
If you do not use an editable install and not use the `` src `` layout (`` mypkg `` directly in the root
directory) you can rely on the fact that Python by default puts the current directory in `` sys.path `` to
import your package and run `` python -m pytest `` to execute the tests against the local copy directly.
See :ref: `pytest vs python -m pytest` for more information about the difference between calling `` pytest `` and
`` python -m pytest `` .
2020-06-13 22:29:01 +08:00
2017-03-10 07:41:33 +08:00
Tests as part of application code
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2013-12-10 21:54:13 +08:00
2017-03-10 07:41:33 +08:00
Inlining test directories into your application package
is useful if you have direct relation between tests and application modules and
2019-08-07 07:20:06 +08:00
want to distribute them along with your application:
2013-12-10 21:54:13 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: text
2021-10-22 05:01:58 +08:00
pyproject.toml
2022-09-01 18:55:41 +08:00
[src/]mypkg/
2013-12-10 21:54:13 +08:00
__init__.py
2017-03-10 07:41:33 +08:00
app.py
view.py
2022-04-16 21:51:21 +08:00
tests/
2017-03-10 07:41:33 +08:00
__init__.py
2013-12-10 21:54:13 +08:00
test_app.py
2017-03-10 07:41:33 +08:00
test_view.py
2013-12-10 21:54:13 +08:00
...
2019-08-07 07:20:06 +08:00
In this scheme, it is easy to run your tests using the `` --pyargs `` option:
2013-12-10 21:54:13 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: bash
2017-03-10 07:41:33 +08:00
pytest --pyargs mypkg
2013-12-10 21:54:13 +08:00
2017-03-10 07:41:33 +08:00
`` pytest `` will discover where `` mypkg `` is installed and collect tests from there.
2013-12-10 21:54:13 +08:00
2017-03-15 05:39:02 +08:00
Note that this layout also works in conjunction with the `` src `` layout mentioned in the previous section.
2013-12-10 21:54:13 +08:00
.. note ::
2021-10-22 05:01:58 +08:00
You can use namespace packages (PEP420) for your application
2013-12-17 14:58:49 +08:00
but pytest will still perform `test package name`_ discovery based on the
2014-01-18 19:31:33 +08:00
presence of `` __init__.py `` files. If you use one of the
2013-12-17 14:58:49 +08:00
two recommended file system layouts above but leave away the `` __init__.py ``
2021-10-22 05:01:58 +08:00
files from your directories, it should just work. From
2013-12-10 21:54:13 +08:00
"inlined tests", however, you will need to use absolute imports for
2013-12-17 14:58:49 +08:00
getting at your application code.
2013-12-10 21:54:13 +08:00
2013-12-17 14:58:49 +08:00
.. _`test package name`:
2013-12-10 21:54:13 +08:00
.. note ::
2020-06-13 22:29:01 +08:00
In `` prepend `` and `` append `` import-modes, if pytest finds a `` "a/b/test_module.py" ``
test file while recursing into the filesystem it determines the import name
2013-12-10 21:54:13 +08:00
as follows:
* determine `` basedir `` : this is the first "upward" (towards the root)
directory not containing an `` __init__.py `` . If e.g. both `` a ``
and `` b `` contain an `` __init__.py `` file then the parent directory
of `` a `` will become the `` basedir `` .
* perform `` sys.path.insert(0, basedir) `` to make the test module
importable under the fully qualified import name.
* `` import a.b.test_module `` where the path is determined
by converting path separators `` / `` into "." characters. This means
you must follow the convention of having directory and file
names map directly to the import names.
The reason for this somewhat evolved importing technique is
that in larger projects multiple test modules might import
from each other and thus deriving a canonical import name helps
2016-12-02 23:09:38 +08:00
to avoid surprises such as a test module getting imported twice.
2013-12-10 21:54:13 +08:00
2020-06-13 22:29:01 +08:00
With `` --import-mode=importlib `` things are less convoluted because
pytest doesn't need to change `` sys.path `` or `` sys.modules `` , making things
much less surprising.
2010-10-14 01:30:00 +08:00
2022-09-01 18:55:41 +08:00
.. _which-import-mode:
Choosing an import mode
^^^^^^^^^^^^^^^^^^^^^^^
For historical reasons, pytest defaults to the `` prepend `` :ref: `import mode <import-modes>`
instead of the `` importlib `` import mode we recommend for new projects.
The reason lies in the way the `` prepend `` mode works:
Since there are no packages to derive a full package name from,
`` pytest `` will import your test files as *top-level* modules.
The test files in the first example (:ref: `src layout <src-layout>` ) would be imported as
`` test_app `` and `` test_view `` top-level modules by adding `` tests/ `` to `` sys.path `` .
This results in a drawback compared to the import mode `` importlib `` :
your test files must have **unique names** .
If you need to have test modules with the same name,
as a workaround you might add `` __init__.py `` files to your `` tests `` folder and subfolders,
changing them to packages:
.. code-block :: text
pyproject.toml
mypkg/
...
tests/
__init__.py
foo/
__init__.py
test_view.py
bar/
__init__.py
test_view.py
Now pytest will load the modules as `` tests.foo.test_view `` and `` tests.bar.test_view `` ,
allowing you to have modules with the same name.
But now this introduces a subtle problem:
in order to load the test modules from the `` tests `` directory,
pytest prepends the root of the repository to `` sys.path `` ,
which adds the side-effect that now `` mypkg `` is also importable.
This is problematic if you are using a tool like tox_ to test your package in a virtual environment,
because you want to test the *installed* version of your package,
not the local code from the repository.
The `` importlib `` import mode does not have any of the drawbacks above,
because `` sys.path `` is not changed when importing test modules.
2021-10-22 20:47:57 +08:00
.. _`buildout`: http://www.buildout.org/en/latest/
2010-10-14 01:30:00 +08:00
2013-12-10 21:54:13 +08:00
.. _`use tox`:
2018-05-25 15:54:03 +08:00
tox
2021-10-22 20:47:57 +08:00
---
2015-12-02 08:47:36 +08:00
2017-03-10 07:41:33 +08:00
Once you are done with your work and want to make sure that your actual
2021-10-22 20:47:57 +08:00
package passes all tests you may want to look into :doc: `tox <tox:index>` , the
virtualenv test automation tool and its :doc: `pytest support <tox:example/pytest>` .
2018-05-25 15:54:03 +08:00
tox helps you to setup virtualenv environments with pre-defined
2013-12-10 21:54:13 +08:00
dependencies and then executing a pre-configured test command with
options. It will run tests against the installed package and not
against your source code checkout, helping to detect packaging
glitches.
2021-10-11 20:28:27 +08:00
Do not run via setuptools
-------------------------
Integration with setuptools is **not recommended** ,
i.e. you should not be using `` python setup.py test `` or `` pytest-runner `` ,
and may stop working in the future.
This is deprecated since it depends on deprecated features of setuptools
and relies on features that break security mechanisms in pip.
For example 'setup_requires' and 'tests_require' bypass `` pip --require-hashes `` .
For more information and migration instructions,
see the `pytest-runner notice <https://github.com/pytest-dev/pytest-runner#deprecation-notice> `_ .
See also `pypa/setuptools#1684 <https://github.com/pypa/setuptools/issues/1684> `_ .
setuptools intends to
`remove the test command <https://github.com/pypa/setuptools/issues/931> `_ .