Move testing docs to myst.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Edward G · committed by Edward G · parent 17040b9 · Browse files · 8 files·+348 added−263 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+348 added−263 removed
- other:+348 added−263 removed
Provenance
- Commit
e5abf20- Tree
27a385f- Author
Edward G <edward.g2013@gmail.com>- Committer
Edward G <edward.g2013@gmail.com>- Parent
17040b9
8 files changed+348 added−263 removed
modified:docs/conf.py+1 added
16 unmodified lines17 "sphinx.ext.intersphinx",18 "sphinx_tabs.tabs",19 "sphinxcontrib.log_cabinet",20 "pallets_sphinx_themes",21+ "myst_parser",22 ]23 autodoc_member_order = "bysource"24 autodoc_typehints = "description"25 autodoc_preserve_defaults = True31 unmodified linesmodified:docs/index.rst+1 added-1 removed
79 unmodified lines80 documentation81 prompts82 complex83 advanced84- testing84+ testing <testing.md>85 utils86 shell-completion87 exceptions88 unicode-support22 unmodified linesadded:docs/testing.md+198 added
1+# Testing Click Applications2+3+```{eval-rst}4+.. currentmodule:: click.testing5+```6+7+Click provides the {ref}`click.testing <testing>` module to help you invoke command line applications and check their behavior.8+9+These tools should only be used for testing since they change10+the entire interpreter state for simplicity. They are not thread-safe!11+12+The examples use [pytest](https://docs.pytest.org/en/stable/) style tests.13+14+```{contents}15+:depth: 116+:local: true17+```18+19+## Basic Example20+21+The key pieces are:22+ - {class}`CliRunner` - used to invoke commands as command line scripts.23+ - {class}`Result` - returned from {meth}`CliRunner.invoke`. Captures output data, exit code, optional exception, and captures the output as bytes and binary data.24+25+```{code-block} python26+:caption: hello.py27+28+import click29+30+@click.command()31+@click.argument('name')32+def hello(name):33+ click.echo(f'Hello {name}!')34+```35+36+```{code-block} python37+:caption: test_hello.py38+39+from click.testing import CliRunner40+from hello import hello41+42+def test_hello_world():43+ runner = CliRunner()44+ result = runner.invoke(hello, ['Peter'])45+ assert result.exit_code == 046+ assert result.output == 'Hello Peter!\n'47+```48+49+## Subcommands50+51+A subcommand name must be specified in the `args` parameter {meth}`CliRunner.invoke`:52+53+```{code-block} python54+:caption: sync.py55+56+import click57+58+@click.group()59+@click.option('--debug/--no-debug', default=False)60+def cli(debug):61+ click.echo(f"Debug mode is {'on' if debug else 'off'}")62+63+@cli.command()64+def sync():65+ click.echo('Syncing')66+```67+68+```{code-block} python69+:caption: test_sync.py70+71+from click.testing import CliRunner72+from sync import cli73+74+def test_sync():75+ runner = CliRunner()76+ result = runner.invoke(cli, ['--debug', 'sync'])77+ assert result.exit_code == 078+ assert 'Debug mode is on' in result.output79+ assert 'Syncing' in result.output80+```81+82+## Context Settings83+84+Additional keyword arguments passed to {meth}`CliRunner.invoke` will be used to construct the initial {class}`Context object <click.Context>`.85+For example, setting a fixed terminal width equal to 60:86+87+```{code-block} python88+:caption: sync.py89+90+import click91+92+@click.group()93+def cli():94+ pass95+96+@cli.command()97+def sync():98+ click.echo('Syncing')99+```100+101+```{code-block} python102+:caption: test_sync.py103+104+from click.testing import CliRunner105+from sync import cli106+107+def test_sync():108+ runner = CliRunner()109+ result = runner.invoke(cli, ['sync'], terminal_width=60)110+ assert result.exit_code == 0111+ assert 'Debug mode is on' in result.output112+ assert 'Syncing' in result.output113+```114+115+## File System Isolation116+117+The {meth}`CliRunner.isolated_filesystem` context manager sets the current working directory to a new, empty folder.118+119+```{code-block} python120+:caption: cat.py121+122+import click123+124+@click.command()125+@click.argument('f', type=click.File())126+def cat(f):127+ click.echo(f.read())128+```129+130+```{code-block} python131+:caption: test_cat.py132+133+from click.testing import CliRunner134+from cat import cat135+136+def test_cat():137+ runner = CliRunner()138+ with runner.isolated_filesystem():139+ with open('hello.txt', 'w') as f:140+ f.write('Hello World!')141+142+ result = runner.invoke(cat, ['hello.txt'])143+ assert result.exit_code == 0144+ assert result.output == 'Hello World!\n'145+```146+147+Pass in a path to control where the temporary directory is created.148+In this case, the directory will not be removed by Click. Its useful149+to integrate with a framework like Pytest that manages temporary files.150+151+```{code-block} python152+:caption: test_cat.py153+154+from click.testing import CliRunner155+from cat import cat156+157+def test_cat_with_path_specified():158+ runner = CliRunner()159+ with runner.isolated_filesystem('~/test_folder'):160+ with open('hello.txt', 'w') as f:161+ f.write('Hello World!')162+163+ result = runner.invoke(cat, ['hello.txt'])164+ assert result.exit_code == 0165+ assert result.output == 'Hello World!\n'166+```167+168+## Input Streams169+170+The test wrapper can provide input data for the input stream (stdin). This is very useful for testing prompts, for instance:171+172+```{code-block} python173+:caption: prompt.py174+175+import click176+177+@click.command()178+@click.option('--foo', prompt=True)179+def prompt(foo):180+ click.echo(f"foo={foo}")181+```182+183+```{code-block} python184+:caption: test_prompt.py185+186+from click.testing import CliRunner187+from prompt import prompt188+189+def test_prompts():190+ runner = CliRunner()191+ result = runner.invoke(prompt, input='wau wau\n')192+ assert not result.exception193+ assert result.output == 'Foo: wau wau\nfoo=wau wau\n'194+```195+196+Prompts will be emulated so they write the input data to197+the output stream as well. If hidden input is expected then this198+does not happen.deleted:docs/testing.rst-190 removed
1-Testing Click Applications2-==========================3-4-.. currentmodule:: click.testing5-6-Click provides the :ref:`click.testing <testing>` module to help you invoke command line applications and check their behavior.7-8-These tools should only be used for testing since they change9-the entire interpreter state for simplicity. They are not thread-safe!10-11-The examples use `pytest <https://docs.pytest.org/en/stable/>`_ style tests.12-13-.. contents::14- :depth: 115- :local:16-17-Basic Example18--------------19-20-The key pieces are:21- * :class:`CliRunner` - used to invoke commands as command line scripts.22- * :class:`Result` - returned from :meth:`CliRunner.invoke`. Captures output data, exit code, optional exception, and captures the output as bytes and binary data.23-24-.. code-block:: python25- :caption: hello.py26-27- import click28-29- @click.command()30- @click.argument('name')31- def hello(name):32- click.echo(f'Hello {name}!')33-34-.. code-block:: python35- :caption: test_hello.py36-37- from click.testing import CliRunner38- from hello import hello39-40- def test_hello_world():41- runner = CliRunner()42- result = runner.invoke(hello, ['Peter'])43- assert result.exit_code == 044- assert result.output == 'Hello Peter!\n'45-46-Subcommands47-------------48-49-A subcommand name must be specified in the `args` parameter :meth:`CliRunner.invoke`:50-51-.. code-block:: python52- :caption: sync.py53-54- import click55-56- @click.group()57- @click.option('--debug/--no-debug', default=False)58- def cli(debug):59- click.echo(f"Debug mode is {'on' if debug else 'off'}")60-61- @cli.command()62- def sync():63- click.echo('Syncing')64-65-.. code-block:: python66- :caption: test_sync.py67-68- from click.testing import CliRunner69- from sync import cli70-71- def test_sync():72- runner = CliRunner()73- result = runner.invoke(cli, ['--debug', 'sync'])74- assert result.exit_code == 075- assert 'Debug mode is on' in result.output76- assert 'Syncing' in result.output77-78-Context Settings79------------------80-Additional keyword arguments passed to :meth:`CliRunner.invoke` will be used to construct the initial :class:`Context object <click.Context>`.81-For example, setting a fixed terminal width equal to 60:82-83-.. code-block:: python84- :caption: sync.py85-86- import click87-88- @click.group()89- def cli():90- pass91-92- @cli.command()93- def sync():94- click.echo('Syncing')95-96-.. code-block:: python97- :caption: test_sync.py98-99- from click.testing import CliRunner100- from sync import cli101-102- def test_sync():103- runner = CliRunner()104- result = runner.invoke(cli, ['sync'], terminal_width=60)105- assert result.exit_code == 0106- assert 'Debug mode is on' in result.output107- assert 'Syncing' in result.output108-109-File System Isolation110----------------------111-112-The :meth:`CliRunner.isolated_filesystem` context manager sets the current working directory to a new, empty folder.113-114-.. code-block:: python115- :caption: cat.py116-117- import click118-119- @click.command()120- @click.argument('f', type=click.File())121- def cat(f):122- click.echo(f.read())123-124-.. code-block:: python125- :caption: test_cat.py126-127- from click.testing import CliRunner128- from cat import cat129-130- def test_cat():131- runner = CliRunner()132- with runner.isolated_filesystem():133- with open('hello.txt', 'w') as f:134- f.write('Hello World!')135-136- result = runner.invoke(cat, ['hello.txt'])137- assert result.exit_code == 0138- assert result.output == 'Hello World!\n'139-140-Pass in a path to control where the temporary directory is created.141-In this case, the directory will not be removed by Click. Its useful142-to integrate with a framework like Pytest that manages temporary files.143-144-.. code-block:: python145- :caption: test_cat.py146-147- from click.testing import CliRunner148- from cat import cat149-150- def test_cat_with_path_specified():151- runner = CliRunner()152- with runner.isolated_filesystem('~/test_folder'):153- with open('hello.txt', 'w') as f:154- f.write('Hello World!')155-156- result = runner.invoke(cat, ['hello.txt'])157- assert result.exit_code == 0158- assert result.output == 'Hello World!\n'159-160-161-Input Streams162--------------163-164-The test wrapper can provide input data for the input stream (stdin). This is very useful for testing prompts, for instance:165-166-.. code-block:: python167- :caption: prompt.py168-169- import click170-171- @click.command()172- @click.option('--foo', prompt=True)173- def prompt(foo):174- click.echo(f"foo={foo}")175-176-.. code-block:: python177- :caption: test_prompt.py178-179- from click.testing import CliRunner180- from prompt import prompt181-182- def test_prompts():183- runner = CliRunner()184- result = runner.invoke(prompt, input='wau wau\n')185- assert not result.exception186- assert result.output == 'Foo: wau wau\nfoo=wau wau\n'187-188-Prompts will be emulated so they write the input data to189-the output stream as well. If hidden input is expected then this190-does not happen.modified:requirements/dev.in+1 added
1 unmodified line2 -r tests.txt3 -r typing.txt4 pre-commit5 tox6+sphinx-autobuildmodified:requirements/dev.txt+123 added-62 removed
1-#2-# This file is autogenerated by pip-compile with Python 3.133-# by the following command:4-#5-# pip-compile dev.in6-#1+# This file was autogenerated by uv via the following command:2+# uv pip compile -o dev.txt dev.in3 alabaster==1.0.08- # via sphinx4+ # via5+ # -r docs.txt6+ # sphinx7+anyio==4.8.08+ # via9+ # starlette10+ # watchfiles11 babel==2.16.010- # via sphinx11-build==1.2.2.post112- # via pip-tools13-cachetools==5.5.012+ # via13+ # -r docs.txt14+ # sphinx15+cachetools==5.5.116 # via tox17 certifi==2024.8.3016- # via requests18+ # via19+ # -r docs.txt20+ # requests21 cfgv==3.4.022 # via pre-commit23 chardet==5.2.024 # via tox25 charset-normalizer==3.4.022- # via requests23-click==8.1.726 # via25- # pip-compile-multi26- # pip-tools27+ # -r docs.txt28+ # requests29+click==8.1.830+ # via uvicorn31 colorama==0.4.628- # via tox32+ # via33+ # click34+ # pytest35+ # sphinx36+ # sphinx-autobuild37+ # tox38 distlib==0.3.939 # via virtualenv40 docutils==0.21.241 # via42+ # -r docs.txt43 # sphinx44 # sphinx-tabs35-filelock==3.16.145+exceptiongroup==1.2.246 # via47+ # anyio48+ # pytest49+filelock==3.17.050+ # via51 # tox52 # virtualenv53+h11==0.14.054+ # via uvicorn39-identify==2.6.155+identify==2.6.756 # via pre-commit57 idna==3.1042- # via requests58+ # via59+ # -r docs.txt60+ # anyio61+ # requests62 imagesize==1.4.144- # via sphinx63+ # via64+ # -r docs.txt65+ # sphinx66 iniconfig==2.0.046- # via pytest67+ # via68+ # -r tests.txt69+ # pytest70 jinja2==3.1.448- # via sphinx71+ # via72+ # -r docs.txt73+ # sphinx74 markupsafe==3.0.250- # via jinja275+ # via76+ # -r docs.txt77+ # jinja278 mypy==1.13.052- # via -r typing.in79+ # via -r typing.txt80 mypy-extensions==1.0.054- # via mypy81+ # via82+ # -r typing.txt83+ # mypy84 nodeenv==1.9.185 # via86+ # -r typing.txt87 # pre-commit88 # pyright89 packaging==24.190 # via61- # build91+ # -r docs.txt92+ # -r tests.txt93 # pallets-sphinx-themes94 # pyproject-api95 # pytest96 # sphinx97 # tox98 pallets-sphinx-themes==2.3.068- # via -r docs.in69-pip-compile-multi==2.6.470- # via -r dev.in71-pip-tools==7.4.172- # via pip-compile-multi99+ # via -r docs.txt100 platformdirs==4.3.6101 # via102 # tox103 # virtualenv104 pluggy==1.5.0105 # via106+ # -r tests.txt107 # pytest108 # tox81-pre-commit==4.0.1109+pre-commit==4.1.0110 # via -r dev.in111 pygments==2.18.0112 # via113+ # -r docs.txt114 # sphinx115 # sphinx-tabs116 pyproject-api==1.8.0117 # via tox89-pyproject-hooks==1.2.090- # via91- # build92- # pip-tools118 pyright==1.1.38694- # via -r typing.in119+ # via -r typing.txt120 pytest==8.3.396- # via -r tests.in121+ # via -r tests.txt122 pyyaml==6.0.2123 # via pre-commit124 requests==2.32.3100- # via sphinx125+ # via126+ # -r docs.txt127+ # sphinx128+sniffio==1.3.1129+ # via anyio130 snowballstemmer==2.2.0102- # via sphinx131+ # via132+ # -r docs.txt133+ # sphinx134 sphinx==8.1.3135 # via105- # -r docs.in136+ # -r docs.txt137 # pallets-sphinx-themes138+ # sphinx-autobuild139 # sphinx-issues140 # sphinx-notfound-page141 # sphinx-tabs142 # sphinxcontrib-log-cabinet143+sphinx-autobuild==2024.10.3144+ # via -r dev.in145 sphinx-issues==5.0.0112- # via -r docs.in146+ # via -r docs.txt147 sphinx-notfound-page==1.0.4148+ # via149+ # -r docs.txt114- # via pallets-sphinx-themes150+ # pallets-sphinx-themes151 sphinx-tabs==3.4.7116- # via -r docs.in152+ # via -r docs.txt153 sphinxcontrib-applehelp==2.0.0118- # via sphinx154+ # via155+ # -r docs.txt156+ # sphinx157 sphinxcontrib-devhelp==2.0.0120- # via sphinx158+ # via159+ # -r docs.txt160+ # sphinx161 sphinxcontrib-htmlhelp==2.1.0122- # via sphinx162+ # via163+ # -r docs.txt164+ # sphinx165 sphinxcontrib-jsmath==1.0.1124- # via sphinx166+ # via167+ # -r docs.txt168+ # sphinx169 sphinxcontrib-log-cabinet==1.0.1126- # via -r docs.in170+ # via -r docs.txt171 sphinxcontrib-qthelp==2.0.0128- # via sphinx172+ # via173+ # -r docs.txt174+ # sphinx175 sphinxcontrib-serializinghtml==2.0.0130- # via sphinx131-toposort==1.10132- # via pip-compile-multi176+ # via177+ # -r docs.txt178+ # sphinx179+starlette==0.45.3180+ # via sphinx-autobuild181+tomli==2.2.1182+ # via183+ # mypy184+ # pyproject-api185+ # pytest186+ # sphinx187+ # tox188 tox==4.23.2189 # via -r dev.in190 typing-extensions==4.12.2191 # via192+ # -r typing.txt193+ # anyio194 # mypy195 # pyright196+ # tox197+ # uvicorn198 urllib3==2.2.3140- # via requests141-virtualenv==20.27.0199 # via200+ # -r docs.txt201+ # requests202+uvicorn==0.34.0203+ # via sphinx-autobuild204+virtualenv==20.29.2205+ # via206 # pre-commit207 # tox145-wheel==0.44.0146- # via pip-tools147-148-# The following packages are considered to be unsafe in a requirements file:149-# pip150-# setuptools208+watchfiles==1.0.4209+ # via sphinx-autobuild210+websockets==15.0211+ # via sphinx-autobuildmodified:requirements/docs.in+1 added
1 pallets-sphinx-themes2 sphinx3 sphinxcontrib-log-cabinet4 sphinx-tabs5+myst-parsermodified:requirements/docs.txt+23 added-10 removed
1-#2-# This file is autogenerated by pip-compile with Python 3.133-# by the following command:4-#5-# pip-compile docs.in6-#1+# This file was autogenerated by uv via the following command:2+# uv pip compile -o docs.txt docs.in3 alabaster==1.0.04 # via sphinx5 babel==2.16.06 # via sphinx7 certifi==2024.8.308 # via requests9 charset-normalizer==3.4.010 # via requests11+colorama==0.4.612+ # via sphinx13 docutils==0.21.214 # via15+ # myst-parser16 # sphinx17 # sphinx-tabs18 idna==3.1019 # via requests20 imagesize==1.4.121 # via sphinx22 jinja2==3.1.424- # via sphinx23+ # via24+ # myst-parser25+ # sphinx26+markdown-it-py==3.0.027+ # via28+ # mdit-py-plugins29+ # myst-parser30 markupsafe==3.0.231 # via jinja232+mdit-py-plugins==0.4.233+ # via myst-parser34+mdurl==0.1.235+ # via markdown-it-py36+myst-parser==4.0.137+ # via -r docs.in38 packaging==24.139 # via40 # pallets-sphinx-themes41 # sphinx2 unmodified lines44 pygments==2.18.045 # via46 # sphinx47 # sphinx-tabs48+pyyaml==6.0.249+ # via myst-parser50 requests==2.32.351 # via sphinx52 snowballstemmer==2.2.053 # via sphinx54 sphinx==8.1.355 # via56 # -r docs.in57+ # myst-parser58 # pallets-sphinx-themes45- # sphinx-issues59 # sphinx-notfound-page60 # sphinx-tabs61 # sphinxcontrib-log-cabinet49-sphinx-issues==5.0.050- # via -r docs.in62 sphinx-notfound-page==1.0.463 # via pallets-sphinx-themes64 sphinx-tabs==3.4.765 # via -r docs.in10 unmodified lines76 sphinxcontrib-qthelp==2.0.077 # via sphinx78 sphinxcontrib-serializinghtml==2.0.079 # via sphinx80+tomli==2.2.181+ # via sphinx82 urllib3==2.2.383 # via requests