|
| 1 | +Command line options |
| 2 | +==================== |
| 3 | + |
| 4 | +In :ref:`dynamic-fixture-scope`, we have already seen how the fixture scope can |
| 5 | +be changed using a command line option. Now let’s take a closer look at the |
| 6 | +command line options. |
| 7 | + |
| 8 | +Passing different values to a test function |
| 9 | +------------------------------------------- |
| 10 | + |
| 11 | +Suppose you want to write a test that depends on a command line option. You can |
| 12 | +achieve this using the following pattern: |
| 13 | + |
| 14 | +.. code-block:: python |
| 15 | + :caption: test_example.py |
| 16 | +
|
| 17 | + def test_db(items_db, db_path, cmdopt): |
| 18 | + if cmdopt == "json": |
| 19 | + print("Save as JSON file") |
| 20 | + elif cmdopt == "sqlite": |
| 21 | + print("Save in a SQLite database") |
| 22 | + assert items_db.path() == db_path |
| 23 | +
|
| 24 | +For this to work, the command line option must be added and ``cmdopt`` must be |
| 25 | +provided via a fixture function: |
| 26 | + |
| 27 | +.. code-block:: python |
| 28 | + :caption: conftest.py |
| 29 | +
|
| 30 | + import pytest |
| 31 | +
|
| 32 | +
|
| 33 | + def pytest_addoption(parser): |
| 34 | + parser.addoption( |
| 35 | + "--cmdopt", |
| 36 | + action="store", |
| 37 | + default="json", |
| 38 | + help="Store data as JSON file or in a SQLite database", |
| 39 | + ) |
| 40 | +
|
| 41 | +
|
| 42 | + @pytest.fixture |
| 43 | + def cmdopt(request): |
| 44 | + return request.config.getoption("--cmdopt") |
| 45 | +
|
| 46 | +You can then call up your tests, for example, with: |
| 47 | + |
| 48 | +.. code-block:: console |
| 49 | +
|
| 50 | + $ pytest --sqlite |
| 51 | +
|
| 52 | +In addition, you can add a simple validation of the input by listing the |
| 53 | +options: |
| 54 | + |
| 55 | +.. code-block:: python |
| 56 | + :caption: conftest.py |
| 57 | + :emphasize-lines: 7 |
| 58 | +
|
| 59 | + def pytest_addoption(parser): |
| 60 | + parser.addoption( |
| 61 | + "--cmdopt", |
| 62 | + action="store", |
| 63 | + default="json", |
| 64 | + help="Store data as JSON file or in a SQLite database", |
| 65 | + choices=("json", "sqlite"), |
| 66 | + ) |
| 67 | +
|
| 68 | +This is how we receive feedback on an incorrect argument: |
| 69 | + |
| 70 | +.. code-block:: console |
| 71 | +
|
| 72 | + $ pytest --postgresql |
| 73 | + ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...] |
| 74 | + pytest: error: argument --cmdopt: invalid choice: 'postgresql' (choose from json, sqlite) |
| 75 | +
|
| 76 | +If you want to provide more detailed error messages, you can use the ``type`` |
| 77 | +parameter and raise ``pytest.UsageError``: |
| 78 | + |
| 79 | +.. code-block:: python |
| 80 | + :caption: conftest.py |
| 81 | + :emphasize-lines: -6, 15 |
| 82 | +
|
| 83 | + def type_checker(value): |
| 84 | + msg = "cmdopt must specify json or sqlite" |
| 85 | + if not value.startswith("json" or "sqlite"): |
| 86 | + raise pytest.UsageError(msg) |
| 87 | +
|
| 88 | + return value |
| 89 | +
|
| 90 | +
|
| 91 | + def pytest_addoption(parser): |
| 92 | + parser.addoption( |
| 93 | + "--cmdopt", |
| 94 | + action="store", |
| 95 | + default="json", |
| 96 | + help="Store data as JSON file or in a SQLite database", |
| 97 | + type=type_checker, |
| 98 | + ) |
| 99 | +
|
| 100 | +However, command line options often need to be processed outside of the test and |
| 101 | +more complex objects need to be passed. |
| 102 | + |
| 103 | +Adding command line options dynamically |
| 104 | +--------------------------------------- |
| 105 | + |
| 106 | +With :ref:`addopts`, you can add static command line options to your project. |
| 107 | +However, you can also change the command line arguments dynamically before they |
| 108 | +are processed: |
| 109 | + |
| 110 | +.. code-block:: python |
| 111 | + :caption: conftest.py |
| 112 | +
|
| 113 | + import sys |
| 114 | +
|
| 115 | +
|
| 116 | + def pytest_load_initial_conftests(args): |
| 117 | + if "xdist" in sys.modules: |
| 118 | + import multiprocessing |
| 119 | +
|
| 120 | + num = max(multiprocessing.cpu_count() / 2, 1) |
| 121 | + args[:] = ["-n", str(num)] + args |
| 122 | +
|
| 123 | +If you have installed the :ref:`xdist-plugin` plugin, test runs will always be |
| 124 | +performed with a number of subprocesses close to your CPU. |
| 125 | + |
| 126 | +Command line option for skipping tests |
| 127 | +-------------------------------------- |
| 128 | + |
| 129 | +Below, we add a :file:`conftest.py` file with a command line option |
| 130 | +``--runslow`` to control the skipping of tests marked with ``pytest.mark.slow``: |
| 131 | + |
| 132 | +.. code-block:: python |
| 133 | + :caption: conftest.py |
| 134 | +
|
| 135 | + import pytest |
| 136 | +
|
| 137 | +
|
| 138 | + def pytest_addoption(parser): |
| 139 | + parser.addoption( |
| 140 | + "--runslow", action="store_true", default=False, help="run slow tests" |
| 141 | + ) |
| 142 | +
|
| 143 | +
|
| 144 | + def pytest_collection_modifyitems(config, items): |
| 145 | + if config.getoption("--runslow"): |
| 146 | + # If --runslow is specified on the CLI, slow tests are not skipped. |
| 147 | + return |
| 148 | + skip_slow = pytest.mark.skip(reason="need --runslow option to run") |
| 149 | + for item in items: |
| 150 | + if "slow" in item.keywords: |
| 151 | + item.add_marker(skip_slow) |
| 152 | +
|
| 153 | +If we now write a test with the ``@pytest.mark.slow`` decorator, a skipped |
| 154 | +‘slow’ test will be displayed when pytest is called: |
| 155 | + |
| 156 | +.. code-block:: pytest |
| 157 | +
|
| 158 | + $ uv run pytest |
| 159 | + ============================= test session starts ============================== |
| 160 | + ... |
| 161 | + test_example.py s. [100%] |
| 162 | +
|
| 163 | + =========================== short test summary info ============================ |
| 164 | + SKIPPED [1] test_example.py:8: need --runslow option to run |
| 165 | + ========================= 1 passed, 1 skipped in 0.05s ========================= |
| 166 | +
|
| 167 | +Extend test report header |
| 168 | +------------------------- |
| 169 | + |
| 170 | +Additional information can be easily provided in a ``pytest -v`` run: |
| 171 | + |
| 172 | +.. code-block:: python |
| 173 | + :caption: conftest.py |
| 174 | +
|
| 175 | + import sys |
| 176 | +
|
| 177 | +
|
| 178 | + def pytest_report_header(config): |
| 179 | + gil = sys._is_gil_enabled() |
| 180 | + return f"Is GIL enabled? {gil}" |
| 181 | +
|
| 182 | +
|
| 183 | +.. code-block:: pytest |
| 184 | + :emphasize-lines: 5 |
| 185 | +
|
| 186 | + $ uv run pytest -v |
| 187 | + ============================= test session starts ============================== |
| 188 | + platform darwin -- Python 3.14.0b4, pytest-8.4.1, pluggy-1.6.0 |
| 189 | + cachedir: .pytest_cache |
| 190 | + Is GIL enabled? False |
| 191 | + rootdir: /Users/veit/sandbox/items |
| 192 | + configfile: pyproject.toml |
| 193 | + plugins: anyio-4.9.0, Faker-37.4.0, cov-6.2.1 |
| 194 | + ... |
| 195 | + ============================== 2 passed in 0.04s =============================== |
| 196 | +
|
| 197 | +Determine test duration |
| 198 | +----------------------- |
| 199 | + |
| 200 | +If you have a large test suite that runs slowly, you will probably want to use |
| 201 | +``-vv --durations`` to find out which tests are the slowest. |
| 202 | + |
| 203 | +.. code-block:: pytest |
| 204 | +
|
| 205 | + $ uv run pytest -vv --durations=3 |
| 206 | + ============================= test session starts ============================== |
| 207 | + ... |
| 208 | + ============================= slowest 3 durations ============================== |
| 209 | + 0.02s setup tests/api/test_add.py::test_add_from_empty |
| 210 | + 0.00s call tests/cli/test_help.py::test_help[add] |
| 211 | + 0.00s call tests/cli/test_help.py::test_help[update] |
| 212 | + ============================== 83 passed in 0.17s ============================== |
0 commit comments