[TIP] How do I run the same test on many different classes?

Robert Collins robertc at robertcollins.net
Sun Dec 13 14:33:15 PST 2009


On Sun, 2009-12-13 at 20:32 +0000, Michael Foord wrote:
> 
> I believe that the Bzr guys (?) do this by copying TestCase classes.

https://edge.launchpad.net/testscenarios

This is a modular form of what we do in bzr.

TaskN are implementations of Task to be injected into the TestTask
tests.

http://bazaar.launchpad.net/~lifeless/testscenarios/trunk/annotate/head%
3A/README

Which has docs including how to hook this in with python 2.7(3.1) using
load_tests, or by using the test_suite idiom to define your tests.

As a short demo here is a minimal scenario testing that str and unicode
are both implementations of basestring. See after the file for info on
running it.

test_task.py:
=============
import unittest
from testscenarios.scenarios import generate_scenarios

#include your types here
Task1=str
Task2=unicode
Task=basestring

# boilerplate
def test_suite(): 
    loader = unittest.TestLoader()
    tests = loader.loadTestsFromName(__name__)
    result = loader.suiteClass()
    result.addTests(generate_scenarios(tests))
    return result

#actual tests
class TestTask(unittest.TestCase):

    scenarios = [
        ('Task1', dict(impl=Task1)),
        ('Task2', dict(impl=Task2)),]

    def test_can_construct(self):
        task = self.impl()
        self.assertTrue(isinstance(task, Task))

=============

Now, I could swear you used to be able to do:
$ python -m unittest test_task.test_suite

But it now breaks. see http://bugs.python.org/issue7501 for the issue,
http://twistedmatrix.com/trac/ticket/4168 for the trial equivalent, and
nosetests helpfully runs the test_suite function as a test, and direct
constructs the test object without parameters :(. I haven't filed a
nosetests bug for this at this point.

However, testtools and subunit can both run the demo:
$ python -m testtools.run test_task.test_suite
For testtools you need the branch I added this to while doing some
cleanup yesterday: lp:~lifeless/testtools/run/

Anyhow, regardless of how you arrange to run it, the result of the
test_suite() function is the test suite to run.

E.g., I get:
$ python -m testtools.run test_task.test_suite
Tests running...
Ran 2 tests in 0.000s

OK

or

$ python -m subunit.run test_task.test_suite | subunit-stats
Total tests:       2
Passed tests:      2
Failed tests:      0
Skipped tests:     0
Seen tags: 

Cheers,
Rob
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20091214/02c6c958/attachment.pgp>


More information about the testing-in-python mailing list