[Patches] [ python-Patches-736962 ] Port tests to unittest (Part 2)
SourceForge.net
noreply@sourceforge.net
Sun, 18 May 2003 16:51:07 -0700
Patches item #736962, was opened at 2003-05-13 12:45
Message generated for change (Settings changed) made by doerwalter
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=736962&group_id=5470
Category: Tests
Group: None
Status: Open
Resolution: Accepted
Priority: 5
Submitted By: Walter Dörwald (doerwalter)
>Assigned to: Raymond Hettinger (rhettinger)
Summary: Port tests to unittest (Part 2)
Initial Comment:
Here are the next test scripts ported to PyUnit:
test_winsound and test_array. For test_array many
additional tests have been added (code coverage is at 91%)
----------------------------------------------------------------------
Comment By: Walter Dörwald (doerwalter)
Date: 2003-05-19 01:46
Message:
Logged In: YES
user_id=89016
Agreed, this is too much magic for too little gain.
Back to business: Here is test_mimetools ported to PyUnit.
Tests for mimetools.Message are still missing. If you can
think of any tests please add them.
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2003-05-18 05:18
Message:
Logged In: YES
user_id=80475
Get the module with sys.modules:
tests = test_support.findtestclasses(sys.modules
[__name__])
test_support.unittest(*tests)
Yeah, the inheritance thing is a problem. I was trying to
avoid having to modify unittest.TestCase to have a
metaclass. The control of the module is kept in a
separate SF project and one of its goals is to be backward
compatible through 1.5.2 (meaning no metaclasses).
A possible workaround is to define a modified testcase in
test_support so that people don't import unittest directly
anymore:
test_support.py
-------------------------
import unittest
class SmartTestCase(unittest.TestCase):
__metaclass__ = autotracktests
pass
test_sets.py
------------------
class TestBasicOps(test_support.SmartTestCase):
run = False
. . .
class TestBasicOpsEmpty(TestBasicOps):
def setUp(self):
. . .
Still, this is starting to seem a bit magical and tricky.
----------------------------------------------------------------------
Comment By: Walter Dörwald (doerwalter)
Date: 2003-05-18 04:52
Message:
Logged In: YES
user_id=89016
But how do I pass the module object from inside the module?
And skipping abstract classes seems to be more work in this
version: If skipping is done via a class attribute, derived
classes have to explicitely reset this flag because of
interitance.
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2003-05-18 03:59
Message:
Logged In: YES
user_id=80475
Good call.
Instead of using metaclasses, perhaps add a module
introspector function to test_support:
def findtestclasses(mod):
tests = []
for elem in dir(mod):
member = getattr(mod, elem)
if type(member) != type: continue
if issubclass(member, unittest.TestCase):
tests.append(member)
return tests
----------------------------------------------------------------------
Comment By: Walter Dörwald (doerwalter)
Date: 2003-05-18 03:45
Message:
Logged In: YES
user_id=89016
But this can be solved with a special non-inheritable class
attribute:
class BaseTest(unittest.TestCase):
run = False
Then the metaclass can do the following:
def __new__(cls, name, bases, dict):
if "run" not in dict:
dict["run"] = True
cls = type.__new__(cls, name, bases, dict)
if cls.run:
tests.append(cls)
return cls
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2003-05-18 03:04
Message:
Logged In: YES
user_id=80475
I don't think metaclasses or module introspection would
help whenever there are classes that derive from TestCase
but are not meant to be run directly (their subclasses have
the setup/teardown/or class data). test_sets.py has
examples of that kind of thing.
----------------------------------------------------------------------
Comment By: Walter Dörwald (doerwalter)
Date: 2003-05-18 02:50
Message:
Logged In: YES
user_id=89016
Checked in as:
Lib/test/test_array.py 1.20
Lib/test/test_winsound.py 1.5
Lib/test/output/test_winsound delete
> The approach of using tests.append() is elegant and
> makes it easier to verify that no tests are being omitted.
The most elegant approach would probably be a metaclass that
collects all TestCase subclasses that get defined. Classes
that only serve as a base class could be skipped by
specifying a certain class attribute.
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2003-05-18 01:35
Message:
Logged In: YES
user_id=80475
The approach of using tests.append() is elegant and
makes it easier to verify that no tests are being omitted.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=736962&group_id=5470