我写测试功能。在docstring中,我经常提到测试用例名称作为摘要行。测试用例描述如下。现在我只需要从docstring中获取测试用例名称(第一行)。有没有pythonic方式来做到这一点?
def test_filesystem_001():
"""This is test case name of test_filesystem_001.
[Test Description]
-Create a file
-write some data
-delete it
"""
pass
所以我需要一种方法来打印文档字符串的第一行,即“这是test_filesystem_001的测试用例名称”。提前致谢。
刚刚获得第一行:
>>>test_filesystem_001.__doc__.split("\n")[0]
This is test case name of test_filesystem_001.
你split
__doc__
字符串在一个新的行。这将返回新行之前的部分数组和新行之后的部分。要访问第一部分,请使用[0]
获取文档字符串,将其拆分为行,然后取第一个。
print test_filesystem_001.__doc__.splitlines()[0]