~treinish/subunit/trace-on-import-error

« back to all changes in this revision

Viewing changes to python/subunit/run.py

  • Committer: mtreinish at kortar
  • Date: 2014-05-30 17:34:20 UTC
  • Revision ID: mtreinish@kortar.org-20140530173420-vttq4jughvu7bs2y
Generate an import traceback for test import errors

This commit attempts to generate a stack trace for each of the import
errors that testtools.list_test() 

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import io
24
24
import os
25
25
import sys
 
26
import traceback
26
27
 
27
28
from testtools import ExtendedToStreamDecorator
28
29
from testtools.testsuite import iterate_tests
67
68
    def list(self, test):
68
69
        "List the test."
69
70
        result, errors = self._list(test)
 
71
        formatted_errors = []
70
72
        if errors:
71
 
            failed_descr = '\n'.join(errors).encode('utf8')
 
73
            for error in errors:
 
74
                try:
 
75
                    __import__(error)
 
76
                except ImportError as import_error:
 
77
                    formatted_errors.append(traceback.format_exc())
 
78
                else:
 
79
                    formatted_errors.append(error)
 
80
            failed_descr = '\n'.join(formatted_errors).encode('utf8')
72
81
            result.status(file_name="import errors", runnable=False,
73
82
                file_bytes=failed_descr, mime_type="text/plain;charset=utf8")
74
83
            sys.exit(2)