majorly changing the unittest compatibility code, calling TestCase(name)(result)

This commit is contained in:
holger krekel 2010-11-01 23:08:16 +01:00
parent 53d1cfc3a1
commit 7d495cc250
6 changed files with 50 additions and 45 deletions

View File

@ -2,6 +2,8 @@ Changes between 1.3.4 and 2.0.0dev0
---------------------------------------------- ----------------------------------------------
- pytest-2.0 is now its own package and depends on pylib-2.0 - pytest-2.0 is now its own package and depends on pylib-2.0
- try harder to run unittest test suites in a more compatible manner
by deferring setup/teardown semantics to the unittest package.
- introduce a new way to set config options via ini-style files, - introduce a new way to set config options via ini-style files,
by default setup.cfg and tox.ini files are searched. The old by default setup.cfg and tox.ini files are searched. The old
ways (certain environment variables, dynamic conftest.py reading ways (certain environment variables, dynamic conftest.py reading

View File

@ -5,7 +5,7 @@ see http://pytest.org for documentation and details
(c) Holger Krekel and others, 2004-2010 (c) Holger Krekel and others, 2004-2010
""" """
__version__ = '2.0.0.dev17' __version__ = '2.0.0.dev18'
__all__ = ['config', 'cmdline'] __all__ = ['config', 'cmdline']

View File

@ -383,7 +383,8 @@ class Function(FunctionMixin, pytest.collect.Item):
config=config, collection=collection) config=config, collection=collection)
self._args = args self._args = args
if self._isyieldedfunction(): if self._isyieldedfunction():
assert not callspec, "yielded functions (deprecated) cannot have funcargs" assert not callspec, (
"yielded functions (deprecated) cannot have funcargs")
else: else:
if callspec is not None: if callspec is not None:
self.funcargs = callspec.funcargs or {} self.funcargs = callspec.funcargs or {}

View File

@ -18,52 +18,32 @@ def pytest_pycollect_makeitem(collector, name, obj):
return UnitTestCase(name, parent=collector) return UnitTestCase(name, parent=collector)
class UnitTestCase(py.test.collect.Class): class UnitTestCase(py.test.collect.Class):
def collect(self):
return [UnitTestCaseInstance("()", self)]
def setup(self):
pass
def teardown(self):
pass
_dummy = object()
class UnitTestCaseInstance(py.test.collect.Instance):
def collect(self): def collect(self):
loader = py.std.unittest.TestLoader() loader = py.std.unittest.TestLoader()
names = loader.getTestCaseNames(self.obj.__class__) for name in loader.getTestCaseNames(self.obj):
l = [] yield TestCaseFunction(name, parent=self)
for name in names:
callobj = getattr(self.obj, name)
if py.builtin.callable(callobj):
l.append(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
if hasattr(self.parent, 'newinstance'):
self.parent.newinstance()
self.obj = self._getobj()
def runtest(self):
target = self.obj
args = self._args
target(*args)
def setup(self): def setup(self):
instance = py.builtin._getimself(self.obj) meth = getattr(self.obj, 'setUpClass', None)
instance.setUp() if meth is not None:
meth()
def teardown(self): def teardown(self):
instance = py.builtin._getimself(self.obj) meth = getattr(self.obj, 'tearDownClass', None)
instance.tearDown() if meth is not None:
meth()
class TestCaseFunction(py.test.collect.Function):
def startTest(self, testcase):
pass
def addError(self, testcase, rawexcinfo):
py.builtin._reraise(*rawexcinfo)
def addFailure(self, testcase, rawexcinfo):
py.builtin._reraise(*rawexcinfo)
def addSuccess(self, testcase):
pass
def stopTest(self, testcase):
pass
def runtest(self):
testcase = self.parent.obj(self.name)
testcase(result=self)

View File

@ -22,7 +22,7 @@ def main():
name='pytest', name='pytest',
description='py.test: simple powerful testing with Python', description='py.test: simple powerful testing with Python',
long_description = long_description, long_description = long_description,
version='2.0.0.dev17', version='2.0.0.dev18',
url='http://pytest.org', url='http://pytest.org',
license='MIT license', license='MIT license',
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

View File

@ -81,3 +81,25 @@ def test_module_level_pytestmark(testdir):
""") """)
reprec = testdir.inline_run(testpath, "-s") reprec = testdir.inline_run(testpath, "-s")
reprec.assertoutcome(skipped=1) reprec.assertoutcome(skipped=1)
def test_class_setup(testdir):
testpath = testdir.makepyfile("""
import unittest
import py
class MyTestCase(unittest.TestCase):
x = 0
@classmethod
def setUpClass(cls):
cls.x += 1
def test_func1(self):
assert self.x == 1
def test_func2(self):
assert self.x == 1
@classmethod
def tearDownClass(cls):
cls.x -= 1
def test_teareddown():
assert MyTestCase.x == 0
""")
reprec = testdir.inline_run(testpath)
reprec.assertoutcome(passed=3)