[TIP] Meta-test methods...

Douglas Philips dgou at mac.com
Thu Apr 23 21:01:35 PDT 2009


On 2009 Apr 23, at 9:45 AM, C. Titus Brown wrote:

> On Thu, Apr 23, 2009 at 09:06:42AM -0400, Douglas Philips wrote:
> -> On or about 2009 Apr 23, at 1:14 AM, C. Titus Brown indited:
> -> > Anyway, I double-dare you to make this:
> -> >
> -> > 	http://github.com/ctb/figleaf/blob/d11d2ee0ce70427b229af15409bff2ab23e090d2/tests/test_regress/__init__.py
> -> >
> -> > work nicely without decorators.
> ->
> -> ??? With decorators, you mean?
>
> Sorry: generators.
>
> --titus


Anyway, I double-dare you pay attention to the issue at hand. :)
I never said generators were bad. I said that checking a test method  
for being a generator was a hack, seeminly cool at first, but upon  
further thought it is just as a proof of concept that it is useful to  
have a way to dynamically generate a sequence of test methods/functions.
Testing only for generators is both opaque and, critically, fails to  
generalize to arbitrary python sequences.


And since I don't find generators to be bad, this is somewhat moot,  
however it amused me to reply anyways...

As to your dare dare, the only generator I found in that file was this  
one:

def test_source_generator():
     for filename in os.listdir(testdir):
         if filename.startswith('tst') and filename.endswith('.py'):
             yield compare_coverage, filename


It is rather foolish to post my version, since you have not disclosed  
your criteria of "nicely"...
none-the-less, off the cuff, I find this to be slightly nicer. And  
while you don't state your criteria, I will say that I think this is - 
slightly- nicer because it reduces the level of nesting (Flatter is  
better than nested) and gives meaningful names to the concepts involved.

def test_source_generator():
     is_test_file = lambda name: name.startswith('tst') and  
name.endswith('.py')
     pair_up = lambda name: (compare_coverage, file)
     return map(pair_up, filter(is_test_file, os.listdir(testdir))


--Doug




More information about the testing-in-python mailing list