Skip to main content

rsttst makes your reStructuredText testable

Project description

https://travis-ci.org/willemt/rsttst.png https://pypip.in/version/rsttst/badge.svg https://pypip.in/download/rsttst/badge.svg

rsttst makes your reStructuredText documentation testable.

In fact, this README file is testable and is used to test rsttst.

Below is an example:

2 + 2 = 4

The title “2 + 2 = 4” becomes the test name after being converted to a Python friendly identifier (ie. 2_plus_2_equals_4).

The bash code in the below code block will be run…

echo '2 + 2' | bc

…and the resulting stdout will be compared to the following code block:

4

The test fails if stdout doesn’t match the block above.

Dotted notation

Sometimes you want to be flexible with the output you accept.

You can use “.” and the “:class: dotted” rst directive option to support this.

echo Date: $(date)
echo '\ok'

The below code block uses the “:class: dotted” option.

Date: ............................
\ok

Three dots match in a similiar way to what you’d expect for a regex pattern of “.+” to work:

echo '<NZ>'
echo $(date "+DATE: %Y-%m-%d%nTIME: %H:%M:%S")

The below code block uses the “:class: dotted” option.

<...>
DATE: ... TIME: ...

Generating tests

Under the hood rsttst generates Python code which is executable with py.test. Here’s how we generate the Python test code:

rsttst README.rst
cat test_readme.py | head -n 28

The resulting test code looks like the following:

# -*- coding: utf-8 -*-
from rsttst.core import run, Dotted

def test_2_plus_2_equals_4():
    output = run(u"""echo '2 + 2' | bc""")
    assert output == u"""4"""

def test_dotted_notation():
    output = run(u"""echo Date: $(date)
echo '\\ok'""")
    expected = Dotted(u"""Date: ............................
\\ok""")
    cmp(output, expected)
    expected = u"{0}".format(expected)
    assert output == expected

def test_dotted_notation__2():
    output = run(u"""echo '<NZ>'
echo $(date "+DATE: %Y-%m-%d%nTIME: %H:%M:%S")""")
    expected = Dotted(u"""<...>
DATE: ... TIME: ...""")
    cmp(output, expected)
    expected = u"{0}".format(expected)
    assert output == expected

def test_generating_tests():
    output = run(u"""rsttst README.rst
cat test_readme.py | head -n 28""")

Windows new lines

^M characters are automatically removed.

printf 'supports\012\015windows new lines'
supports
windows new lines

Running the tests

You could probably use another test runner, but pytest works quite well:

py.test -k 'not test_running_the_tests' | grep -v seconds

Note: we had to exclude ‘test_running_the_tests’, otherwise it’s turtles all the way down.

============================= test session starts ==============================
platform ...
collected 6 items

test_readme.py .....

============= 1 tests deselected by '-knot test_running_the_tests' =============

Functionality

Right now rsttst only supports bash testing.

FAQ

Why does pytest throw an “IndexError: list index out of range” exception for my JSON tests?

Please upgrade to the latest version of pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rsttst-0.1.5.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rsttst-0.1.5-py2.py3-none-any.whl (10.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file rsttst-0.1.5.tar.gz.

File metadata

  • Download URL: rsttst-0.1.5.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for rsttst-0.1.5.tar.gz
Algorithm Hash digest
SHA256 812d6b12b60e339443ba1950070539173c5187ff12a05d40d08ee4d3d99bf9fe
MD5 03b450c172203f39475f4ebeb5cdba70
BLAKE2b-256 49cdc34df487fe78d49c81bf90502a40eedb3292f709a841a6188271887c87fa

See more details on using hashes here.

File details

Details for the file rsttst-0.1.5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for rsttst-0.1.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 94c540c1e2a6159a443c055f8935c26dedb17813a6bf785baf1c2c9222e28de1
MD5 53ab666e49ad6ff7a7b9c6f621c2b35b
BLAKE2b-256 3fdcfaff4ad6493194de33ae93e93dbc159d3e4fe6b9acc8f823034b459759dc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page