2010-10-28 15:29:56 +08:00
|
|
|
.. highlightlang:: python
|
2010-11-25 19:11:10 +08:00
|
|
|
.. _`goodpractises`:
|
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
|
|
|
=================================================
|
|
|
|
|
2010-11-06 06:37:25 +08:00
|
|
|
Work with virtual environments
|
2010-10-14 01:30:00 +08:00
|
|
|
-----------------------------------------------------------
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
We recommend to use virtualenv_ environments and use pip_
|
|
|
|
(or easy_install_) for installing your application and any dependencies
|
2013-12-10 21:54:13 +08:00
|
|
|
as well as the ``pytest`` package itself. This way you will get an isolated
|
|
|
|
and reproducible environment. Given you have installed virtualenv_
|
|
|
|
and execute it from the command line, here is an example session for unix
|
|
|
|
or windows::
|
|
|
|
|
|
|
|
virtualenv . # create a virtualenv directory in the current directory
|
|
|
|
|
|
|
|
source bin/activate # on unix
|
|
|
|
|
|
|
|
scripts/activate # on Windows
|
|
|
|
|
|
|
|
We can now install pytest::
|
|
|
|
|
|
|
|
pip install pytest
|
|
|
|
|
|
|
|
Due to the ``activate`` step above the ``pip`` will come from
|
|
|
|
the virtualenv directory and install any package into the isolated
|
2014-01-18 19:31:33 +08:00
|
|
|
virtual environment.
|
2013-12-10 21:54:13 +08:00
|
|
|
|
|
|
|
Choosing a test layout / import rules
|
|
|
|
------------------------------------------
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
``pytest`` supports two common test layouts:
|
2013-12-10 21:54:13 +08:00
|
|
|
|
|
|
|
* putting tests into an extra directory outside your actual application
|
|
|
|
code, useful if you have many functional tests or for other reasons
|
|
|
|
want to keep tests separate from actual application code (often a good
|
|
|
|
idea)::
|
|
|
|
|
2015-09-21 19:00:48 +08:00
|
|
|
setup.py # your setuptools Python package metadata
|
2013-12-10 21:54:13 +08:00
|
|
|
mypkg/
|
|
|
|
__init__.py
|
|
|
|
appmodule.py
|
|
|
|
tests/
|
|
|
|
test_app.py
|
|
|
|
...
|
|
|
|
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
* inlining test directories into your application package, useful if you
|
2013-12-10 21:54:13 +08:00
|
|
|
have direct relation between (unit-)test and application modules and
|
|
|
|
want to distribute your tests along with your application::
|
|
|
|
|
2015-09-21 19:00:48 +08:00
|
|
|
setup.py # your setuptools Python package metadata
|
2013-12-10 21:54:13 +08:00
|
|
|
mypkg/
|
|
|
|
__init__.py
|
|
|
|
appmodule.py
|
|
|
|
...
|
|
|
|
test/
|
|
|
|
test_app.py
|
|
|
|
...
|
|
|
|
|
|
|
|
Important notes relating to both schemes:
|
|
|
|
|
|
|
|
- **make sure that "mypkg" is importable**, for example by typing once::
|
|
|
|
|
|
|
|
pip install -e . # install package using setup.py in editable mode
|
|
|
|
|
|
|
|
- **avoid "__init__.py" files in your test directories**.
|
2014-01-18 19:31:33 +08:00
|
|
|
This way your tests can run easily against an installed version
|
2014-03-27 11:21:58 +08:00
|
|
|
of ``mypkg``, independently from the installed package if it contains
|
2014-01-18 19:31:33 +08:00
|
|
|
the tests or not.
|
2013-12-10 21:54:13 +08:00
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
- With inlined tests you might put ``__init__.py`` into test
|
2013-12-10 21:54:13 +08:00
|
|
|
directories and make them installable as part of your application.
|
|
|
|
Using the ``py.test --pyargs mypkg`` invocation pytest will
|
|
|
|
discover where mypkg is installed and collect tests from there.
|
|
|
|
With the "external" test you can still distribute tests but they
|
|
|
|
will not be installed or become importable.
|
|
|
|
|
|
|
|
Typically you can run tests by pointing to test directories or modules::
|
|
|
|
|
|
|
|
py.test tests/test_app.py # for external test dirs
|
|
|
|
py.test mypkg/test/test_app.py # for inlined test dirs
|
|
|
|
py.test mypkg # run tests in all below test directories
|
|
|
|
py.test # run all tests below current dir
|
|
|
|
...
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
Because of the above ``editable install`` mode you can change your
|
2013-12-10 21:54:13 +08:00
|
|
|
source code (both tests and the app) and rerun tests at will.
|
|
|
|
Once you are done with your work, you can `use tox`_ to make sure
|
2014-01-18 19:31:33 +08:00
|
|
|
that the package is really correct and tests pass in all
|
2013-12-10 21:54:13 +08:00
|
|
|
required configurations.
|
|
|
|
|
|
|
|
.. 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::
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
If ``pytest`` finds a "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
|
|
|
|
to avoid surprises such as a test modules getting imported twice.
|
|
|
|
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
.. _`virtualenv`: http://pypi.python.org/pypi/virtualenv
|
|
|
|
.. _`buildout`: http://www.buildout.org/
|
|
|
|
.. _pip: http://pypi.python.org/pypi/pip
|
|
|
|
|
2013-12-10 21:54:13 +08:00
|
|
|
.. _`use tox`:
|
|
|
|
|
2011-02-17 21:46:40 +08:00
|
|
|
Use tox and Continuous Integration servers
|
2010-11-25 19:11:10 +08:00
|
|
|
-------------------------------------------------
|
|
|
|
|
2013-12-10 21:54:13 +08:00
|
|
|
If you frequently release code and want to make sure that your actual
|
|
|
|
package passes all tests you may want to look into `tox`_, the
|
|
|
|
virtualenv test automation tool and its `pytest support
|
|
|
|
<http://testrun.org/tox/latest/example/pytest.html>`_.
|
|
|
|
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.
|
|
|
|
|
|
|
|
If you want to use Jenkins_ you can use the ``--junitxml=PATH`` option
|
|
|
|
to create a JUnitXML file that Jenkins_ can pick up and generate reports.
|
2010-11-25 19:11:10 +08:00
|
|
|
|
|
|
|
.. _standalone:
|
|
|
|
.. _`genscript method`:
|
|
|
|
|
2015-07-25 19:59:00 +08:00
|
|
|
(deprecated) Create a pytest standalone script
|
2015-08-04 19:11:02 +08:00
|
|
|
-----------------------------------------------
|
2010-11-25 19:11:10 +08:00
|
|
|
|
2013-12-10 21:54:13 +08:00
|
|
|
If you are a maintainer or application developer and want people
|
2014-01-18 19:31:33 +08:00
|
|
|
who don't deal with python much to easily run tests you may generate
|
|
|
|
a standalone ``pytest`` script::
|
2010-11-25 19:11:10 +08:00
|
|
|
|
|
|
|
py.test --genscript=runtests.py
|
|
|
|
|
2013-12-10 21:54:13 +08:00
|
|
|
This generates a ``runtests.py`` script which is a fully functional basic
|
2014-01-18 19:31:33 +08:00
|
|
|
``pytest`` script, running unchanged under Python2 and Python3.
|
2010-11-25 19:11:10 +08:00
|
|
|
You can tell people to download the script and then e.g. run it like this::
|
|
|
|
|
|
|
|
python runtests.py
|
|
|
|
|
2015-05-04 20:42:01 +08:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
You must have pytest and its dependencies installed as an sdist, not
|
|
|
|
as wheels because genscript need the source code for generating a
|
|
|
|
standalone script.
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-25 19:11:10 +08:00
|
|
|
|
2015-09-21 19:00:48 +08:00
|
|
|
Integrating with setuptools / ``python setup.py test``
|
|
|
|
------------------------------------------------------
|
2010-11-25 19:11:10 +08:00
|
|
|
|
2015-09-21 19:00:48 +08:00
|
|
|
You can integrate test runs into your
|
2010-11-25 19:11:10 +08:00
|
|
|
setuptools based project. Use the `genscript method`_
|
2014-01-18 19:31:33 +08:00
|
|
|
to generate a standalone ``pytest`` script::
|
2010-11-25 19:11:10 +08:00
|
|
|
|
|
|
|
py.test --genscript=runtests.py
|
|
|
|
|
|
|
|
and make this script part of your distribution and then add
|
|
|
|
this to your ``setup.py`` file::
|
|
|
|
|
|
|
|
from distutils.core import setup, Command
|
|
|
|
# you can also import from setuptools
|
|
|
|
|
|
|
|
class PyTest(Command):
|
|
|
|
user_options = []
|
|
|
|
def initialize_options(self):
|
|
|
|
pass
|
2014-03-07 20:51:51 +08:00
|
|
|
|
2010-11-25 19:11:10 +08:00
|
|
|
def finalize_options(self):
|
|
|
|
pass
|
2014-03-07 20:51:51 +08:00
|
|
|
|
2010-11-25 19:11:10 +08:00
|
|
|
def run(self):
|
2015-01-14 20:38:37 +08:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2012-11-10 15:29:43 +08:00
|
|
|
errno = subprocess.call([sys.executable, 'runtests.py'])
|
2010-11-25 19:11:10 +08:00
|
|
|
raise SystemExit(errno)
|
2014-03-07 20:51:51 +08:00
|
|
|
|
|
|
|
|
2010-11-25 19:11:10 +08:00
|
|
|
setup(
|
|
|
|
#...,
|
|
|
|
cmdclass = {'test': PyTest},
|
|
|
|
#...,
|
|
|
|
)
|
|
|
|
|
|
|
|
If you now type::
|
|
|
|
|
|
|
|
python setup.py test
|
|
|
|
|
2012-11-10 15:29:43 +08:00
|
|
|
this will execute your tests using ``runtests.py``. As this is a
|
2014-01-18 19:31:33 +08:00
|
|
|
standalone version of ``pytest`` no prior installation whatsoever is
|
2010-11-25 19:11:10 +08:00
|
|
|
required for calling the test command. You can also pass additional
|
2011-03-04 06:40:38 +08:00
|
|
|
arguments to the subprocess-calls such as your test directory or other
|
2010-11-25 19:11:10 +08:00
|
|
|
options.
|
|
|
|
|
2012-03-15 22:15:21 +08:00
|
|
|
|
2013-08-01 15:42:44 +08:00
|
|
|
Integration with setuptools test commands
|
2012-03-15 22:15:21 +08:00
|
|
|
----------------------------------------------------
|
|
|
|
|
2013-12-10 21:54:13 +08:00
|
|
|
Setuptools supports writing our own Test command for invoking pytest.
|
|
|
|
Most often it is better to use tox_ instead, but here is how you can
|
|
|
|
get started with setuptools integration::
|
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):
|
2014-05-20 01:35:28 +08:00
|
|
|
user_options = [('pytest-args=', 'a', "Arguments to pass to py.test")]
|
|
|
|
|
|
|
|
def initialize_options(self):
|
|
|
|
TestCommand.initialize_options(self)
|
2014-09-05 21:34:01 +08:00
|
|
|
self.pytest_args = []
|
2014-05-20 01:35:28 +08:00
|
|
|
|
2012-03-15 22:15:21 +08:00
|
|
|
def finalize_options(self):
|
|
|
|
TestCommand.finalize_options(self)
|
|
|
|
self.test_args = []
|
|
|
|
self.test_suite = True
|
2014-03-07 20:51:51 +08:00
|
|
|
|
2012-03-15 22:15:21 +08:00
|
|
|
def run_tests(self):
|
|
|
|
#import here, cause outside the eggs aren't loaded
|
|
|
|
import pytest
|
2014-05-20 01:35:28 +08:00
|
|
|
errno = pytest.main(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"
|
|
|
|
|
|
|
|
is equivalent to running ``py.test --durations=5``.
|
2012-03-15 22:15:21 +08:00
|
|
|
|
2015-07-14 08:59:37 +08:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
For a more powerful solution, take a look at the
|
|
|
|
`pytest-runner <https://pypi.python.org/pypi/pytest-runner>`_ plugin.
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
.. _`test discovery`:
|
|
|
|
.. _`Python test discovery`:
|
2012-03-15 22:15:21 +08:00
|
|
|
|
2010-11-06 06:37:25 +08:00
|
|
|
Conventions for Python test discovery
|
|
|
|
-------------------------------------------------
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
``pytest`` implements the following standard test discovery:
|
2010-11-06 06:37:25 +08:00
|
|
|
|
2015-07-09 09:51:18 +08:00
|
|
|
* collection starts from paths specified in :confval:`testpaths` if configured,
|
|
|
|
otherwise from initial command line arguments which may be directories,
|
|
|
|
filenames or test ids. If :confval:`testpaths` is not configured and no
|
|
|
|
directories or files were given in the command line, start collection from
|
|
|
|
the current directory.
|
2010-11-06 06:37:25 +08:00
|
|
|
* recurse into directories, unless they match :confval:`norecursedirs`
|
2014-09-10 15:57:12 +08:00
|
|
|
* ``test_*.py`` or ``*_test.py`` files, imported by their `test package name`_.
|
2010-11-06 06:37:25 +08:00
|
|
|
* ``Test`` prefixed test classes (without an ``__init__`` method)
|
|
|
|
* ``test_`` prefixed test functions or methods are test items
|
|
|
|
|
2011-08-19 13:58:50 +08:00
|
|
|
For examples of how to customize your test discovery :doc:`example/pythoncollection`.
|
2010-11-06 06:37:25 +08:00
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
Within Python modules, ``pytest`` also discovers tests using the standard
|
2010-11-06 06:37:25 +08:00
|
|
|
:ref:`unittest.TestCase <unittest.TestCase>` subclassing technique.
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
.. include:: links.inc
|