[TIP] Test discovery for unittest

Robert Collins robertc at robertcollins.net
Sat Apr 11 07:14:03 PDT 2009


On Sat, 2009-04-11 at 15:29 +0300, Marius Gedminas wrote:
> 
> > If you look at the prototype you'll see that it skips classes that
> don't 
> > have any tests on them. If your base classes don't have any tests
> it 
> > wouldn't be a problem, if they do (why?) then you can use load_tests
> to 
> > skip them.
> 
> The "why?" cries for an explanation.  Some of the older Zope 3 code
> has
> test cases that can test any particular implementation of an
> interface.
> You have to subclass that test and override the factory method to
> supply
> the implementation you want to test.

As Michael says, load_tests can avoid this for you by letting you filter
out the test cases you don't want. But there is a better way...

In bzr we used to avoided the same issue by mixins (the mixin defined
tests but was not a TestCase subclass) until we had figured out how to
do clean test multiplication. Now we use generally use loadtests to
multiply the tests by the implementations of the interface under test. 

I wrote a separate library recently, taking into consideration what we'd
learnt doing this with bzr; its called testscenarios
(http://launchpad.net/testscenarios). 

For example, we have a VFS (which does http, https, sftp, ftp, file, bzr
+ssh etc). In test_transport_implementations.py we have:

def load_tests(standard_tests, module, loader):
    """Multiply tests for tranport implementations."""
    result = loader.suiteClass()
    scenarios = transport_test_permutations()
    return multiply_tests(standard_tests, scenarios, result)

scenarios is an iterable of (label, attributes) tuples. E.g.
[('sftp', {'transport_factory': SFTPTransportFactory}), ...]

multiply_tests deep copies all the tests its given, renames the copies
using the label so they are distinctive, and applies the attributes to
the copies, adding the resulting tests to result - its a
DependencyInjection pattern, for folk that like naming things :).

-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/20090412/96ead53f/attachment.pgp 


More information about the testing-in-python mailing list