在名为`types.py`的文件上运行Python的doctest

时间:2018-04-19 13:38:14

标签: python testing doctest

在Python 2和3中,我都无法在名为types.py的文件中运行doctests,这是一个包的一部分。这就是我得到的:

$ cat foo/types.py
def x():
    """do something

    >>> x()
    1
    """
    return 1

$ cp foo/types.py foo/types2.py

$ python -m doctest -v foo/types.py
1 items had no tests:
    types
0 tests in 1 items.
0 passed and 0 failed.
Test passed.

$ python -m doctest -v foo/types2.py
Trying:
    x()
Expecting:
    1
ok
1 items had no tests:
    types2
1 items passed all tests:
   1 tests in types2.x
1 tests in 2 items.
1 passed and 0 failed.
Test passed.

$ python3 -m doctest -v foo/types.py
37 items had no tests:
    types
    types.DynamicClassAttribute
    types.DynamicClassAttribute.__delete__
    types.DynamicClassAttribute.__get__
    types.DynamicClassAttribute.__init__
    types.DynamicClassAttribute.__set__
    types.DynamicClassAttribute.deleter
    types.DynamicClassAttribute.getter
    types.DynamicClassAttribute.setter
    types.SimpleNamespace
    types.SimpleNamespace.__delattr__
    types.SimpleNamespace.__eq__
    types.SimpleNamespace.__ge__
    types.SimpleNamespace.__getattribute__
    types.SimpleNamespace.__gt__
    types.SimpleNamespace.__init__
    types.SimpleNamespace.__le__
    types.SimpleNamespace.__lt__
    types.SimpleNamespace.__ne__
    types.SimpleNamespace.__reduce__
    types.SimpleNamespace.__repr__
    types.SimpleNamespace.__setattr__
    types._GeneratorWrapper
    types._GeneratorWrapper.__init__
    types._GeneratorWrapper.__iter__
    types._GeneratorWrapper.__next__
    types._GeneratorWrapper.close
    types._GeneratorWrapper.cr_await
    types._GeneratorWrapper.gi_code
    types._GeneratorWrapper.gi_frame
    types._GeneratorWrapper.gi_running
    types._GeneratorWrapper.send
    types._GeneratorWrapper.throw
    types._calculate_meta
    types.coroutine
    types.new_class
    types.prepare_class
0 tests in 37 items.
0 passed and 0 failed.
Test passed.

$ python3 -m doctest -v foo/types2.py
Trying:
    x()
Expecting:
    1
ok
1 items had no tests:
    types2
1 items passed all tests:
   1 tests in types2.x
1 tests in 2 items.
1 passed and 0 failed.
Test passed.

如您所见,foo/types2.py的所有调用都按预期工作,所有foo/types.py的调用似乎都尝试加载Python内置types模块。

我也无法通过修改PYTHONPATH

来解决这个问题
$ PYTHONPATH=.:$PYTHONPATH python -m doctest -v foo/types.py
Traceback (most recent call last):
  File "/usr/lib/python2.7/site.py", line 68, in <module>
    import os
  File "/usr/lib/python2.7/os.py", line 400, in <module>
    import UserDict
  File "/usr/lib/python2.7/UserDict.py", line 116, in <module>
    import _abcoll
  File "/usr/lib/python2.7/_abcoll.py", line 70, in <module>
    Iterable.register(str)
  File "/usr/lib/python2.7/abc.py", line 107, in register
    if not isinstance(subclass, (type, types.ClassType)):
AttributeError: 'module' object has no attribute 'ClassType'

不幸的是,我不能简单地重命名foo/types.py

除了在它周围编写大量的样板代码之外,是否有可能从这个文件中运行doctests?

1 个答案:

答案 0 :(得分:1)

我认为您不能在此使用python -m doctest:文档说明“import[s the module] as a standalone module”,添加

  

请注意,如果文件是程序包的一部分并从该程序包导入其他子模块,则这可能无法正常工作。

这是一种说法,它使用模块的非限定名称。当然它会与标准库模块发生冲突。