Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6044337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:00:00+00:00 2026-05-23T07:00:00+00:00

Assuming a python file with a whole bunch of functions I want to write

  • 0

Assuming a python file with a whole bunch of functions I want to write tests for the functions using doctest. For example every function takes a string and a connection object (httplib.HTTPConnection(...)). So I want to test every function if the string is empty or None. The tests would look like this.

def function_1(mystring, conn):
    r'''
    >>> conn = httplib.HTTPConnection(...)
    >>> function_1(None, conn)
    Traceback (most recent call last):
    NoneAsInputError: `mystring` should be a string and not `None`!

    >>> function_1("", conn)
    Traceback (most recent call last):
    EmptyStringError: `mystring` should not be an empty string!
    '''

    pass

def function_2(mystring, conn):
    r'''
    >>> conn = httplib.HTTPConnection(...)
    >>> function_2(None, conn)
    Traceback (most recent call last):
    NoneAsInputError: `mystring` should be a string and not `None`!

    >>> function_2("", conn)
    Traceback (most recent call last):
    EmptyStringError: `mystring` should not be an empty string!
    '''

    pass

[...]

def function_n(mystring, conn):
    r'''
    >>> conn = httplib.HTTPConnection(...)
    >>> function_n(None, conn)
    Traceback (most recent call last):
    NoneAsInputError: `mystring` should be a string and not `None`!

    >>> function_n("", conn)
    Traceback (most recent call last):
    EmptyStringError: `mystring` should not be an empty string!
    '''

    pass

As you can see, the test are identical, only the function name changes. Is it possible to refactor that to avoid code repetition?

Or is there a better way to lump together such tests?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:00:00+00:00Added an answer on May 23, 2026 at 7:00 am

    Never used doctest.

    def genDocText(func_name):
        return r'''
        >>> conn = httplib.HTTPConnection(...)
        >>> %(func_name)s(None, conn)
        Traceback (most recent call last):
        NoneAsInputError: `mystring` should be a string and not `None`!
    
        >>> %(func_name)s("", conn)
        Traceback (most recent call last):
        EmptyStringError: `mystring` should not be an empty string!
        ''' % {'func_name': func_name}
    
    def function_1(mystring, conn):
        pass
    
    function_1.__doc__ = genDocText('function_1')
    

    Is this a bad way?

    UPDATE:
    Using decorators the solution above can be written as:

    def genDocText(func):
        func.__doc__ = r'''
        >>> conn = httplib.HTTPConnection(...)
        >>> %(func_name)s(None, conn)
        Traceback (most recent call last):
        NoneAsInputError: `mystring` should be a string and not `None`!
    
        >>> %(func_name)s("", conn)
        Traceback (most recent call last):
        EmptyStringError: `mystring` should not be an empty string!
        ''' % {'func_name': func.__name__}
        return func
    
    @genDocText
    def function_1(mystring, conn):
        pass
    
    @genDocText
    def function_2(mystring, conn):
        pass
    
    @genDocText
    def function_n(mystring, conn):
        pass
    
    if __name__ == '__main__':
        print function_1.__doc__
        print function_2.__doc__
        print function_n.__doc__
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to detect whether a file is locked, using python on Unix. It's
How do I retrieve the temperature of my CPU using Python? (Assuming I'm on
Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
Im using Python's built in XML parser to load a 1.5 gig XML file
I'm a newbie to Python and I'm looking at using it to write some
Assuming the webserver is configured to handle .exe , Can i compile a python
Assuming I'm using some graphic API which allows me to draw bezier curves by
Assuming I have a column called A and I want to check if A
Assuming you have only the URL to a file (hosted on the same server
assuming that I know the PID of a process and want to do a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.