Skip to content

Commit 41f1979

Browse files
committed
Merge pull request pytest-dev#1212 from nicoddemus/goodpractices
Goodpractises docs reorganization/review
2 parents 427e6c3 + 8c74bb0 commit 41f1979

File tree

3 files changed

+68
-127
lines changed

3 files changed

+68
-127
lines changed

doc/en/customize.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ Builtin configuration file options
162162
.. versionadded:: 2.8
163163

164164
Sets list of directories that should be searched for tests when
165-
no specific directories or files are given in the command line when
165+
no specific directories, files or test ids are given in the command line when
166166
executing pytest from the :ref:`rootdir <rootdir>` directory.
167167
Useful when all project tests are in a known location to speed up
168168
test collection and to avoid picking up undesired tests by accident.

doc/en/faq.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ be the same, confusing the reinterpreter and obfuscating the initial
7676
error (this is also explained at the command line if it happens).
7777

7878
You can also turn off all assertion interaction using the
79-
``--assertmode=off`` option.
79+
``--assert=plain`` option.
8080

8181
.. _`py namespaces`: index.html
8282
.. _`py/__init__.py`: http://bitbucket.org/hpk42/py-trunk/src/trunk/py/__init__.py

doc/en/goodpractises.rst

+66-125
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,28 @@
44
Good Integration Practices
55
=================================================
66

7-
Work with virtual environments
8-
-----------------------------------------------------------
97

10-
We recommend to use virtualenv_ environments and use pip_
11-
(or easy_install_) for installing your application and any dependencies
12-
as well as the ``pytest`` package itself. This way you will get an isolated
13-
and reproducible environment. Given you have installed virtualenv_
14-
and execute it from the command line, here is an example session for unix
15-
or windows::
8+
.. _`test discovery`:
9+
.. _`Python test discovery`:
1610

17-
virtualenv . # create a virtualenv directory in the current directory
11+
Conventions for Python test discovery
12+
-------------------------------------------------
1813

19-
source bin/activate # on unix
14+
``pytest`` implements the following standard test discovery:
2015

21-
scripts/activate # on Windows
16+
* If no arguments are specified then collection starts from :confval:`testpaths`
17+
(if configured) or the current directory. Alternatively, command line arguments
18+
can be used in any combination of directories, file names or node ids.
19+
* recurse into directories, unless they match :confval:`norecursedirs`
20+
* ``test_*.py`` or ``*_test.py`` files, imported by their `test package name`_.
21+
* ``Test`` prefixed test classes (without an ``__init__`` method)
22+
* ``test_`` prefixed test functions or methods are test items
2223

23-
We can now install pytest::
24+
For examples of how to customize your test discovery :doc:`example/pythoncollection`.
2425

25-
pip install pytest
26+
Within Python modules, ``pytest`` also discovers tests using the standard
27+
:ref:`unittest.TestCase <unittest.TestCase>` subclassing technique.
2628

27-
Due to the ``activate`` step above the ``pip`` will come from
28-
the virtualenv directory and install any package into the isolated
29-
virtual environment.
3029

3130
Choosing a test layout / import rules
3231
------------------------------------------
@@ -135,8 +134,13 @@ required configurations.
135134

136135
.. _`use tox`:
137136

138-
Use tox and Continuous Integration servers
139-
-------------------------------------------------
137+
Tox
138+
------
139+
140+
For development, we recommend to use virtualenv_ environments and pip_
141+
for installing your application and any dependencies
142+
as well as the ``pytest`` package itself. This ensures your code and
143+
dependencies are isolated from the system Python installation.
140144

141145
If you frequently release code and want to make sure that your actual
142146
package passes all tests you may want to look into `tox`_, the
@@ -148,45 +152,21 @@ options. It will run tests against the installed package and not
148152
against your source code checkout, helping to detect packaging
149153
glitches.
150154

151-
If you want to use Jenkins_ you can use the ``--junitxml=PATH`` option
152-
to create a JUnitXML file that Jenkins_ can pick up and generate reports.
153-
154-
.. _standalone:
155-
.. _`genscript method`:
156-
157-
(deprecated) Create a pytest standalone script
158-
-----------------------------------------------
159-
160-
If you are a maintainer or application developer and want people
161-
who don't deal with python much to easily run tests you may generate
162-
a standalone ``pytest`` script::
163-
164-
py.test --genscript=runtests.py
165-
166-
This generates a ``runtests.py`` script which is a fully functional basic
167-
``pytest`` script, running unchanged under Python2 and Python3.
168-
You can tell people to download the script and then e.g. run it like this::
169-
170-
python runtests.py
171-
172-
.. note::
173-
174-
You must have pytest and its dependencies installed as an sdist, not
175-
as wheels because genscript need the source code for generating a
176-
standalone script.
177-
155+
Continuous integration services such as Jenkins_ can make use of the
156+
``--junitxml=PATH`` option to create a JUnitXML file and generate reports.
178157

179158

180159
Integrating with setuptools / ``python setup.py test`` / ``pytest-runner``
181160
--------------------------------------------------------------------------
182161

183162
You can integrate test runs into your setuptools based project
184-
with pytest-runner.
163+
with the `pytest-runner <https://pypi.python.org/pypi/pytest-runner>`_ plugin.
185164

186-
Add this to ``setup.py`` file::
165+
Add this to ``setup.py`` file:
187166

188-
from distutils.core import setup
189-
# you can also import from setuptools
167+
.. code-block:: python
168+
169+
from setuptools import setup
190170
191171
setup(
192172
#...,
@@ -195,7 +175,11 @@ Add this to ``setup.py`` file::
195175
#...,
196176
)
197177
198-
And create an alias into ``setup.cfg``file::
178+
179+
And create an alias into ``setup.cfg`` file:
180+
181+
182+
.. code-block:: ini
199183
200184
[aliases]
201185
test=pytest
@@ -210,59 +194,14 @@ required for calling the test command. You can also pass additional
210194
arguments to py.test such as your test directory or other
211195
options using ``--addopts``.
212196

213-
Integrating with setuptools / ``python setup.py test`` / ``genscript``
214-
----------------------------------------------------------------------
215-
216-
You can integrate test runs into your
217-
setuptools based project. Use the `genscript method`_
218-
to generate a standalone ``pytest`` script::
219-
220-
py.test --genscript=runtests.py
221-
222-
and make this script part of your distribution and then add
223-
this to your ``setup.py`` file::
224-
225-
from distutils.core import setup, Command
226-
# you can also import from setuptools
227-
228-
class PyTest(Command):
229-
user_options = []
230-
def initialize_options(self):
231-
pass
232-
233-
def finalize_options(self):
234-
pass
235-
236-
def run(self):
237-
import subprocess
238-
import sys
239-
errno = subprocess.call([sys.executable, 'runtests.py'])
240-
raise SystemExit(errno)
241-
242-
243-
setup(
244-
#...,
245-
cmdclass = {'test': PyTest},
246-
#...,
247-
)
248-
249-
If you now type::
250-
251-
python setup.py test
252-
253-
this will execute your tests using ``runtests.py``. As this is a
254-
standalone version of ``pytest`` no prior installation whatsoever is
255-
required for calling the test command. You can also pass additional
256-
arguments to the subprocess-calls such as your test directory or other
257-
options.
258197

198+
Manual Integration
199+
^^^^^^^^^^^^^^^^^^
259200

260-
Integration with setuptools test commands
261-
----------------------------------------------------
201+
If for some reason you don't want/can't use ``pytest-runner``, you can write
202+
your own setuptools Test command for invoking pytest.
262203

263-
Setuptools supports writing our own Test command for invoking pytest.
264-
Most often it is better to use tox_ instead, but here is how you can
265-
get started with setuptools integration::
204+
.. code-block:: python
266205
267206
import sys
268207
@@ -276,11 +215,6 @@ get started with setuptools integration::
276215
TestCommand.initialize_options(self)
277216
self.pytest_args = []
278217
279-
def finalize_options(self):
280-
TestCommand.finalize_options(self)
281-
self.test_args = []
282-
self.test_suite = True
283-
284218
def run_tests(self):
285219
#import here, cause outside the eggs aren't loaded
286220
import pytest
@@ -306,32 +240,39 @@ using the ``--pytest-args`` or ``-a`` command-line option. For example::
306240

307241
is equivalent to running ``py.test --durations=5``.
308242

309-
.. seealso::
310243

311-
For a more powerful solution, take a look at the
312-
`pytest-runner <https://pypi.python.org/pypi/pytest-runner>`_ plugin.
244+
.. _standalone:
245+
.. _`genscript method`:
313246

314-
.. _`test discovery`:
315-
.. _`Python test discovery`:
247+
(deprecated) Create a pytest standalone script
248+
-----------------------------------------------
316249

317-
Conventions for Python test discovery
318-
-------------------------------------------------
250+
.. deprecated:: 2.8
319251

320-
``pytest`` implements the following standard test discovery:
252+
.. note::
321253

322-
* collection starts from paths specified in :confval:`testpaths` if configured,
323-
otherwise from initial command line arguments which may be directories,
324-
filenames or test ids. If :confval:`testpaths` is not configured and no
325-
directories or files were given in the command line, start collection from
326-
the current directory.
327-
* recurse into directories, unless they match :confval:`norecursedirs`
328-
* ``test_*.py`` or ``*_test.py`` files, imported by their `test package name`_.
329-
* ``Test`` prefixed test classes (without an ``__init__`` method)
330-
* ``test_`` prefixed test functions or methods are test items
254+
``genscript`` has been deprecated because:
331255

332-
For examples of how to customize your test discovery :doc:`example/pythoncollection`.
256+
* It cannot support plugins, rendering its usefulness extremely limited;
257+
* Tooling has become much better since ``genscript`` was introduced;
258+
* It is possible to build a zipped ``pytest`` application without the
259+
shortcomings above.
260+
261+
There's no planned version in which this command will be removed
262+
at the moment of this writing, but its use is discouraged for new
263+
applications.
264+
265+
If you are a maintainer or application developer and want people
266+
who don't deal with python much to easily run tests you may generate
267+
a standalone ``pytest`` script::
268+
269+
py.test --genscript=runtests.py
270+
271+
This generates a ``runtests.py`` script which is a fully functional basic
272+
``pytest`` script, running unchanged under Python2 and Python3.
273+
You can tell people to download the script and then e.g. run it like this::
274+
275+
python runtests.py
333276

334-
Within Python modules, ``pytest`` also discovers tests using the standard
335-
:ref:`unittest.TestCase <unittest.TestCase>` subclassing technique.
336277

337278
.. include:: links.inc

0 commit comments

Comments
 (0)