[TIP] using py.test's setup_class with unittest tests?

holger krekel holger at merlinux.eu
Tue Nov 23 15:27:45 PST 2010


Hi Hans,

On Tue, Nov 23, 2010 at 09:46 -0800, Hans Sebastian wrote:
> Hello testing in python members,
> 
> I need some help related to py.test and unittest. I have tests written and
> run using unittest and now I also want py.test to be able to run it. The
> problem is at the beginning of each suite in a test script there is a setup
> that needs to be done. I need this setup function to be called as well when
> run with py.test.
> 
> To illustrate my problem, I have a test script below and my goal is to be
> able to run custom_setup() when run with unittest or py.test. The problem is
> setup_class is not getting called. We use python 2.6.4 and py-1.2.1. Can
> this be done? Thank you.

Neither Python-2.6.4 nor py-1.2.1 support your example usage. You can 
experimentally install the upcoming 2.0 release like this:

    pip install -i http://pypi.testrun.org -U pytest
    # or
    easy_install -i http://pypi.testrun.org -U pytest

and then your example should work as pytest-2.0 has improved unittest integration
report (is e.g. known to work with Django and trial TestCases much better now).
   
cheers,
holger
     
> -hans
> 
> import logging
> import unittest
> logging.getLogger()
> 
> def custom_setup():
>     logging.info('custom setup')
> 
> class TestSimple(unittest.TestCase):
> 
>     def setup_class(cls):
>         logging.info('setup_class')
>         custom_setup()
> 
>     def setUp(self):
>         logging.info('setUp')
> 
>     def test_1(self):
>         logging.info('test_1')
> 
>     def test_2(self):
>         logging.info('test_2')
> 
> if __name__ == '__main__':
>     custom_setup()
>     suite = unittest.TestLoader().loadTestsFromTestCase(TestSimple)
>     unittest.TextTestRunner(verbosity=2).run(suite)

> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python


-- 



More information about the testing-in-python mailing list