【问题标题】:why the doctest didn't work in qpython3为什么 doctest 在 qpython3 中不起作用
【发布时间】:2017-07-25 01:44:22
【问题描述】:

我正在尝试在 qpython 中使用 doctest。但是脚本不行,在PC环境下没问题。

这与我在 PC 环境中的脚本只有两行不同: 导入 sl4a 机器人 = sl4a.Android()

在qpython中运行脚本后,我可以看到docctest的日志,但是测试用例似乎没有被测试。我得到的味精是: 11个项目没有测试: ... 11 个项目中的 0 个测试。 0 次通过,0 次失败。 测试通过

我错过了什么重要的事情吗? 感谢您的帮助!

【问题讨论】:

    标签: android doctest qpython qpython3


    【解决方案1】:

    那是因为 qpython 在 (-OO) 上运行带有优化的 python,它删除了文档字符串,所以 doctest 看不到任何东西。 以下技巧为您提供了几乎所有功能。 基本上,它只是用 ast 模块解析源文件以获取文档字符串,然后放入 __test__ 字典中。

    def setupDoctest():
        global __test__
        import ast
        __test__ = {}
        parsed = ast.parse(open(__file__).read(), "doctest")
        doctypes = ast.Module, ast.FunctionDef, ast.ClassDef
        for node in ast.walk(parsed):
            if isinstance(node, doctypes):
                d = ast.get_docstring(node, True)
                if d:
                    __test__[getattr(node, "name", "module")] = d
    

    在调用 doctest.testmod 之前调用它,它会运行文档字符串。

    【讨论】:

      【解决方案2】:

      也许你找到了解决方案,但前几天我在使用 QPython 时也有类似的经历。似乎你不能(还)在 QPython 的控制台中使用 Player 或 Pyjnius。将脚本作为 Kivy 应用程序运行可以导入 Plyer 或 Pyjnius。尝试添加以下行:

      #-*-coding:utf8;-*-
      #qpy:2
      #qpy:kivy
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-31
        • 1970-01-01
        • 1970-01-01
        • 2018-11-12
        • 2021-09-14
        • 2019-04-29
        相关资源
        最近更新 更多