13

我想从此文件中运行 doctests,但我不清楚要完成它:

README.md

# WELCOME!

This library is helpful and will help you in _many_ ways!

For example:

```
>>> import library
>>> library.helps()
True
```

(旁白:谁能帮我把它突出显示为降价?)

4

3 回答 3

7

您可以使用以下命令在命令行上doctest运行:README

python -m doctest -v README.md

-m参数告诉 Python 将以下模块作为脚本运行。当作为脚本运行时doctest模块doctest.testmod在以下文件上运行函数。最后,-v参数使得doctest以详细模式运行;如果它被关闭,doctest则仅在至少一个测试失败时才产生输出(如果一切都成功则不会产生输出)。

于 2014-04-23T16:47:48.077 回答
4

作为doctest的替代方案,我编写了mkcodes,这是一个从 markdown 文件中提取代码块的脚本,以便可以在单独的文件中对其进行测试。

这是我使用 mkcodes 的实际测试脚本:

mkcodes --github --output tests/docs/test_{name}.py docs
py.test tests
pyflakes tests
pep8 tests
于 2015-12-20T03:20:34.060 回答
0

刚刚发现这个phmdoctest包,它似乎与常见的 python 突出显示降价配合得很好:

Any text here for example...
```python
print(1+2)
```
sample output:
```
3
```

和一个简单的用法:

phmdoctest README.md --outfile tests/test_readme.py
python -m pytest tests -v

在第一行中,您生成一个新的测试文件,然后为整个项目运行标准测试...

于 2021-03-27T23:24:55.640 回答