From 314d1038849281ffdf7a1d4edbaa7cbf52d51003 Mon Sep 17 00:00:00 2001 From: hpk Date: Tue, 2 Sep 2008 21:08:45 +0200 Subject: [PATCH] [svn r57771] adding Guido's py_unittest unittest TestCase collector, adapting to py/trunk API and simplifing it a bit. --HG-- branch : trunk --- contrib/py_unittest/conftest.py | 71 ++++++++++++++++++++++++++++ contrib/py_unittest/readme.txt | 6 +++ contrib/py_unittest/test_conftest.py | 64 +++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 contrib/py_unittest/conftest.py create mode 100644 contrib/py_unittest/readme.txt create mode 100644 contrib/py_unittest/test_conftest.py diff --git a/contrib/py_unittest/conftest.py b/contrib/py_unittest/conftest.py new file mode 100644 index 000000000..8582e00f2 --- /dev/null +++ b/contrib/py_unittest/conftest.py @@ -0,0 +1,71 @@ +import py +import unittest +import sys +from py.__.test.collect import configproperty as _configproperty +unittest.failureException = AssertionError + +def configproperty(name): + def fget(self): + ret = self._config.getvalue(name, self.fspath) + return ret + return property(fget) + +class Module(py.test.collect.Module): + UnitTestCase = configproperty('UnitTestCase') + def makeitem(self, name, obj, usefilters=True): + # XXX add test_suite() support(?) + if py.std.inspect.isclass(obj) and issubclass(obj, unittest.TestCase): + return self.UnitTestCase(name, parent=self) + elif callable(obj) and getattr(obj, 'func_name', '') == 'test_suite': + return None + return super(Module, self).makeitem(name, obj, usefilters) + +class UnitTestCase(py.test.collect.Class): + TestCaseInstance = configproperty('TestCaseInstance') + def collect(self): + return [self.TestCaseInstance("()", self)] + + def setup(self): + pass + + def teardown(self): + pass + +_dummy = object() +class TestCaseInstance(py.test.collect.Instance): + UnitTestFunction = configproperty('UnitTestFunction') + def collect(self): + loader = unittest.TestLoader() + names = loader.getTestCaseNames(self.obj.__class__) + l = [] + for name in names: + callobj = getattr(self.obj, name) + if callable(callobj): + l.append(self.UnitTestFunction(name, parent=self)) + return l + + def _getobj(self): + x = self.parent.obj + return self.parent.obj(methodName='run') + +class UnitTestFunction(py.test.collect.Function): + def __init__(self, name, parent, args=(), obj=_dummy, sort_value=None): + super(UnitTestFunction, self).__init__(name, parent) + self._args = args + if obj is not _dummy: + self._obj = obj + self._sort_value = sort_value + + def runtest(self): + target = self.obj + args = self._args + target(*args) + + def setup(self): + instance = self.obj.im_self + instance.setUp() + + def teardown(self): + instance = self.obj.im_self + instance.tearDown() + diff --git a/contrib/py_unittest/readme.txt b/contrib/py_unittest/readme.txt new file mode 100644 index 000000000..d19c4e02f --- /dev/null +++ b/contrib/py_unittest/readme.txt @@ -0,0 +1,6 @@ +code for collecting traditional unit tests based on + + http://johnnydebris.net/svn/projects/py_unittest + +from Guido Wesdorp. + diff --git a/contrib/py_unittest/test_conftest.py b/contrib/py_unittest/test_conftest.py new file mode 100644 index 000000000..402b3b13b --- /dev/null +++ b/contrib/py_unittest/test_conftest.py @@ -0,0 +1,64 @@ +import py +from py.__.test.outcome import Failed +from py.__.test.testing import suptest +conftestpath = py.magic.autopath().dirpath("conftest.py") + +class TestTestCaseInstance(suptest.InlineSession): + def setup_method(self, method): + super(TestTestCaseInstance, self).setup_method(method) + self.tmpdir.ensure("__init__.py") + conftestpath.copy(self.tmpdir.join(conftestpath.basename)) + + def test_simple_unittest(self): + test_one = self.makepyfile(test_one=""" + import unittest + class MyTestCase(unittest.TestCase): + def test_passing(self): + self.assertEquals('foo', 'foo') + """) + sorter = self.parse_and_run(test_one) + rep = sorter.getreport("test_passing") + assert rep.passed + + def test_simple_failing(self): + test_one = self.makepyfile(test_one=""" + import unittest + class MyTestCase(unittest.TestCase): + def test_failing(self): + self.assertEquals('foo', 'bar') + """) + sorter = self.parse_and_run(test_one) + rep = sorter.getreport("test_failing") + assert rep.failed + + def test_setup(self): + test_one = self.makepyfile(test_one=""" + import unittest + class MyTestCase(unittest.TestCase): + def setUp(self): + self.foo = 1 + def test_setUp(self): + self.assertEquals(1, self.foo) + """) + sorter = self.parse_and_run(test_one) + rep = sorter.getreport("test_setUp") + assert rep.passed + + def test_teardown(self): + test_one = self.makepyfile(test_one=""" + import unittest + class MyTestCase(unittest.TestCase): + l = [] + def test_one(self): + pass + def tearDown(self): + self.l.append(None) + class Second(unittest.TestCase): + def test_check(self): + self.assertEquals(MyTestCase.l, [None]) + """) + sorter = self.parse_and_run(test_one) + passed, skipped, failed = sorter.countoutcomes() + assert passed + skipped + failed == 2 + assert failed == 0, failed + assert passed == 2