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

Hi all,

I have a unittest question for which my current solution looks very ugly.  I
need to build some test classes based on a subclass of TestCase:

# pseudo-code follows:

class testBase(TestCase): pass

class test1(testBase): pass

class test2(testBase): pass

Now I want to build a test suite which uses all the test* methdods in test1
and test2, but NOT in testBase. testBase is meant to be used only to
provide common functionality to other test classes, not to be used by
itself.

Further, I want to have a way of using unittest.main() to call this test
suite.

I tried

mysuite = makeSuite(test1)
mysuite.addTests(makeSuite(test2))

but that doesn't work. Furthermore, main() seems to want to run _all_
subclasses of TestCase.

My solution was to make testBase a mix-in which doesn't inherit from
TestCase so that it 'flies under the radar' of main(), but I find that
rather kludgy:

class testBase(): pass

class test1(testBase,TestCase): pass

class test2(testBase,TestCase): pass

Are there any better solutions?  I read the code for unittest.main() and I
guess I can just re-implement it myself by putting just what I want in, but
I'm sure what I'm trying to accomplish is routine enough that a standard
solution must exist.

Thanks in advance for any input.

Cheers,

f.



Sun, 31 Jul 2005 07:19:22 GMT  
 unittest: how to specify which suites to run with unittest.main()?

Quote:
> Now I want to build a test suite which uses all the test* methdods
> in test1 and test2, but NOT in testBase. testBase is meant to be
> used only to provide common functionality to other test classes, not
> to be used by itself.

1.  Don't name any method of testBase test*--or am I missing something?

and/or:

2.  Put testBase in a separate module and import it.

// m
-



Sun, 31 Jul 2005 07:44:59 GMT  
 unittest: how to specify which suites to run with unittest.main()?

Quote:

>> Now I want to build a test suite which uses all the test* methdods
>> in test1 and test2, but NOT in testBase. testBase is meant to be
>> used only to provide common functionality to other test classes, not
>> to be used by itself.

> 1.  Don't name any method of testBase test*--or am I missing something?

Well, that doesn't work because I want to run those methods, but only from
the test1/test2 instances.  These guys override the setUp() method to
configure the tests with different parameters, but otherwise need to run
exactly the same tests.  So having them inherit all the test* methods from
testBase actually makes perfect sense: they inherit all the tests,
configure the setUp() call differently, and go on happily.  So that
solution doesn't work.

Quote:
> 2.  Put testBase in a separate module and import it.

I thought of doing this, but it seems unnecessarily ugly. I like to have a
single test.py file in my library code, and it would seem to me that there
should be a way to tell unittest.main() exactly which test suite to run.

Thanks for the input, though.  I'm very new at this unittest business, so
all ideas are appreciated.

Regards,

f.



Sun, 31 Jul 2005 08:21:47 GMT  
 unittest: how to specify which suites to run with unittest.main()?

Quote:


> > 1.  Don't name any method of testBase test*--or am I missing something?

> Well, that doesn't work because I want to run those methods, but only from
> the test1/test2 instances.  These guys override the setUp() method to
> configure the tests with different parameters, but otherwise need to run
> exactly the same tests.  So having them inherit all the test* methods from
> testBase actually makes perfect sense: they inherit all the tests,
> configure the setUp() call differently, and go on happily.  So that
> solution doesn't work.

I think the mix-in approach you gave is perfectly fine.  That is exactly
what multiple inheritence is for.  test1 and test2 ARE both a testBase
and a TestCase, and a TestCase is by definition what you want unittest
to run.

I'd call it _testBase, or something, to indicate it is supposed to be
subclassed (and so that .

ie:

class _testBase(): pass

class test1(_testBase,TestCase): pass

class test2(_testBase,TestCase): pass



Sun, 31 Jul 2005 09:17:26 GMT  
 unittest: how to specify which suites to run with unittest.main()?

Quote:

> 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')

-Peter



Sun, 31 Jul 2005 21:44:04 GMT  
 unittest: how to specify which suites to run with unittest.main()?

Quote:

>> 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.



Mon, 01 Aug 2005 01:29:59 GMT  
 unittest: how to specify which suites to run with unittest.main()?

Quote:

>> > 1.  Don't name any method of testBase test*--or am I missing something?

>> Well, that doesn't work because I want to run those methods, but only
>> from
>> the test1/test2 instances.  These guys override the setUp() method to
>> configure the tests with different parameters, but otherwise need to run
>> exactly the same tests.  So having them inherit all the test* methods
>> from testBase actually makes perfect sense: they inherit all the tests,
>> configure the setUp() call differently, and go on happily.  So that
>> solution doesn't work.

> I think the mix-in approach you gave is perfectly fine.  That is exactly
> what multiple inheritence is for.  test1 and test2 ARE both a testBase
> and a TestCase, and a TestCase is by definition what you want unittest
> to run.

Thanks for your input, but I think I'll go with the solution proposed by
Peter Hansen.  It allows me to build my classes with a cleaner structure
(no unnecessary mixins which add typing and clutter), and to tell
unittest.main() exactly what I want it to run.

Best regards,

Fernando.



Mon, 01 Aug 2005 01:32:12 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Need help Translating Python unittest to Ruby Test::Unit

2. The various UnitTest frameworks.

3. Windows python 2.2.1 IDLE glitch - unittest output display nothing

4. problems redirecting unittest output to sys.stdout

5. print unittest errors immediately

6. Code Coverage support for unittest?

7. Tkinter and unittest

8. unittest / pdb tricks?

9. A modest suggestion for unittest.py

10. Standard module unittest, where has unittestgui gone?

11. Notes on unittest.py

12. doctest -> unittest

 

 
Powered by phpBB® Forum Software