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

Erik Holleboom's --add-reporter, with changes #1

Merged
merged 13 commits into from Jul 25, 2011
31 changes: 31 additions & 0 deletions .gitignore
@@ -0,0 +1,31 @@
*.py[co]

# Packages
*.egg
*.egg-info
dist
build
eggs
parts
bin
var
sdist
develop-eggs
.installed.cfg

# Installer logs
pip-log.txt

# Unit test / coverage reports
.coverage
.tox

#Translations
*.mo

#Mr Developer
.mr.developer.cfg

# Other
*~
.*.swp
37 changes: 37 additions & 0 deletions src/testoob/commandline/dynamic_reporter_option.py
@@ -0,0 +1,37 @@
import parsing
parsing.parser.add_option("--add-reporter", metavar="CLASSNAME",
help="dynamically load reporter class (package.module.classname), "
"must be in the PYTHONPATH")

def split_module_classname(full_class_name):
last_dot = full_class_name.rfind(".")
module_name = full_class_name[:last_dot]
class_name = full_class_name[last_dot+1:]
return module_name, class_name

def dynamically_import_class(full_class_name):
module_name, class_name = split_module_classname(full_class_name)

try:
module = __import__(module_name, globals(), locals(), [class_name])
return getattr(module, class_name)
except ImportError, e:
# TODO: show entire stack trace?
raise parsing.ArgumentsError("Can't load module '%s', got error: %s" % (module_name, e))
except AttributeError:
raise parsing.ArgumentsError("Can't find class '%s' in module '%s'" % (class_name, module_name))

def process_options(options):
if options.add_reporter is None:
return

klass = dynamically_import_class(options.add_reporter)

from testoob.reporting.base import IReporter
if not issubclass(klass, IReporter):
raise parsing.ArgumentsError("class '%s' must subclass testoob.reporting.base.IReporter" % options.add_reporter)

# instantiate the klass and attach it as a reporter
parsing.kwargs["reporters"].append( klass() )

parsing.option_processors.append(process_options)
39 changes: 39 additions & 0 deletions tests/large/test_commandline.py
Expand Up @@ -877,6 +877,45 @@ def testHtmlDoubleFailure(self):
finally:
_safe_unlink(output_file)

def testAddReporterBadModuleName(self):
args = _testoob_args(options=["--add-reporter=NoSuchModule.Foo"])
regex=r"Can't load module 'NoSuchModule', got error: "
testoob.testing.command_line(
args = args,
expected_output_regex = "", # accept anything on stdout
expected_error_regex = regex,
rc_predicate = lambda rc: rc != 0,
)

def testAddReporterBadClassName(self):
args = _testoob_args(options=["--add-reporter=sys.NoSuchClass"])
regex=r"Can't find class 'NoSuchClass' in module 'sys'"
testoob.testing.command_line(
args = args,
expected_output_regex = "", # accept anything on stdout
expected_error_regex = regex,
rc_predicate = lambda rc: rc != 0,
)

def testAddReporterReporterNotDerivedFromIReporter(self):
args = _testoob_args(options=["--add-reporter=testoob.reporting.reporter_proxy.ReporterProxy"])
regex=r"class '.*ReporterProxy' must subclass .*IReporter"
testoob.testing.command_line(
args = args,
expected_output_regex = "", # accept anything on stdout
expected_error_regex = regex,
rc_predicate = lambda rc: rc != 0,
)

def testAddReporter(self):
args = _testoob_args(options=["--add-reporter=testoob.reporting.base.BaseReporter"], tests=["CaseDigits"])
testoob.testing.command_line(
args = args,
expected_output_regex = "", # accept anything on stdout
expected_error_regex = "", # accept anything on stderr
expected_rc = 0,
)


if __name__ == "__main__":
testoob.main()
1 change: 1 addition & 0 deletions win/runtestsall.bat
@@ -1,5 +1,6 @@
set PYTHONPATH=src
set TESTOOB_DEVEL_TEST=1
c:\Python27\python.exe src\testoob\testoob tests\alltests.py suite %*
c:\Python26\python.exe src\testoob\testoob tests\alltests.py suite %*
c:\Python25\python.exe src\testoob\testoob tests\alltests.py suite %*
c:\Python24\python.exe src\testoob\testoob tests\alltests.py suite %*