diff options
author | Jason Pellerin <jpellerin@gmail.com> | 2006-12-19 03:35:52 +0000 |
---|---|---|
committer | Jason Pellerin <jpellerin@gmail.com> | 2006-12-19 03:35:52 +0000 |
commit | 05bff2bd41a7d1930dbfc4eb87a19e1461f5b467 (patch) | |
tree | 23e570e05f07cfda8862862ba54bd53331031468 /examples | |
download | nose-05bff2bd41a7d1930dbfc4eb87a19e1461f5b467.tar.gz |
[0.10-dev] Imported last revision from python-hosting to start 0.10-dev branch
Diffstat (limited to 'examples')
-rw-r--r-- | examples/attrib_plugin.py | 82 | ||||
-rw-r--r-- | examples/html_plugin/htmlplug.py | 88 | ||||
-rw-r--r-- | examples/html_plugin/setup.py | 24 | ||||
-rw-r--r-- | examples/plugin/plug.py | 4 | ||||
-rw-r--r-- | examples/plugin/setup.py | 27 |
5 files changed, 225 insertions, 0 deletions
diff --git a/examples/attrib_plugin.py b/examples/attrib_plugin.py new file mode 100644 index 0000000..c1f8458 --- /dev/null +++ b/examples/attrib_plugin.py @@ -0,0 +1,82 @@ +"""
+Examples of test function/method attribute usage with patched nose
+
+Simple syntax (-a, --attr) examples:
+ * nosetests -a status=stable
+ => only test cases with attribute "status" having value "stable"
+
+ * nosetests -a priority=2,status=stable
+ => both attributes must match
+
+ * nosetests -a tags=http
+ => attribute list "tags" must contain value "http" (see test_foobar()
+ below for definition)
+
+ * nosetests -a slow
+ => attribute "slow" must be defined and its value cannot equal to False
+ (False, [], "", etc...)
+
+ * nosetests -a !slow
+ => attribute "slow" must NOT be defined or its value must be equal to False
+
+Eval expression syntax (-A, --eval-attr) examples:
+ * nosetests -A "not slow"
+ * nosetests -A "(priority > 5) and not slow"
+
+This example and the accompanied patch is in public domain, free for any use.
+
+email: mika.eloranta@gmail.com
+
+"""
+
+__author__ = 'Mika Eloranta'
+
+def attr(**kwargs):
+ """Add attributes to a test function/method/class"""
+ def wrap(func):
+ func.__dict__.update(kwargs)
+ return func
+ return wrap
+
+# test function with single attribute
+@attr(priority = 1)
+def test_dummy():
+ print "dummy"
+
+# test function with multiple attributes
+@attr(status = "stable", # simple string attribute
+ slow = True, # attributes can be of any type
+ # (e.g. bool)
+ priority = 1, # ...or int
+ tags = ["http", "pop", "imap"]) # will be run if any of the list items
+ # matches
+def test_foobar():
+ print "foobar"
+
+# another way of adding attributes...
+def test_fluffy():
+ print "fluffy"
+test_fluffy.status = "unstable"
+test_fluffy.slow = True
+test_fluffy.priority = 2
+
+# works for class methods, too
+class TestSomething:
+ @attr(status = "stable", priority = 2)
+ def test_xyz(self):
+ print "xyz"
+
+# class methods "inherit" attributes from the class but can override them
+class TestOverride:
+ value = "class"
+ # run all methods with "nosetests -a value"
+
+ @attr(value = "method")
+ def test_override(self):
+ # run with "nosetests -a value=method"
+ print "override"
+
+ def test_inherit(self):
+ # run with "nosetests -a value=class"
+ print "inherit"
+
diff --git a/examples/html_plugin/htmlplug.py b/examples/html_plugin/htmlplug.py new file mode 100644 index 0000000..8394363 --- /dev/null +++ b/examples/html_plugin/htmlplug.py @@ -0,0 +1,88 @@ +"""This is a very basic example of a plugin that controls all test +output. In this case, it formats the output as ugly unstyled html. + +Upgrading this plugin into one that uses a template and css to produce +nice-looking, easily-modifiable html output is left as an exercise for +the reader who would like to see his or her name in the nose AUTHORS file. +""" +import traceback +from nose.plugins import Plugin + +class HtmlOutput(Plugin): + """Output test results as ugly, unstyled html. + """ + + name = 'html-output' + + def __init__(self): + super(HtmlOutput, self).__init__() + self.html = [ '<html><head>', + '<title>Test output</title>', + '</head><body>' ] + + def addSuccess(self, test, capt): + self.html.append('<span>ok</span>') + + def addSkip(self, test): + self.html.append('<span>SKIPPED</span>') + + def addDeprecated(self, test): + self.html.append('<span>DEPRECATED</span>') + + def addError(self, test, err, capt): + err = self.formatErr(err) + self.html.append('<span>ERROR</span>') + self.html.append('<pre>%s</pre>' % err) + if capt: + self.html.append('<pre>%s</pre>' % capt) + + def addFailure(self, test, err, capt, tb_info): + err = self.formatErr(err) + self.html.append('<span>FAIL</span>') + self.html.append('<pre>%s</pre>' % err) + if tb_info: + self.html.append('<pre>%s</pre>' % tb_info) + if capt: + self.html.append('<pre>%s</pre>' % capt) + + def finalize(self, result): + self.html.append('<div>') + self.html.append("Ran %d test%s" % + (result.testsRun, result.testsRun != 1 and "s" or "")) + self.html.append('</div>') + self.html.append('<div>') + if not result.wasSuccessful(): + self.html.extend(['<span>FAILED ( ', + 'failures=%d ' % len(result.failures), + 'errors=%d' % len(result.errors), + ')</span>']) + else: + self.html.append('OK') + self.html.append('</div></body></html>') + # print >> sys.stderr, self.html + for l in self.html: + self.stream.writeln(l) + + def formatErr(self, err): + exctype, value, tb = err + return traceback.format_exception(exctype, value, tb) + + def setOutputStream(self, stream): + # grab for own use + self.stream = stream + # return dummy stream + class dummy: + def write(self, *arg): + pass + def writeln(self, *arg): + pass + d = dummy() + return d + + def startTest(self, test): + self.html.extend([ '<div><span>', + test.shortDescription() or str(test), + '</span>' ]) + + def stopTest(self, test): + self.html.append('</div>') diff --git a/examples/html_plugin/setup.py b/examples/html_plugin/setup.py new file mode 100644 index 0000000..ecc839e --- /dev/null +++ b/examples/html_plugin/setup.py @@ -0,0 +1,24 @@ +import sys +try: + import ez_setup + ez_setup.use_setuptools() +except ImportError: + pass + +from setuptools import setup + +setup( + name='Example html output plugin', + version='0.1', + author='Jason Pellerin', + author_email = 'jpellerin+nose@gmail.com', + description = 'Example nose html output plugin', + license = 'GNU LGPL', + py_modules = ['htmlplug'], + entry_points = { + 'nose.plugins': [ + 'htmlout = htmlplug:HtmlOutput' + ] + } + + ) diff --git a/examples/plugin/plug.py b/examples/plugin/plug.py new file mode 100644 index 0000000..444226d --- /dev/null +++ b/examples/plugin/plug.py @@ -0,0 +1,4 @@ +from nose.plugins import Plugin + +class ExamplePlugin(Plugin): + pass diff --git a/examples/plugin/setup.py b/examples/plugin/setup.py new file mode 100644 index 0000000..92a42f3 --- /dev/null +++ b/examples/plugin/setup.py @@ -0,0 +1,27 @@ +""" +An example of how to create a simple nose plugin. + +""" +try: + import ez_setup + ez_setup.use_setuptools() +except ImportError: + pass + +from setuptools import setup + +setup( + name='Example plugin', + version='0.1', + author='Jason Pellerin', + author_email = 'jpellerin+nose@gmail.com', + description = 'Example nose plugin', + license = 'GNU LGPL', + py_modules = ['plug'], + entry_points = { + 'nose.plugins': [ + 'example = plug:ExamplePlugin' + ] + } + + ) |