我有一段代码,a.py
。
from functools import cached_property, cache
import doctest
class C1:
def test_1(self):
"""
>>> C1().test_1()
'f'
"""
return "f"
@property
def test_2(self):
"""
>>> C1().test_2
'p'
"""
return "p"
@cached_property
def test_3(self):
"""
>>> C1().test_3
'cp'
"""
return "cp"
@cache
def test_4(self):
"""
>>> C1().test_4()
'c'
"""
return "c"
doctest.testmod()
test_3
是一个被 @cached_property
装饰的函数。
它有一个文档测试,但是并没有被执行。
$ python3 a.py -v
Trying:
C1().test_1()
Expecting:
'f'
ok
Trying:
C1().test_2
Expecting:
'p'
ok
Trying:
C1().test_4()
Expecting:
'c'
ok
2 items had no tests:
__main__
__main__.C1
3 items passed all tests:
1 tests in __main__.C1.test_1
1 tests in __main__.C1.test_2
1 tests in __main__.C1.test_4
3 tests in 5 items.
3 passed and 0 failed.
Test passed.
我该如何运行test_3 doctest?
环境
$ python3 --version
Python 3.9.6
$ uname
Darwin