4

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.

4
  • 1
    Possible duplicate of Getting the docstring from a function Commented Mar 22, 2019 at 10:49
  • 3
    This is clearly not duplicate of the mentioned link. This question is specific to getting summary/first line from doc string. Commented Mar 22, 2019 at 11:46
  • Yeah, but the part which isn't obvious from the duplicate is not well-defined in your question. Commented Mar 22, 2019 at 11:51
  • Anyway, I got the appropriate answer for my question. Commented Mar 22, 2019 at 11:56

2 Answers 2

6

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]

Sign up to request clarification or add additional context in comments.

Comments

1

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

print test_filesystem_001.__doc__.splitlines()[0]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.