Comment 0 for bug 1097975

Revision history for this message
Oliver Drake (216ck-oliver) wrote : TestProtocolClient: Formatting traceback fails in ipython notebook

I was trying to play with subunit in ipython notebook:

import unittest

class MyTest(unittest.TestCase):
    def test_one(self):
        assert True

    def test_two(self):
        assert False, "two failed..."

suite1 = unittest.TestLoader().loadTestsFromTestCase(MyTest)

import subunit
import sys

test_result = subunit.TestProtocolClient(sys.stdout)
suite1.run(test_result)

The traceback I get is:
Traceback (most recent call last):
  File "<ipython-input-5-0e0da22da8b3>", line 6, in <module>
    suite1.run(test_result)
  File "/usr/lib/python2.7/unittest/suite.py", line 108, in run
    test(result)
  File "/usr/lib/python2.7/unittest/case.py", line 391, in __call__
    return self.run(*args, **kwds)
  File "/usr/lib/python2.7/unittest/case.py", line 331, in run
    result.addFailure(self, sys.exc_info())
  File "/home/drakeol/virtualenvs/dvvtools/local/lib/python2.7/site-packages/subunit/__init__.py", line 668, in addFailure
    self._addOutcome("failure", test, error=error, details=details)
  File "/home/drakeol/virtualenvs/dvvtools/local/lib/python2.7/site-packages/subunit/__init__.py", line 697, in _addOutcome
    tb_content = TracebackContent(error, test)
  File "/home/drakeol/virtualenvs/dvvtools/local/lib/python2.7/site-packages/testtools/content.py", line 142, in __init__
    value = self._exc_info_to_unicode(err, test)
  File "/home/drakeol/virtualenvs/dvvtools/local/lib/python2.7/site-packages/testtools/content.py", line 169, in _exc_info_to_unicode
    msgLines = format_exception(exctype, value, tb, length)
  File "/home/drakeol/virtualenvs/dvvtools/local/lib/python2.7/site-packages/testtools/compat.py", line 352, in _format_exc_info
    if evalue is None:
  File "/home/drakeol/virtualenvs/dvvtools/local/lib/python2.7/site-packages/testtools/compat.py", line 289, in _get_source_encoding
    newtuple = _EncodingTuple(linecache.cache[filename])
  File "/home/drakeol/virtualenvs/dvvtools/local/lib/python2.7/site-packages/testtools/compat.py", line 261, in _detect_encoding
    # Source starting with UTF-8 BOM is either UTF-8 or a SyntaxError
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)

When I run the same code in a normal python file (utf-8 encoding) it runs fine and I see subunit output on stdout:
$ python subunitscratch.py
test: __main__.MyTest.test_one
successful: __main__.MyTest.test_one
test: __main__.MyTest.test_two
failure: __main__.MyTest.test_two [
Traceback (most recent call last):
  File "subunitscratch.py", line 8, in test_two
    assert False, "two failed..."
AssertionError: two failed...
]

I'm guessing it's to do with the fact that running in ipython notebook means there's no encoding for the source file of a traceback...
Python 2.7.3
testrepository==0.0.8
testtools==0.9.22
nose-subunit==0.2
python-subunit==0.0.8

Aside: when I run the same code in the vanilla interpreter it runs ok but the traceback is less informative as there's no source file:
suite.run(test_result)
test: __main__.MyTest.test_one
failure: __main__.MyTest.test_one [
Traceback (most recent call last):
  File "<stdin>", line 3, in test_one
AssertionError
]