|
| 1 | +.. _`custom directory collectors`: |
| 2 | + |
| 3 | +Using a custom directory collector |
| 4 | +==================================================== |
| 5 | + |
| 6 | +By default, pytest collects directories using :class:`pytest.Package`, for directories with ``__init__.py`` files, |
| 7 | +and :class:`pytest.Dir` for other directories. |
| 8 | +If you want to customize how a directory is collected, you can write your own :class:`pytest.Directory` collector, |
| 9 | +and use :hook:`pytest_collect_directory` to hook it up. |
| 10 | + |
| 11 | +.. _`directory manifest plugin`: |
| 12 | + |
| 13 | +A basic example for a directory manifest file |
| 14 | +-------------------------------------------------------------- |
| 15 | + |
| 16 | +Suppose you want to customize how collection is done on a per-directory basis. |
| 17 | +Here is an example ``conftest.py`` plugin. |
| 18 | +This plugin allows directories to contain a ``manifest.json`` file, |
| 19 | +which defines how the collection should be done for the directory. |
| 20 | +In this example, only a simple list of files is supported, |
| 21 | +however you can imagine adding other keys, such as exclusions and globs. |
| 22 | + |
| 23 | +.. include:: customdirectory/conftest.py |
| 24 | + :literal: |
| 25 | + |
| 26 | +You can create a ``manifest.json`` file and some test files: |
| 27 | + |
| 28 | +.. include:: customdirectory/tests/manifest.json |
| 29 | + :literal: |
| 30 | + |
| 31 | +.. include:: customdirectory/tests/test_first.py |
| 32 | + :literal: |
| 33 | + |
| 34 | +.. include:: customdirectory/tests/test_second.py |
| 35 | + :literal: |
| 36 | + |
| 37 | +.. include:: customdirectory/tests/test_third.py |
| 38 | + :literal: |
| 39 | + |
| 40 | +An you can now execute the test specification: |
| 41 | + |
| 42 | +.. code-block:: pytest |
| 43 | +
|
| 44 | + customdirectory $ pytest |
| 45 | + =========================== test session starts ============================ |
| 46 | + platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y |
| 47 | + rootdir: /home/sweet/project/customdirectory |
| 48 | + configfile: pytest.ini |
| 49 | + collected 2 items |
| 50 | +
|
| 51 | + tests/test_first.py . [ 50%] |
| 52 | + tests/test_second.py . [100%] |
| 53 | +
|
| 54 | + ============================ 2 passed in 0.12s ============================= |
| 55 | +
|
| 56 | +.. regendoc:wipe |
| 57 | +
|
| 58 | +Notice how ``test_three.py`` was not executed, because it is not listed in the manifest. |
| 59 | + |
| 60 | +You can verify that your custom collector appears in the collection tree: |
| 61 | + |
| 62 | +.. code-block:: pytest |
| 63 | +
|
| 64 | + customdirectory $ pytest --collect-only |
| 65 | + =========================== test session starts ============================ |
| 66 | + platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y |
| 67 | + rootdir: /home/sweet/project/customdirectory |
| 68 | + configfile: pytest.ini |
| 69 | + collected 2 items |
| 70 | +
|
| 71 | + <Dir customdirectory> |
| 72 | + <ManifestDirectory tests> |
| 73 | + <Module test_first.py> |
| 74 | + <Function test_1> |
| 75 | + <Module test_second.py> |
| 76 | + <Function test_2> |
| 77 | +
|
| 78 | + ======================== 2 tests collected in 0.12s ======================== |
0 commit comments