Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If a function passed to .addOnException itself raises an exception, the test is treated as passing. #307

Open
tomprince opened this issue Jun 11, 2021 · 0 comments

Comments

@tomprince
Copy link

from testtools import TestCase

class Tests(TestCase):
    def test_thing(self):
        def raiser(_):
            raise Exception("cascade")
        self.addOnException(raiser)
        self.fail()

gives

Tests running...

Ran 1 test in 0.001s
OK
Traceback (most recent call last):
  File "/usr/lib64/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib64/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "../lib/python2.7/site-packages/testtools/run.py", line 267, in <module>
    main(sys.argv, sys.stdout)
  File "../lib/python2.7/site-packages/testtools/run.py", line 264, in main
    stdout=stdout)
  File "../lib/python2.7/site-packages/testtools/run.py", line 187, in __init__
    self.runTests()
  File "../lib/python2.7/site-packages/testtools/run.py", line 221, in runTests
    self.result = testRunner.run(self.test)
  File "../lib/python2.7/site-packages/testtools/run.py", line 107, in run
    return test.run(result)
  File "../lib/python2.7/site-packages/unittest2/suite.py", line 126, in run
    test(result)
  File "../lib/python2.7/site-packages/unittest2/suite.py", line 87, in __call__
    return self.run(*args, **kwds)
  File "../lib/python2.7/site-packages/unittest2/suite.py", line 126, in run
    test(result)
  File "../lib/python2.7/site-packages/unittest2/suite.py", line 87, in __call__
    return self.run(*args, **kwds)
  File "../lib/python2.7/site-packages/unittest2/suite.py", line 126, in run
    test(result)
  File "../lib/python2.7/site-packages/unittest2/case.py", line 673, in __call__
    return self.run(*args, **kwds)
  File "../lib/python2.7/site-packages/testtools/testcase.py", line 675, in run
    return run_test.run(result)
  File "../lib/python2.7/site-packages/testtools/runtest.py", line 80, in run
    return self._run_one(actual_result)
  File "../lib/python2.7/site-packages/testtools/runtest.py", line 94, in _run_one
    return self._run_prepared_result(ExtendedToOriginalDecorator(result))
  File "../lib/python2.7/site-packages/testtools/runtest.py", line 108, in _run_prepared_result
    self._run_core()
  File "../lib/python2.7/site-packages/testtools/runtest.py", line 144, in _run_core
    self.case._run_test_method, self.result):
  File "../lib/python2.7/site-packages/testtools/runtest.py", line 193, in _run_user
    return self._got_user_exception(sys.exc_info())
  File "../lib/python2.7/site-packages/testtools/runtest.py", line 213, in _got_user_exception
    self.case.onException(exc_info, tb_label=tb_label)
  File "../lib/python2.7/site-packages/testtools/testcase.py", line 626, in onException
    handler(exc_info)
  File "test_thing.py", line 6, in raiser
    raise Exception("cascade")
Exception: cascade

When using AsynchronousDeferredRunTest:

from testtools import TestCase                      
from testtools.twistedsupport import (              
    AsynchronousDeferredRunTest,                    
)                                                   
                                                    
class Tests(TestCase):                              
    run_tests_with = AsynchronousDeferredRunTest    
    def test_thing(self):                           
        def raiser(_):                              
            raise Exception("cascade")              
        self.addOnException(raiser)                 
        self.fail()                                 

the failure is entirely swallowed:

Tests running...

Ran 1 test in 0.063s
OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant