unittest: how to specify which suites to run with unittest.main()?

Fernando Perez fperez528 at yahoo.com
Wed Feb 12 12:29:59 EST 2003


Peter Hansen wrote:

>> mysuite = makeSuite(test1)
>> mysuite.addTests(makeSuite(test2))
>> 
>> but that doesn't work. Furthermore, main() seems to want to run _all_
>> subclasses of TestCase.
> 
> Here's a slightly modified example from work which I hope will help you
> with the solution.  Note that the group here chose to make the test
> functions start with "check" instead of "test" but I believe you
> will have no trouble translating to your own situation.
> 
> def suite():
>     return unittest.TestSuite((
>         unittest.makeSuite(SetupFileImportTestCase, 'check'),
>         unittest.makeSuite(FunctionTestCase, 'check'),
>         ))
> 
> 
> if __name__ == '__main__':
>     unittest.main(defaultTest='suite')

YES! Thanks a lot.  I had tried variations on this one without any success, 
my problem had been not realizing that the value given defaultTest needs to 
be the _name_ of the suite as a _string_, not the suite object itself.  
Unfortunately the unittest documentation is rather terse and even the code 
doesn't specify in docstrings what the parameters are meant to be.  

The docs for main() simply say:

/begin doc paste
main([module[, defaultTest[, argv[, testRunner[, testRunner]]]]])
    A command-line program that runs a set of tests; this is primarily for 
making test modules conveniently executable. The simplest use for this 
function is:

if __name__ == '__main__':
    unittest.main()
/end doc paste

No mention of what all the arguments to main() should be and what they are 
for.  And looking through the source didn't make immediately obvious to me 
what the right way to use this was.

But as always, c.l.py saves the day :)

Thanks again.

Regards,

Fernando.




More information about the Python-list mailing list