🧪 shtest¶
shtest tests shell commands in your Sphinx documentation.
The shtest directive supports the usual doctest syntax. The following example renders as shown below during a normal documentation build and runs the test when executed using the shtest builder (see Installation and Usage for details).
.. shtest::
# Obligatory hello world example.
$ echo hello world
hello world
# Obligatory hello world example.
$ echo hello world
hello world
The directive offers the following options:
:returncode: [integer]specifies the expected return code (defaults to 0).adding the
:stderr:flag compares results with thestderrrather thanstdoutstream.:cwd: [relative path]specifies the working directory relative to the source of the document (defaults to the directory containing the source document).:tempdir:executes the test in a temporary directory.
Installation and Usage¶
Run
pip install sphinxcontrib-shtestto install the package.Add
"sphinxcontrib.shtest"to yourextensionslist inconf.pyto enable the extension (see here for details).Execute
sphinx-build -b shtest /path/to/source/directory /path/to/output/directory. The-b shtestflag executes the shell tests; run without the-b shtestflag to build your documentation as usual.
Examples¶
.. shtest::
:stderr:
# Read from stderr instead of stdout.
$ echo message on stderr >&2
message on stderr
# Read from stderr instead of stdout.
$ echo message on stderr >&2
message on stderr
.. shtest::
:returncode: 1
# Use a non-zero expected return code.
$ false
# Use a non-zero expected return code.
$ false
.. shtest::
# Run multiple tests in one directive.
$ echo hello
hello
$ echo world
world
# Run multiple tests in one directive.
$ echo hello
hello
$ echo world
world
.. shtest::
:cwd: tests
# Run a test in a particular working directory relative to the document.
$ cat hello.txt
world
# Run a test in a particular working directory relative to the document.
$ cat hello.txt
world
.. shtest::
:tempdir:
# Run a test in a temporary directory.
$ echo hello > world.txt
# Run a test in a temporary directory.
$ echo hello > world.txt
.. sh:: ls -l
$ ls -l
total 60
-rw-r--r-- 1 docs docs 11357 Jul 22 14:46 LICENSE
-rw-r--r-- 1 docs docs 3531 Jul 22 14:46 README.rst
drwxr-xr-x 3 docs docs 4096 Jul 22 14:47 _build
drwxr-xr-x 3 docs docs 4096 Jul 22 14:47 _readthedocs
-rw-r--r-- 1 docs docs 118 Jul 22 14:46 conf.py
lrwxrwxrwx 1 docs docs 10 Jul 22 14:46 index.rst -> README.rst
-rw-r--r-- 1 docs docs 965 Jul 22 14:46 recipe.py
-rw-r--r-- 1 docs docs 70 Jul 22 14:46 requirements.in
-rw-r--r-- 1 docs docs 3083 Jul 22 14:46 requirements.txt
-rw-r--r-- 1 docs docs 31 Jul 22 14:46 setup.cfg
-rw-r--r-- 1 docs docs 534 Jul 22 14:46 setup.py
drwxr-xr-x 3 docs docs 4096 Jul 22 14:46 sphinxcontrib
drwxr-xr-x 2 docs docs 4096 Jul 22 14:47 sphinxcontrib_shtest.egg-info
drwxr-xr-x 3 docs docs 4096 Jul 22 14:46 tests