Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get just summary line from python docstring?

I write test functions. In the docstring, I usually mention test case name as the summary line. The test case description follows that. Now I just need to fetch the test case name (first line) from docstring. Is there any pythonic way to do it?

   def test_filesystem_001():
        """This is test case name of test_filesystem_001.

        [Test Description]
        -Create a file
        -write some data
        -delete it
        """
        pass

So I need a way here just to print the first line of the docstring, i.e., "This is test case name of test_filesystem_001." Thanks in advance.

like image 658
XgigPro Avatar asked Sep 17 '25 08:09

XgigPro


1 Answers

Just getting the first line:

>>>test_filesystem_001.__doc__.split("\n")[0]
This is test case name of test_filesystem_001.

You split the __doc__ string at a new line. This returns an array of the part before the new line and the part after the new line. To access the first part use [0]

like image 180
Uli Sotschok Avatar answered Sep 19 '25 23:09

Uli Sotschok