【问题标题】:python: doctest my github-markdown files?python: doctest 我的 github-markdown 文件?
【发布时间】:2014-06-08 15:02:35
【问题描述】:

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

README.md:

# WELCOME!

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

For example:

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

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

【问题讨论】:

    标签: python markdown doctest github-flavored-markdown


    【解决方案1】:

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

    python -m doctest -v README.md
    

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

    【讨论】:

    • 更新代码很容易使文档示例出错。当这种情况发生时,测试套件应该/会告诉我们。
    • 完全确定胜于相当确定。
    • @bukzor 嘿,我刚刚收到有关此答案的通知。我不知道为什么我最初写它时会如此刻薄。对于那个很抱歉。我刚刚更新了我的答案,以(希望)更有帮助/提供更多信息。
    【解决方案2】:

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

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

    mkcodes --github --output tests/docs/test_{name}.py docs
    py.test tests
    pyflakes tests
    pep8 tests
    

    【讨论】:

      【解决方案3】:

      刚刚发现了这个phmdoctest 包,它似乎与常见的python 高亮markdown 配合得很好:

      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
      

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

      【讨论】:

        猜你喜欢
        • 2012-08-10
        • 2019-10-10
        • 1970-01-01
        • 2015-12-02
        • 2011-11-30
        • 2016-07-12
        • 1970-01-01
        • 2013-01-01
        • 2017-06-17
        相关资源
        最近更新 更多