2010-10-28 15:29:56 +08:00
|
|
|
.. highlightlang:: 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
|
|
|
=================================================
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
* ``test_`` prefixed test functions or methods outside of class
|
|
|
|
* ``test_`` prefixed test functions or methods inside ``Test`` prefixed test classes (without an ``__init__`` method)
|
2015-12-02 08:27:27 +08:00
|
|
|
|
|
|
|
For examples of how to customize your test discovery :doc:`example/pythoncollection`.
|
|
|
|
|
|
|
|
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
|
|
|
|
to keep tests separate from actual application code (often a good idea)::
|
2013-12-10 21:54:13 +08:00
|
|
|
|
2017-03-10 07:41:33 +08:00
|
|
|
setup.py
|
2013-12-10 21:54:13 +08:00
|
|
|
mypkg/
|
|
|
|
__init__.py
|
2017-03-10 07:41:33 +08:00
|
|
|
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
|
|
|
...
|
|
|
|
|
2017-03-10 07:41:33 +08:00
|
|
|
This way your tests can run easily against an installed version
|
|
|
|
of ``mypkg``.
|
2013-12-10 21:54:13 +08:00
|
|
|
|
2017-03-10 07:41:33 +08:00
|
|
|
Note that using this scheme your test files must have **unique names**, because
|
|
|
|
``pytest`` will import them as *top-level* modules since there are no packages
|
2017-03-14 18:59:01 +08:00
|
|
|
to derive a full package name from. In other words, the test files in the example above will
|
|
|
|
be imported as ``test_app`` and ``test_view`` top-level modules by adding ``tests/`` to
|
|
|
|
``sys.path``.
|
2013-12-10 21:54:13 +08:00
|
|
|
|
2017-03-14 18:59:01 +08:00
|
|
|
If you need to have test modules with the same name, you might add ``__init__.py`` files to your
|
|
|
|
``tests`` folder and subfolders, changing them to packages::
|
|
|
|
|
2017-03-15 05:39:02 +08:00
|
|
|
setup.py
|
2013-12-10 21:54:13 +08:00
|
|
|
mypkg/
|
|
|
|
...
|
2017-03-14 18:59:01 +08:00
|
|
|
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.
|
|
|
|
|
2017-03-15 05:39:02 +08:00
|
|
|
In this situation, it is **strongly** suggested to use a ``src`` layout where application root package resides in a
|
|
|
|
sub-directory of your root::
|
2017-03-14 18:59:01 +08:00
|
|
|
|
2017-03-15 05:39:02 +08:00
|
|
|
setup.py
|
|
|
|
src/
|
|
|
|
mypkg/
|
|
|
|
__init__.py
|
|
|
|
app.py
|
|
|
|
view.py
|
|
|
|
tests/
|
|
|
|
__init__.py
|
|
|
|
foo/
|
|
|
|
__init__.py
|
|
|
|
test_view.py
|
|
|
|
bar/
|
|
|
|
__init__.py
|
|
|
|
test_view.py
|
2017-03-14 18:59:01 +08:00
|
|
|
|
|
|
|
|
2017-03-15 05:39:02 +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ș <https://blog.ionelmc.ro/2014/05/25/python-packaging/#the-structure>`_.
|
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
|
|
|
|
want to distribute them along with your application::
|
2013-12-10 21:54:13 +08:00
|
|
|
|
2017-03-10 07:41:33 +08:00
|
|
|
setup.py
|
2013-12-10 21:54:13 +08:00
|
|
|
mypkg/
|
|
|
|
__init__.py
|
2017-03-10 07:41:33 +08:00
|
|
|
app.py
|
|
|
|
view.py
|
2013-12-10 21:54:13 +08:00
|
|
|
test/
|
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
|
|
|
...
|
|
|
|
|
2017-08-25 22:01:24 +08:00
|
|
|
In this scheme, it is easy to run your tests using the ``--pyargs`` option::
|
2013-12-10 21:54:13 +08:00
|
|
|
|
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::
|
|
|
|
|
|
|
|
You can use Python3 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``
|
2014-01-18 19:31:33 +08:00
|
|
|
files from your directories it should just work on Python3.3 and above. 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::
|
|
|
|
|
2018-05-13 18:06:09 +08:00
|
|
|
If ``pytest`` finds an "a/b/test_module.py" test file while
|
2013-12-10 21:54:13 +08:00
|
|
|
recursing into the filesystem it determines the import name
|
|
|
|
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
|
|
|
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2018-04-26 21:45:48 +08:00
|
|
|
.. _`virtualenv`: https://pypi.org/project/virtualenv/
|
2010-10-14 01:30:00 +08:00
|
|
|
.. _`buildout`: http://www.buildout.org/
|
2018-04-26 21:45:48 +08:00
|
|
|
.. _pip: https://pypi.org/project/pip/
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2013-12-10 21:54:13 +08:00
|
|
|
.. _`use tox`:
|
|
|
|
|
2015-12-02 08:43:11 +08:00
|
|
|
Tox
|
2015-12-02 08:47:36 +08:00
|
|
|
------
|
|
|
|
|
|
|
|
For development, we recommend to use virtualenv_ environments and pip_
|
2015-12-02 08:52:22 +08:00
|
|
|
for installing your application and any dependencies
|
2015-12-02 08:47:36 +08:00
|
|
|
as well as the ``pytest`` package itself. This ensures your code and
|
|
|
|
dependencies are isolated from the system Python installation.
|
2010-11-25 19:11:10 +08:00
|
|
|
|
2017-03-10 07:41:33 +08:00
|
|
|
You can then install your package in "editable" mode::
|
|
|
|
|
|
|
|
pip install -e .
|
|
|
|
|
|
|
|
which lets you change your source code (both tests and application) and rerun tests at will.
|
2017-04-07 09:01:03 +08:00
|
|
|
This is similar to running `python setup.py develop` or `conda develop` in that it installs
|
2017-04-07 07:35:38 +08:00
|
|
|
your package using a symlink to your development code.
|
2017-03-10 07:41:33 +08:00
|
|
|
|
|
|
|
Once you are done with your work and want to make sure that your actual
|
2013-12-10 21:54:13 +08:00
|
|
|
package passes all tests you may want to look into `tox`_, the
|
|
|
|
virtualenv test automation tool and its `pytest support
|
2017-01-03 06:07:41 +08:00
|
|
|
<https://tox.readthedocs.io/en/latest/example/pytest.html>`_.
|
2013-12-10 21:54:13 +08:00
|
|
|
Tox helps you to setup virtualenv environments with pre-defined
|
|
|
|
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.
|
|
|
|
|
2015-05-04 20:42:01 +08:00
|
|
|
|
2015-11-02 19:12:01 +08:00
|
|
|
Integrating with setuptools / ``python setup.py test`` / ``pytest-runner``
|
|
|
|
--------------------------------------------------------------------------
|
2010-11-25 19:11:10 +08:00
|
|
|
|
2015-11-02 19:12:01 +08:00
|
|
|
You can integrate test runs into your setuptools based project
|
2018-04-26 21:45:48 +08:00
|
|
|
with the `pytest-runner <https://pypi.org/project/pytest-runner/>`_ plugin.
|
2015-12-02 08:43:11 +08:00
|
|
|
|
|
|
|
Add this to ``setup.py`` file:
|
2015-11-02 19:12:01 +08:00
|
|
|
|
2015-12-02 08:43:11 +08:00
|
|
|
.. code-block:: python
|
2015-11-02 19:12:01 +08:00
|
|
|
|
2015-12-03 04:30:12 +08:00
|
|
|
from setuptools import setup
|
2015-11-02 19:12:01 +08:00
|
|
|
|
|
|
|
setup(
|
|
|
|
#...,
|
|
|
|
setup_requires=['pytest-runner', ...],
|
|
|
|
tests_require=['pytest', ...],
|
|
|
|
#...,
|
|
|
|
)
|
|
|
|
|
2015-12-02 08:52:22 +08:00
|
|
|
|
|
|
|
And create an alias into ``setup.cfg`` file:
|
2015-12-02 08:43:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: ini
|
2015-11-02 19:12:01 +08:00
|
|
|
|
|
|
|
[aliases]
|
|
|
|
test=pytest
|
|
|
|
|
|
|
|
If you now type::
|
|
|
|
|
|
|
|
python setup.py test
|
|
|
|
|
|
|
|
this will execute your tests using ``pytest-runner``. As this is a
|
|
|
|
standalone version of ``pytest`` no prior installation whatsoever is
|
|
|
|
required for calling the test command. You can also pass additional
|
2016-06-21 22:16:57 +08:00
|
|
|
arguments to pytest such as your test directory or other
|
2015-11-02 19:12:01 +08:00
|
|
|
options using ``--addopts``.
|
|
|
|
|
2016-08-17 08:30:07 +08:00
|
|
|
You can also specify other pytest-ini options in your ``setup.cfg`` file
|
|
|
|
by putting them into a ``[tool:pytest]`` section:
|
|
|
|
|
|
|
|
.. code-block:: ini
|
|
|
|
|
|
|
|
[tool:pytest]
|
|
|
|
addopts = --verbose
|
|
|
|
python_files = testing/*/*.py
|
|
|
|
|
|
|
|
|
2015-12-02 08:43:11 +08:00
|
|
|
Manual Integration
|
|
|
|
^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
If for some reason you don't want/can't use ``pytest-runner``, you can write
|
|
|
|
your own setuptools Test command for invoking pytest.
|
2012-03-15 22:15:21 +08:00
|
|
|
|
2015-12-02 08:43:11 +08:00
|
|
|
.. code-block:: python
|
2012-03-15 22:15:21 +08:00
|
|
|
|
2012-07-07 13:40:51 +08:00
|
|
|
import sys
|
2012-03-15 22:15:21 +08:00
|
|
|
|
2014-03-07 20:51:51 +08:00
|
|
|
from setuptools.command.test import test as TestCommand
|
|
|
|
|
|
|
|
|
2012-03-15 22:15:21 +08:00
|
|
|
class PyTest(TestCommand):
|
2016-06-21 22:16:57 +08:00
|
|
|
user_options = [('pytest-args=', 'a', "Arguments to pass to pytest")]
|
2014-05-20 01:35:28 +08:00
|
|
|
|
|
|
|
def initialize_options(self):
|
|
|
|
TestCommand.initialize_options(self)
|
2017-08-16 06:26:14 +08:00
|
|
|
self.pytest_args = ''
|
2014-05-20 01:35:28 +08:00
|
|
|
|
2012-03-15 22:15:21 +08:00
|
|
|
def run_tests(self):
|
2016-10-20 22:30:58 +08:00
|
|
|
import shlex
|
2012-03-15 22:15:21 +08:00
|
|
|
#import here, cause outside the eggs aren't loaded
|
|
|
|
import pytest
|
2016-10-20 22:30:58 +08:00
|
|
|
errno = pytest.main(shlex.split(self.pytest_args))
|
2012-07-07 13:40:51 +08:00
|
|
|
sys.exit(errno)
|
2012-03-15 22:15:21 +08:00
|
|
|
|
2014-03-07 20:51:51 +08:00
|
|
|
|
2012-03-15 22:15:21 +08:00
|
|
|
setup(
|
2012-03-15 22:22:13 +08:00
|
|
|
#...,
|
2012-03-15 22:15:21 +08:00
|
|
|
tests_require=['pytest'],
|
2012-07-18 14:01:37 +08:00
|
|
|
cmdclass = {'test': PyTest},
|
2012-03-15 22:15:21 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
Now if you run::
|
|
|
|
|
|
|
|
python setup.py test
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
this will download ``pytest`` if needed and then run your tests
|
2014-05-20 01:35:28 +08:00
|
|
|
as you would expect it to. You can pass a single string of arguments
|
|
|
|
using the ``--pytest-args`` or ``-a`` command-line option. For example::
|
|
|
|
|
|
|
|
python setup.py test -a "--durations=5"
|
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
is equivalent to running ``pytest --durations=5``.
|
2012-03-15 22:15:21 +08:00
|
|
|
|
2015-07-14 08:59:37 +08:00
|
|
|
|
2010-10-14 01:30:00 +08:00
|
|
|
.. include:: links.inc
|