| 1 |
Changing standard (Python) test discovery |
| 2 |
=============================================== |
| 3 |
|
| 4 |
Ignore paths during test collection |
| 5 |
----------------------------------- |
| 6 |
|
| 7 |
You can easily ignore certain test directories and modules during collection |
| 8 |
by passing the ``--ignore=path`` option on the cli. ``pytest`` allows multiple |
| 9 |
``--ignore`` options. Example: |
| 10 |
|
| 11 |
.. code-block:: text |
| 12 |
|
| 13 |
tests/ |
| 14 |
|-- example |
| 15 |
| |-- test_example_01.py |
| 16 |
| |-- test_example_02.py |
| 17 |
| '-- test_example_03.py |
| 18 |
|-- foobar |
| 19 |
| |-- test_foobar_01.py |
| 20 |
| |-- test_foobar_02.py |
| 21 |
| '-- test_foobar_03.py |
| 22 |
'-- hello |
| 23 |
'-- world |
| 24 |
|-- test_world_01.py |
| 25 |
|-- test_world_02.py |
| 26 |
'-- test_world_03.py |
| 27 |
|
| 28 |
Now if you invoke ``pytest`` with ``--ignore=tests/foobar/test_foobar_03.py --ignore=tests/hello/``, |
| 29 |
you will see that ``pytest`` only collects test-modules, which do not match the patterns specified: |
| 30 |
|
| 31 |
.. code-block:: pytest |
| 32 |
|
| 33 |
=========================== test session starts ============================ |
| 34 |
platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y |
| 35 |
rootdir: $REGENDOC_TMPDIR, inifile: |
| 36 |
collected 5 items |
| 37 |
|
| 38 |
tests/example/test_example_01.py . [ 20%] |
| 39 |
tests/example/test_example_02.py . [ 40%] |
| 40 |
tests/example/test_example_03.py . [ 60%] |
| 41 |
tests/foobar/test_foobar_01.py . [ 80%] |
| 42 |
tests/foobar/test_foobar_02.py . [100%] |
| 43 |
|
| 44 |
========================= 5 passed in 0.02 seconds ========================= |
| 45 |
|
| 46 |
The ``--ignore-glob`` option allows to ignore test file paths based on Unix shell-style wildcards. |
| 47 |
If you want to exclude test-modules that end with ``_01.py``, execute ``pytest`` with ``--ignore-glob='*_01.py'``. |
| 48 |
|
| 49 |
Deselect tests during test collection |
| 50 |
------------------------------------- |
| 51 |
|
| 52 |
Tests can individually be deselected during collection by passing the ``--deselect=item`` option. |
| 53 |
For example, say ``tests/foobar/test_foobar_01.py`` contains ``test_a`` and ``test_b``. |
| 54 |
You can run all of the tests within ``tests/`` *except* for ``tests/foobar/test_foobar_01.py::test_a`` |
| 55 |
by invoking ``pytest`` with ``--deselect tests/foobar/test_foobar_01.py::test_a``. |
| 56 |
``pytest`` allows multiple ``--deselect`` options. |
| 57 |
|
| 58 |
Keeping duplicate paths specified from command line |
| 59 |
---------------------------------------------------- |
| 60 |
|
| 61 |
Default behavior of ``pytest`` is to ignore duplicate paths specified from the command line. |
| 62 |
Example: |
| 63 |
|
| 64 |
.. code-block:: pytest |
| 65 |
|
| 66 |
pytest path_a path_a |
| 67 |
|
| 68 |
... |
| 69 |
collected 1 item |
| 70 |
... |
| 71 |
|
| 72 |
Just collect tests once. |
| 73 |
|
| 74 |
To collect duplicate tests, use the ``--keep-duplicates`` option on the cli. |
| 75 |
Example: |
| 76 |
|
| 77 |
.. code-block:: pytest |
| 78 |
|
| 79 |
pytest --keep-duplicates path_a path_a |
| 80 |
|
| 81 |
... |
| 82 |
collected 2 items |
| 83 |
... |
| 84 |
|
| 85 |
As the collector just works on directories, if you specify twice a single test file, ``pytest`` will |
| 86 |
still collect it twice, no matter if the ``--keep-duplicates`` is not specified. |
| 87 |
Example: |
| 88 |
|
| 89 |
.. code-block:: pytest |
| 90 |
|
| 91 |
pytest test_a.py test_a.py |
| 92 |
|
| 93 |
... |
| 94 |
collected 2 items |
| 95 |
... |
| 96 |
|
| 97 |
|
| 98 |
Changing directory recursion |
| 99 |
----------------------------------------------------- |
| 100 |
|
| 101 |
You can set the :confval:`norecursedirs` option in an ini-file, for example your ``pytest.ini`` in the project root directory: |
| 102 |
|
| 103 |
.. code-block:: ini |
| 104 |
|
| 105 |
# content of pytest.ini |
| 106 |
[pytest] |
| 107 |
norecursedirs = .svn _build tmp* |
| 108 |
|
| 109 |
This would tell ``pytest`` to not recurse into typical subversion or sphinx-build directories or into any ``tmp`` prefixed directory. |
| 110 |
|
| 111 |
.. _`change naming conventions`: |
| 112 |
|
| 113 |
Changing naming conventions |
| 114 |
----------------------------------------------------- |
| 115 |
|
| 116 |
You can configure different naming conventions by setting |
| 117 |
the :confval:`python_files`, :confval:`python_classes` and |
| 118 |
:confval:`python_functions` in your :ref:`configuration file <config file formats>`. |
| 119 |
Here is an example: |
| 120 |
|
| 121 |
.. code-block:: ini |
| 122 |
|
| 123 |
# content of pytest.ini |
| 124 |
# Example 1: have pytest look for "check" instead of "test" |
| 125 |
[pytest] |
| 126 |
python_files = check_*.py |
| 127 |
python_classes = Check |
| 128 |
python_functions = *_check |
| 129 |
|
| 130 |
This would make ``pytest`` look for tests in files that match the ``check_* |
| 131 |
.py`` glob-pattern, ``Check`` prefixes in classes, and functions and methods |
| 132 |
that match ``*_check``. For example, if we have: |
| 133 |
|
| 134 |
.. code-block:: python |
| 135 |
|
| 136 |
# content of check_myapp.py |
| 137 |
class CheckMyApp: |
| 138 |
def simple_check(self): |
| 139 |
pass |
| 140 |
|
| 141 |
def complex_check(self): |
| 142 |
pass |
| 143 |
|
| 144 |
The test collection would look like this: |
| 145 |
|
| 146 |
.. code-block:: pytest |
| 147 |
|
| 148 |
$ pytest --collect-only |
| 149 |
=========================== test session starts ============================ |
| 150 |
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y |
| 151 |
rootdir: /home/sweet/project |
| 152 |
configfile: pytest.ini |
| 153 |
collected 2 items |
| 154 |
|
| 155 |
<Dir pythoncollection.rst-200> |
| 156 |
<Module check_myapp.py> |
| 157 |
<Class CheckMyApp> |
| 158 |
<Function simple_check> |
| 159 |
<Function complex_check> |
| 160 |
|
| 161 |
======================== 2 tests collected in 0.12s ======================== |
| 162 |
|
| 163 |
You can check for multiple glob patterns by adding a space between the patterns: |
| 164 |
|
| 165 |
.. code-block:: ini |
| 166 |
|
| 167 |
# Example 2: have pytest look for files with "test" and "example" |
| 168 |
# content of pytest.ini |
| 169 |
[pytest] |
| 170 |
python_files = test_*.py example_*.py |
| 171 |
|
| 172 |
.. note:: |
| 173 |
|
| 174 |
the ``python_functions`` and ``python_classes`` options has no effect |
| 175 |
for ``unittest.TestCase`` test discovery because pytest delegates |
| 176 |
discovery of test case methods to unittest code. |
| 177 |
|
| 178 |
Interpreting cmdline arguments as Python packages |
| 179 |
----------------------------------------------------- |
| 180 |
|
| 181 |
You can use the ``--pyargs`` option to make ``pytest`` try |
| 182 |
interpreting arguments as python package names, deriving |
| 183 |
their file system path and then running the test. For |
| 184 |
example if you have unittest2 installed you can type: |
| 185 |
|
| 186 |
.. code-block:: bash |
| 187 |
|
| 188 |
pytest --pyargs unittest2.test.test_skipping -q |
| 189 |
|
| 190 |
which would run the respective test module. Like with |
| 191 |
other options, through an ini-file and the :confval:`addopts` option you |
| 192 |
can make this change more permanently: |
| 193 |
|
| 194 |
.. code-block:: ini |
| 195 |
|
| 196 |
# content of pytest.ini |
| 197 |
[pytest] |
| 198 |
addopts = --pyargs |
| 199 |
|
| 200 |
Now a simple invocation of ``pytest NAME`` will check |
| 201 |
if NAME exists as an importable package/module and otherwise |
| 202 |
treat it as a filesystem path. |
| 203 |
|
| 204 |
Finding out what is collected |
| 205 |
----------------------------------------------- |
| 206 |
|
| 207 |
You can always peek at the collection tree without running tests like this: |
| 208 |
|
| 209 |
.. code-block:: pytest |
| 210 |
|
| 211 |
. $ pytest --collect-only pythoncollection.py |
| 212 |
=========================== test session starts ============================ |
| 213 |
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y |
| 214 |
rootdir: /home/sweet/project |
| 215 |
configfile: pytest.ini |
| 216 |
collected 3 items |
| 217 |
|
| 218 |
<Dir pythoncollection.rst-200> |
| 219 |
<Dir CWD> |
| 220 |
<Module pythoncollection.py> |
| 221 |
<Function test_function> |
| 222 |
<Class TestClass> |
| 223 |
<Function test_method> |
| 224 |
<Function test_anothermethod> |
| 225 |
|
| 226 |
======================== 3 tests collected in 0.12s ======================== |
| 227 |
|
| 228 |
.. _customizing-test-collection: |
| 229 |
|
| 230 |
Customizing test collection |
| 231 |
--------------------------- |
| 232 |
|
| 233 |
.. regendoc:wipe |
| 234 |
|
| 235 |
You can easily instruct ``pytest`` to discover tests from every Python file: |
| 236 |
|
| 237 |
.. code-block:: ini |
| 238 |
|
| 239 |
# content of pytest.ini |
| 240 |
[pytest] |
| 241 |
python_files = *.py |
| 242 |
|
| 243 |
However, many projects will have a ``setup.py`` which they don't want to be |
| 244 |
imported. Moreover, there may files only importable by a specific python |
| 245 |
version. For such cases you can dynamically define files to be ignored by |
| 246 |
listing them in a ``conftest.py`` file: |
| 247 |
|
| 248 |
.. code-block:: python |
| 249 |
|
| 250 |
# content of conftest.py |
| 251 |
import sys |
| 252 |
|
| 253 |
collect_ignore = ["setup.py"] |
| 254 |
if sys.version_info[0] > 2: |
| 255 |
collect_ignore.append("pkg/module_py2.py") |
| 256 |
|
| 257 |
and then if you have a module file like this: |
| 258 |
|
| 259 |
.. code-block:: python |
| 260 |
|
| 261 |
# content of pkg/module_py2.py |
| 262 |
def test_only_on_python2(): |
| 263 |
try: |
| 264 |
assert 0 |
| 265 |
except Exception, e: |
| 266 |
pass |
| 267 |
|
| 268 |
and a ``setup.py`` dummy file like this: |
| 269 |
|
| 270 |
.. code-block:: python |
| 271 |
|
| 272 |
# content of setup.py |
| 273 |
0 / 0 # will raise exception if imported |
| 274 |
|
| 275 |
If you run with a Python 2 interpreter then you will find the one test and will |
| 276 |
leave out the ``setup.py`` file: |
| 277 |
|
| 278 |
.. code-block:: pytest |
| 279 |
|
| 280 |
#$ pytest --collect-only |
| 281 |
====== test session starts ====== |
| 282 |
platform linux2 -- Python 2.7.10, pytest-2.9.1, py-1.4.31, pluggy-0.3.1 |
| 283 |
rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini |
| 284 |
collected 1 items |
| 285 |
<Module 'pkg/module_py2.py'> |
| 286 |
<Function 'test_only_on_python2'> |
| 287 |
|
| 288 |
====== 1 tests found in 0.04 seconds ====== |
| 289 |
|
| 290 |
If you run with a Python 3 interpreter both the one test and the ``setup.py`` |
| 291 |
file will be left out: |
| 292 |
|
| 293 |
.. code-block:: pytest |
| 294 |
|
| 295 |
$ pytest --collect-only |
| 296 |
=========================== test session starts ============================ |
| 297 |
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y |
| 298 |
rootdir: /home/sweet/project |
| 299 |
configfile: pytest.ini |
| 300 |
collected 0 items |
| 301 |
|
| 302 |
======================= no tests collected in 0.12s ======================== |
| 303 |
|
| 304 |
It's also possible to ignore files based on Unix shell-style wildcards by adding |
| 305 |
patterns to :globalvar:`collect_ignore_glob`. |
| 306 |
|
| 307 |
The following example ``conftest.py`` ignores the file ``setup.py`` and in |
| 308 |
addition all files that end with ``*_py2.py`` when executed with a Python 3 |
| 309 |
interpreter: |
| 310 |
|
| 311 |
.. code-block:: python |
| 312 |
|
| 313 |
# content of conftest.py |
| 314 |
import sys |
| 315 |
|
| 316 |
collect_ignore = ["setup.py"] |
| 317 |
if sys.version_info[0] > 2: |
| 318 |
collect_ignore_glob = ["*_py2.py"] |
| 319 |
|
| 320 |
Since Pytest 2.6, users can prevent pytest from discovering classes that start |
| 321 |
with ``Test`` by setting a boolean ``__test__`` attribute to ``False``. |
| 322 |
|
| 323 |
.. code-block:: python |
| 324 |
|
| 325 |
# Will not be discovered as a test |
| 326 |
class TestClass: |
| 327 |
__test__ = False |