4
4
Good Integration Practices
5
5
=================================================
6
6
7
- Work with virtual environments
8
- -----------------------------------------------------------
9
7
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` :
16
10
17
- virtualenv . # create a virtualenv directory in the current directory
11
+ Conventions for Python test discovery
12
+ -------------------------------------------------
18
13
19
- source bin/activate # on unix
14
+ `` pytest `` implements the following standard test discovery:
20
15
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
22
23
23
- We can now install pytest::
24
+ For examples of how to customize your test discovery :doc: ` example/pythoncollection `.
24
25
25
- pip install pytest
26
+ Within Python modules, ``pytest `` also discovers tests using the standard
27
+ :ref: `unittest.TestCase <unittest.TestCase >` subclassing technique.
26
28
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.
30
29
31
30
Choosing a test layout / import rules
32
31
------------------------------------------
@@ -135,8 +134,13 @@ required configurations.
135
134
136
135
.. _`use tox` :
137
136
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.
140
144
141
145
If you frequently release code and want to make sure that your actual
142
146
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
148
152
against your source code checkout, helping to detect packaging
149
153
glitches.
150
154
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.
178
157
179
158
180
159
Integrating with setuptools / ``python setup.py test `` / ``pytest-runner ``
181
160
--------------------------------------------------------------------------
182
161
183
162
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 .
185
164
186
- Add this to ``setup.py `` file::
165
+ Add this to ``setup.py `` file:
187
166
188
- from distutils.core import setup
189
- # you can also import from setuptools
167
+ .. code-block :: python
168
+
169
+ from setuptools import setup
190
170
191
171
setup(
192
172
# ...,
@@ -195,7 +175,11 @@ Add this to ``setup.py`` file::
195
175
# ...,
196
176
)
197
177
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
199
183
200
184
[aliases]
201
185
test =pytest
@@ -210,59 +194,14 @@ required for calling the test command. You can also pass additional
210
194
arguments to py.test such as your test directory or other
211
195
options using ``--addopts ``.
212
196
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.
258
197
198
+ Manual Integration
199
+ ^^^^^^^^^^^^^^^^^^
259
200
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.
262
203
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
266
205
267
206
import sys
268
207
@@ -276,11 +215,6 @@ get started with setuptools integration::
276
215
TestCommand.initialize_options(self )
277
216
self .pytest_args = []
278
217
279
- def finalize_options(self):
280
- TestCommand.finalize_options(self)
281
- self.test_args = []
282
- self.test_suite = True
283
-
284
218
def run_tests (self ):
285
219
# import here, cause outside the eggs aren't loaded
286
220
import pytest
@@ -306,32 +240,39 @@ using the ``--pytest-args`` or ``-a`` command-line option. For example::
306
240
307
241
is equivalent to running ``py.test --durations=5 ``.
308
242
309
- .. seealso ::
310
243
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` :
313
246
314
- .. _ `test discovery` :
315
- .. _ `Python test discovery` :
247
+ (deprecated) Create a pytest standalone script
248
+ -----------------------------------------------
316
249
317
- Conventions for Python test discovery
318
- -------------------------------------------------
250
+ .. deprecated :: 2.8
319
251
320
- `` pytest `` implements the following standard test discovery :
252
+ .. note : :
321
253
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:
331
255
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
333
276
334
- Within Python modules, ``pytest `` also discovers tests using the standard
335
- :ref: `unittest.TestCase <unittest.TestCase >` subclassing technique.
336
277
337
278
.. include :: links.inc
0 commit comments