Is there a way to catch unittest exceptions with PyCharm?

I know this is an old thread, and perhaps some of these things weren't around when the question was asked. But just for posterity...

You can run your unittest tests with py.test as a test runner. You'll get some great features right out of the box. Directly to the point of your question, there are a few good ways to debug with py.test, including with PyCharm.

Specifically to debug on any failure, post-mortem, with PyCharm - what you are asking for - you can install the pytest-pycharm plugin.

pip install pytest-pycharm

(I am working in a virtualenv per-project so this has no downside for me. I recommend you do the same. Otherwise, be aware that you're installing it globally, and this plugin will be active for all projects using this Python interpeter.)

See also:

  • JetBrains PyCharm docs re: Testing Frameworks, which will tell you how to use py.test as your testrunner in PyCharm.
  • Stack Overflow thread: Debugging pytest post mortem exceptions in pycharm/pydev

The solution added to question itself is not great IMO:

  1. It wouldn't work with PyCharm unittest runner, so you can debug it only as a script not as tests.
  2. It doesn't print information about testing progress and results.

My solution is more "hacky", but it doesn't have mentioned drawbacks. Also, test behavior changes only if tests are ran under debugger.

unittest.TestResult.addError is called in exception handler if an exception is not expected by the test. I redefine this method to re-raise exception being handled.

I use this class as a parent for my test cases:

class DebuggableTestCase(unittest.TestCase):
    def __add_error_replacement(self, _, err):
        value, traceback = err[1:]
        raise value.with_traceback(traceback)

    def run(self, result=None):
        if result and sys.gettrace() is not None:
            result.addError = self.__add_error_replacement
        super().run(result)

Solution was tested with Python 3.4 and PyCharm 3.4


If what you want is inspect why the unittest failed, you don't really need to raise the Exception.

Just set a breakpoint at the line where the exception occurs and run under debug mode. PyCharm will drop to the debug screen and let you inspect variables and the call stack.