8

What's the best practice to provide the description of a test script in python?

Obviously I can put the comments below the test case, but wanted to know if there's any standard practice (any methods I should write) to provide the description of the test case (detailed information on what the test case is supposed to do)?

Is this how you would put the test description?:

Class TestFoo:
    def testfoo1():
    """
    test description:
    step1:
    step2: 
    """

Any suggestions/references would be appreciated.

3 Answers 3

8

The test method docstring is the standard place to put that information If you are using python's unittest module. unittest will use that docstring to format output, etc.

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

Comments

6

In the unittest framework, you have the shortDecription method:

shortDescription()

Returns a description of the test, or None if no description has been provided. The default implementation of this method returns the first line of the test method’s docstring, if available.

So, in fact, using the method docstring is a fine place. You may have to inherit from TestCase in your class declaration for the runner to work like that, though.

For best practice: name the test case (class) and the test methods in a concise but useful fashion which is sufficient for developers to have a high level idea of where something is going wrong, should that particular test fail. A prerequisite to this is each test method should only be testing one thing, rather than asserting on a whole bunch of different things.

With sensible test names, usually a docstring with "detailed information on what the test case is supposed to do" would not be necessary. If you have existing large tests which check many things, you may want to split them up into a bunch of smaller tests which each assert on one and only one individual thing.

7 Comments

I've never noticed shortDescription before. The docs there are a little light -- Specifically, it seems like it would need some way to know which test to write the description for...
That's likely just mutable state on the instance (a TestCase is a class after all)
Oh, never mind, the implementation of the base method relies on some state-based implementation details to figure out what to return. I'm not really sure how a user should override that method in a useful way...
I'm not suggesting to override the method. I'm substantiating that you can use the docstring, because of this method defined on TestCase.
Yeah -- I wasn't trying to attack your answer or anything. I'm more thinking out loud about how that seems to be a silly method to expose in the unit-test API. I think that your answer is fine as-is (better than mine in that it points out that only the first line is actually used for formatting output).
|
2

The best way would be for the name of the class to be descriptive enough for a description not to be needed.

Barring that, the docstring is the best approach.

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.