[Patches] [ python-Patches-736962 ] Port tests to unittest (Part 2)

SourceForge.net noreply@sourceforge.net
Mon, 16 Jun 2003 08:44:14 -0700


Patches item #736962, was opened at 2003-05-13 05:45
Message generated for change (Comment added) made by rhettinger
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: Walter Dörwald (doerwalter)
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: Raymond Hettinger (rhettinger)
Date: 2003-06-16 10:44

Message:
Logged In: YES 
user_id=80475

The test file now has dependencies that do not apply to 
windows.  The failure messages are attached.

----------------------------------------------------------------------

Comment By: Walter Dörwald (doerwalter)
Date: 2003-06-16 07:48

Message:
Logged In: YES 
user_id=89016

Here's the next one: test_posixpath.py with many additional
tests.

----------------------------------------------------------------------

Comment By: Walter Dörwald (doerwalter)
Date: 2003-05-22 12:33

Message:
Logged In: YES 
user_id=89016

Checked in as:
Lib/test/output/test_mimetools delete
Lib/test/test_mimetools.py 1.4

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-05-22 11:18

Message:
Logged In: YES 
user_id=80475

test_mimetools.py is ready.

----------------------------------------------------------------------

Comment By: Walter Dörwald (doerwalter)
Date: 2003-05-22 10:05

Message:
Logged In: YES 
user_id=89016

I've attached a third version of test_mimetools.py that does
some checks for the mimetools.Message class.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-05-21 08:04

Message:
Logged In: YES 
user_id=80475

Attaching a slightly modified test_mimetools which covers 
more encodings and has a stronger set test.

----------------------------------------------------------------------

Comment By: Walter Dörwald (doerwalter)
Date: 2003-05-18 18: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-17 22: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-17 21: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-17 20: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-17 20: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-17 20: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-17 19: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-17 18: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