unit testing, setUp and scoping

Francisco Souza francisco at franciscosouza.net
Wed Apr 14 11:22:24 EDT 2010


>
> On Wed, Apr 14, 2010 at 11:47 AM, john maclean <jayeola at gmail.com> wrote:
> Can one use the setUp block to store variables so that they can be
> used elsewhere in unit tests? I'm thinking that it's better to have
> variables created in another script and have it imported from within
> the unit test
>

Hi John,
each TestCase is a object, and you can "store" attributes in this objects
normally, using self :)

class TestPythonStringsTestCase(
>
> unittest.TestCase):
>        def setUp(self):
>                print '''setting up stuff for ''', __name__
>                *self.*s1 = 'single string'
>                print dir(str)
>
>        def testclass(self):
>                '''test strings are of class str'''
>                self.assertEqual(*self.*s1.__class__, str)
>
> if __name__ == "__main__":
>        unittest.main()


This works fine and s1 is an internal attribute of your TesteCase.

Best regards,
Francisco Souza
Software developer at Giran and also full time
Open source evangelist at full time

http://www.franciscosouza.net
Twitter: @franciscosouza
(27) 8128 0652
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100414/2e256dc8/attachment.html>


More information about the Python-list mailing list