[svn r57771] adding Guido's py_unittest unittest TestCase collector, adapting to py/trunk API and simplifing it a bit.

--HG--
branch : trunk
This commit is contained in:
hpk 2008-09-02 21:08:45 +02:00
parent 9eb1d55380
commit 314d103884
3 changed files with 141 additions and 0 deletions

View File

@ -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()

View File

@ -0,0 +1,6 @@
code for collecting traditional unit tests based on
http://johnnydebris.net/svn/projects/py_unittest
from Guido Wesdorp.

View File

@ -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