2

I'm trying to use doctest in qpython. But the script didn't work , which it it ok in PC enviroment.

It is just two line different with the my script in PC enviroment: import sl4a droid = sl4a.Android()

After run the script in qpython, I can see the log of docctest, but the test case seems not be tested. The msg I got is : 11 items had no tests: ... 0 tests in 11 items. 0 passed and 0 faied. Test passed

Is there any important things I missed? Thanks for your help!

Roger Ding
  • 21
  • 1

2 Answers2

1

That's because qpython runs python with optimization on (-OO), which removes docstrings, so doctest doesn't see anything. The following trick gives you almost all functionality back. Basically, it just parses the source file with the ast module to grab the docstrings and put then in the __test__ dictionary.

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

Just call this before calling doctest.testmod, and it wil run the docstrings.

Jurjen
  • 11
  • 2
0

Maybe you found a solution, but I had a similar experience with QPython the other day. Seems you can not (yet) use Player or Pyjnius in the console with QPython. Running the script as a Kivy app makes imports of Plyer or Pyjnius work. Try adding the following lines:

#-*-coding:utf8;-*-
#qpy:2
#qpy:kivy
Usman Ali
  • 425
  • 1
  • 9
  • 31