3

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.

XgigPro
  • 134
  • 1
  • 11
  • 1
    Possible duplicate of [Getting the docstring from a function](https://stackoverflow.com/questions/713138/getting-the-docstring-from-a-function) – tripleee Mar 22 '19 at 10:49
  • 1
    This is clearly not duplicate of the mentioned link. This question is specific to getting summary/first line from doc string. – XgigPro Mar 22 '19 at 11:46
  • Yeah, but the part which isn't obvious from the duplicate is not well-defined in your question. – tripleee Mar 22 '19 at 11:51
  • Anyway, I got the appropriate answer for my question. – XgigPro Mar 22 '19 at 11:56

2 Answers2

5

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]

Uli Sotschok
  • 1,206
  • 1
  • 9
  • 19
0

Take the docstring, split it into lines, and take the first.

print test_filesystem_001.__doc__.splitlines()[0]
Peter Westlake
  • 4,894
  • 1
  • 26
  • 35