Skip to content

Commit

Permalink
Merge pull request #221 from lpsinger/markdown-fenced-code-blocks
Browse files Browse the repository at this point in the history
Add documentation and unit tests for Markdown code blocks
  • Loading branch information
bsipocz authored Sep 6, 2023
2 parents 45fc0e9 + 4f66d93 commit d7c1f02
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ plugin and are set in ``doctest_optionflags`` in ``setup.cfg``. By default,
doctest settings, see the `doctest documentation
<https://docs.python.org/3/library/doctest.html#option-flags>`_.

Running Tests in Markdown Files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To run doctests in Markdown files, invoke pytest with the command line options
``--doctest-plus --doctest-glob '*.md'``.

If you write doctests inside `GitHub-style triple backtick fenced code blocks
<https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks#fenced-code-blocks>`_,
then in order for pytest-doctest to find and run them you need to include an
extra trailing newline inside your code blocks, like this::

```pycon
>>> 1 + 2
2

```

Doctest Directives
~~~~~~~~~~~~~~~~~~

Expand Down
26 changes: 26 additions & 0 deletions tests/test_doctestplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,32 @@ def test_doctest_glob(testdir):
).assertoutcome(passed=1)


@pytest.mark.xfail(reason='known issue, fenced code blocks require an extra trailing newline')
def test_markdown_fenced_code(testdir):
testdir.makefile('.md', foo="""\
```
>>> 1 + 1
2
```
""")
testdir.inline_run(
'--doctest-plus', '--doctest-glob', '*.md'
).assertoutcome(passed=1)


def test_markdown_fenced_code_with_extra_newline(testdir):
testdir.makefile('.md', foo="""\
```
>>> 1 + 1
2
```
""")
testdir.inline_run(
'--doctest-plus', '--doctest-glob', '*.md'
).assertoutcome(passed=1)


def test_text_file_comments(testdir):
testdir.makefile(
'.md',
Expand Down

0 comments on commit d7c1f02

Please sign in to comment.