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

  • SEARCH
  • Home
  • 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 4608850
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:54:12+00:00 2026-05-22T00:54:12+00:00

I want to write a python decorator to decorate a test function of a

  • 0

I want to write a python decorator to decorate a test function of a unittest.TestCase, to decide the target host this function should run against. See this example:

class MyTestCase(unittest.TestCase):
    @target_host(["host1.com", "host2.com"])
    def test_my_command(self):
        #do something here against the target host

In the decorated function, I want to be able to execute this test against all hosts, how do I do that? The declaration of target_host is supposed to return a new function, but is it possible to return multiple function that the test runner can execute?

Thanks!

  • 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-22T00:54:12+00:00Added an answer on May 22, 2026 at 12:54 am

    You can return exactly one object (so technically, you could return a collections of functions). If you want to avoid astonishing everyone and if you want to call the result, you better return a single function though. But that function may very well call several other function in a loop… do you see where this leads to?

    You need a factory for decorators that return a closure calling the function they’re applied to once per set of arguments that factory got. In code (including functools.wraps to keep name and docstring, may be useful or not, I tend to include it by default):

    def call_with_each(*arg_tuples):
        def decorate(f):
            @functools.wraps(f)
            def decorator():
                for arg_tuple in arg_tuples:
                    f(*arg_tuple)
            return decorator
        return decorate
    
    # useage example:
    @call_with_each((3,), (2,)) # note that we pass several singleton tuples
    def f(x):
        print x
    # calling f() prints "3\n2\n"
    

    Supporting keyword arguments requires more code and perhaps some ugliness, but is possible. If it’s always going to be a single argument, the code can be simplified (def call_with_each(*args), for arg in args: f(arg), etc.).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a python function decorator that tests that certain arguments to
I want to write a function in Python that returns different fixed values based
Considering that I want to write python code that would run on Google App
I want to write some install scripts by python, it should know the OS
I want a decorator that would add the decorated function to list, like this
I want to write a module in python (This is the learning project) to
I want to write an ELisp function to start a Python interpreter (IPython) in
I want to write a wrapper function in R. I should take a function
I want to write in python the equivalent to this code in Matlab, zeta
As part of some WSGI middleware I want to write a python class that

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.