0

I have a decorator that wraps a generator that yields from inside a nose test case. For every iteration, I'm looking to catch and run a specific teardown if an exception occurs, however it does not seem to behave as expected.

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

Is there something I am doing wrong?

ILostMySpoon
  • 2,399
  • 2
  • 19
  • 25

1 Answers1

0

I'm not sure exactly what the unexpected behavior is, but maybe it is happening because you are not trying each loop iteration individually.

Maybe this will work?

def print_log(test_case):
    @wraps(test_case)
    def run_test(self):
        from six.moves import next
        test_iter = iter(test_case(self))
        while True:
            try:
                next(test_iter)
            except StopIteration: 
                break
            except Exception:
                Test_Loop.failure_teardown(self)
                raise
   return run_test
Erotemic
  • 4,806
  • 4
  • 39
  • 80
  • The unexpected behavior is that it doesn't even reach the expect block. To test, I forced an exception some place within the yield function and it gets raised, but it simply continues with a regular teardown as opposed to the specific one I wrote. – ILostMySpoon Mar 03 '16 at 14:25