在 class 属性 和 py.test 上使用 doctest
Using doctest on class property with py.test
我尝试在 py.test 中用 doctest 测试 属性 但我收到一个 NameError 说 class 没有定义。
这是一个小例子:
class ExampleClass:
def __init__(self, attribute):
self.attribute = attribute
@property
def attribute_squared(self):
"""
Examples:
>>> ExampleClass(attribute=2).attribute_squared
4
"""
return self.attribute ** 2
当 运行 它发牢骚时 py.test 我得到:
Error
**********************************************************************
Line 3, in ExampleClass
Failed example:
ExampleClass(attribute=2)
Exception raised:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.1.5\helpers\pycharm\docrunner.py", line 140, in __run
compileflags, 1), test.globs)
File "<doctest ExampleClass[0]>", line 1, in <module>
ExampleClass(attribute=2).attribute_squared
NameError: name 'ExampleClass' is not defined
有没有办法测试 class 属性,或者我应该编写一个真正的测试?
最后我发现只有在Pycharm中做对click/Run 'Doctest attribute_squared'才通过测试。
运行pytest时,测试通过
我尝试在 py.test 中用 doctest 测试 属性 但我收到一个 NameError 说 class 没有定义。
这是一个小例子:
class ExampleClass:
def __init__(self, attribute):
self.attribute = attribute
@property
def attribute_squared(self):
"""
Examples:
>>> ExampleClass(attribute=2).attribute_squared
4
"""
return self.attribute ** 2
当 运行 它发牢骚时 py.test 我得到:
Error
**********************************************************************
Line 3, in ExampleClass
Failed example:
ExampleClass(attribute=2)
Exception raised:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.1.5\helpers\pycharm\docrunner.py", line 140, in __run
compileflags, 1), test.globs)
File "<doctest ExampleClass[0]>", line 1, in <module>
ExampleClass(attribute=2).attribute_squared
NameError: name 'ExampleClass' is not defined
有没有办法测试 class 属性,或者我应该编写一个真正的测试?
最后我发现只有在Pycharm中做对click/Run 'Doctest attribute_squared'才通过测试。 运行pytest时,测试通过