2010-10-14 01:30:00 +08:00
|
|
|
|
2010-10-28 15:29:56 +08:00
|
|
|
.. highlightlang:: python
|
2010-10-14 07:25:09 +08:00
|
|
|
.. _`good practises`:
|
|
|
|
|
2010-10-14 01:30:00 +08:00
|
|
|
Good Practises
|
|
|
|
=================================================
|
|
|
|
|
|
|
|
Recommendation: install tool and dependencies virtually
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
|
|
|
We recommend to work with virtual environments
|
|
|
|
(e.g. virtualenv_ or buildout_ based) and use easy_install_
|
|
|
|
(or pip_) for installing py.test/pylib and any dependencies
|
|
|
|
you need to run your tests. Local virtual Python environments
|
|
|
|
(as opposed to system-wide "global" environments) make for a more
|
|
|
|
reproducible and reliable test environment.
|
|
|
|
|
|
|
|
.. _`virtualenv`: http://pypi.python.org/pypi/virtualenv
|
|
|
|
.. _`buildout`: http://www.buildout.org/
|
|
|
|
.. _pip: http://pypi.python.org/pypi/pip
|
|
|
|
|
|
|
|
.. _standalone:
|
|
|
|
|
2010-10-14 07:25:09 +08:00
|
|
|
|
|
|
|
Choosing a test layout
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
py.test supports common test layouts.
|
|
|
|
|
|
|
|
XXX
|
|
|
|
|
2010-10-28 15:29:56 +08:00
|
|
|
.. _`genscript method`:
|
2010-10-14 07:25:09 +08:00
|
|
|
|
2010-10-14 01:30:00 +08:00
|
|
|
Generating a py.test standalone Script
|
|
|
|
-------------------------------------------
|
|
|
|
|
|
|
|
If you are a maintainer or application developer and want users
|
|
|
|
to run tests you can use a facility to generate a standalone
|
|
|
|
"py.test" script that you can tell users to run::
|
|
|
|
|
2010-10-28 15:29:56 +08:00
|
|
|
py.test --genscript=runtests.py
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
will generate a ``mytest`` script that is, in fact, a ``py.test`` under
|
|
|
|
disguise. You can tell people to download and then e.g. run it like this::
|
|
|
|
|
2010-10-28 15:29:56 +08:00
|
|
|
python runtests.py --pastebin=all
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
and ask them to send you the resulting URL. The resulting script has
|
|
|
|
all core features and runs unchanged under Python2 and Python3 interpreters.
|
|
|
|
|
|
|
|
.. _`Distribute for installation`: http://pypi.python.org/pypi/distribute#installation-instructions
|
|
|
|
.. _`distribute installation`: http://pypi.python.org/pypi/distribute
|
|
|
|
|
2010-10-28 15:29:56 +08:00
|
|
|
|
|
|
|
Integrating with distutils / ``python setup.py test``
|
|
|
|
--------------------------------------------------------
|
|
|
|
|
|
|
|
You can easily integrate test runs into your distutils or
|
|
|
|
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
|
|
|
|
arguments to the subprocess-calls like your test directory or other
|
|
|
|
options.
|
|
|
|
|
2010-10-14 01:30:00 +08:00
|
|
|
.. include:: links.inc
|