ILostMySpoon
ILostMySpoon

Reputation: 2409

Decorator over a nose test case that yields

I have the following decorator that is supposed to wrap the implementation of test case functions within a try/except block and print the log if an exception occurs.

def print_log(test_case):
    @wraps(test_case)
    def run_test(self):
        try:
            test_case(self)
        except:
            Test_Loop.failure_teardown(self)
            raise
   return run_test

This however does not seem to work on one of my test cases that calls a yield generator

Please bear with me as this is a basic example:

class Test_Loop:
    # ton of implementation here (e.g. initialization, etc)

    def runIt(self, name, ip, port):
        # code here

    @print_log
    def test_log_looper(self):
        for l in self.links:
            # initialize variables seen below and other stuff
            for n in names:
                # do stuff
                for i in ips:
                    # do stuff
                    for p in ports:
                        yield self.runIt, l, n, i, p

From debugging, when the decorator is applied, it seems that it does not even enter the first loop. What am I doing wrong?

Upvotes: 1

Views: 147

Answers (1)

DevShark
DevShark

Reputation: 9122

You need to iterate over your generator. Modify your decorator like this:

def print_log(test_case):
    @wraps(test_case)
    def run_test(self):
        try:
            for _ in test_case(self): pass
        except:
            Test_Loop.failure_teardown(self)
            raise
   return run_test

Upvotes: 1

Related Questions