2010-10-14 01:30:00 +08:00
|
|
|
|
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
|
|
|
|
2010-11-02 07:53:53 +08:00
|
|
|
Good Integration Practises
|
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
|
|
|
-----------------------------------------------------------
|
|
|
|
|
2011-03-04 06:40:38 +08:00
|
|
|
We recommend to use virtualenv_ environments and use easy_install_
|
2010-11-02 07:53:53 +08:00
|
|
|
(or pip_) for installing your application dependencies as well as
|
2011-03-04 06:40:38 +08:00
|
|
|
the ``pytest`` package itself. This way you will get a much more reproducible
|
2010-11-02 07:53:53 +08:00
|
|
|
environment. A good tool to help you automate test runs against multiple
|
2011-03-04 06:40:38 +08:00
|
|
|
dependency configurations or Python interpreters is `tox`_.
|
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
|
|
|
|
|
2011-02-17 21:46:40 +08:00
|
|
|
Use tox and Continuous Integration servers
|
2010-11-25 19:11:10 +08:00
|
|
|
-------------------------------------------------
|
|
|
|
|
2011-12-05 18:10:48 +08:00
|
|
|
If you frequently release code to the public you
|
2010-11-25 19:11:10 +08:00
|
|
|
may want to look into `tox`_, the virtualenv test automation
|
|
|
|
tool and its `pytest support <http://codespeak.net/tox/example/pytest.html>`_.
|
2011-03-04 06:40:38 +08:00
|
|
|
The basic idea is to generate a JUnitXML file through the ``--junitxml=PATH`` option and have a continuous integration server like Jenkins_ pick it up
|
|
|
|
and generate reports.
|
2010-11-25 19:11:10 +08:00
|
|
|
|
|
|
|
.. _standalone:
|
|
|
|
.. _`genscript method`:
|
|
|
|
|
2011-12-05 18:10:48 +08:00
|
|
|
Create a py.test standalone script
|
2010-11-25 19:11:10 +08:00
|
|
|
-------------------------------------------
|
|
|
|
|
|
|
|
If you are a maintainer or application developer and want others
|
|
|
|
to easily run tests you can generate a completely standalone "py.test"
|
|
|
|
script::
|
|
|
|
|
|
|
|
py.test --genscript=runtests.py
|
|
|
|
|
|
|
|
generates a ``runtests.py`` script which is a fully functional basic
|
|
|
|
``py.test`` script, running unchanged under Python2 and Python3.
|
|
|
|
You can tell people to download the script and then e.g. run it like this::
|
|
|
|
|
|
|
|
python runtests.py
|
|
|
|
|
|
|
|
|
|
|
|
.. _`Distribute for installation`: http://pypi.python.org/pypi/distribute#installation-instructions
|
|
|
|
.. _`distribute installation`: http://pypi.python.org/pypi/distribute
|
|
|
|
|
|
|
|
|
|
|
|
Integrating with distutils / ``python setup.py test``
|
|
|
|
--------------------------------------------------------
|
|
|
|
|
2011-08-19 13:58:50 +08:00
|
|
|
You can integrate test runs into your distutils or
|
2010-11-25 19:11:10 +08:00
|
|
|
setuptools based project. Use the `genscript method`_
|
|
|
|
to generate a standalone py.test script::
|
|
|
|
|
|
|
|
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
|
|
|
|
def finalize_options(self):
|
|
|
|
pass
|
|
|
|
def run(self):
|
|
|
|
import sys,subprocess
|
|
|
|
errno = subprocess.call([sys.executable, 'runtest.py'])
|
|
|
|
raise SystemExit(errno)
|
|
|
|
setup(
|
|
|
|
#...,
|
|
|
|
cmdclass = {'test': PyTest},
|
|
|
|
#...,
|
|
|
|
)
|
|
|
|
|
|
|
|
If you now type::
|
|
|
|
|
|
|
|
python setup.py test
|
|
|
|
|
|
|
|
this will execute your tests using ``runtest.py``. As this is a
|
|
|
|
standalone version of ``py.test`` no prior installation whatsoever is
|
|
|
|
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.
|
|
|
|
|
2010-11-06 06:37:25 +08:00
|
|
|
.. _`test discovery`:
|
2010-11-25 02:02:08 +08:00
|
|
|
.. _`Python test discovery`:
|
2010-11-06 06:37:25 +08:00
|
|
|
|
|
|
|
Conventions for Python test discovery
|
|
|
|
-------------------------------------------------
|
|
|
|
|
|
|
|
``py.test`` implements the following standard test discovery:
|
|
|
|
|
2011-03-04 06:40:38 +08:00
|
|
|
* collection starts from the initial command line arguments
|
2010-11-06 06:37:25 +08:00
|
|
|
which may be directories, filenames or test ids.
|
|
|
|
* recurse into directories, unless they match :confval:`norecursedirs`
|
|
|
|
* ``test_*.py`` or ``*_test.py`` files, imported by their `package name`_.
|
|
|
|
* ``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
|
|
|
|
2011-08-19 13:58:50 +08:00
|
|
|
Within Python modules, py.test 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
|
|
|
|
2010-11-02 07:53:53 +08:00
|
|
|
Choosing a test layout / import rules
|
|
|
|
------------------------------------------
|
|
|
|
|
|
|
|
py.test supports common test layouts:
|
|
|
|
|
|
|
|
* inlining test directories into your application package, useful if you want to
|
|
|
|
keep (unit) tests and actually tested code close together::
|
|
|
|
|
|
|
|
mypkg/
|
|
|
|
__init__.py
|
|
|
|
appmodule.py
|
|
|
|
...
|
|
|
|
test/
|
|
|
|
test_app.py
|
|
|
|
...
|
|
|
|
|
|
|
|
* putting tests into an extra directory outside your actual application
|
|
|
|
code, useful if you have many functional tests or want to keep
|
|
|
|
tests separate from actual application code::
|
|
|
|
|
|
|
|
mypkg/
|
|
|
|
__init__.py
|
|
|
|
appmodule.py
|
|
|
|
tests/
|
|
|
|
test_app.py
|
|
|
|
...
|
2010-10-14 07:25:09 +08:00
|
|
|
|
2011-08-19 13:58:50 +08:00
|
|
|
In both cases you usually need to make sure that ``mypkg`` is importable,
|
|
|
|
for example by using the setuptools ``python setup.py develop`` method.
|
|
|
|
|
|
|
|
You can run your tests by pointing to it::
|
2010-10-14 07:25:09 +08:00
|
|
|
|
2010-11-02 07:53:53 +08:00
|
|
|
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
|
|
|
|
...
|
2010-10-14 07:25:09 +08:00
|
|
|
|
2010-11-06 06:37:25 +08:00
|
|
|
.. _`package name`:
|
2010-11-06 06:37:31 +08:00
|
|
|
|
|
|
|
.. note::
|
2010-10-14 07:25:09 +08:00
|
|
|
|
2011-08-19 13:58:50 +08:00
|
|
|
If py.test finds a "a/b/test_module.py" test file while
|
|
|
|
recursing into the filesystem it determines the import name
|
|
|
|
as follows:
|
2010-11-02 07:53:53 +08:00
|
|
|
|
2011-03-04 06:40:38 +08:00
|
|
|
* find ``basedir`` -- this is the first "upward" (towards the root)
|
2011-08-19 13:58:50 +08:00
|
|
|
directory not containing an ``__init__.py``. If both the ``a``
|
2011-12-05 18:10:48 +08:00
|
|
|
and ``b`` directories contain an ``__init__.py`` the basedir will
|
2011-08-19 13:58:50 +08:00
|
|
|
be the parent dir of ``a``.
|
2010-11-02 07:53:53 +08:00
|
|
|
|
2011-08-19 13:58:50 +08:00
|
|
|
* perform ``sys.path.insert(0, basedir)`` to make the test module
|
|
|
|
importable under the fully qualified import name.
|
2010-11-02 07:53:53 +08:00
|
|
|
|
2011-08-19 13:58:50 +08:00
|
|
|
* ``import a.b.test_module`` where the path is determined
|
|
|
|
by converting path separators ``/`` into "." characters. This means
|
2010-11-06 06:37:31 +08:00
|
|
|
you must follow the convention of having directory and file
|
2011-08-19 13:58:50 +08:00
|
|
|
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-11-02 07:53:53 +08:00
|
|
|
|
2010-10-28 15:29:56 +08:00
|
|
|
|
2010-10-14 01:30:00 +08:00
|
|
|
.. include:: links.inc
|