Help needed with unittest and global

Dave Angel davea at ieee.org
Sun Jan 9 14:53:46 EST 2011


On 01/-10/-28163 02:59 PM, Ian Hobson wrote:
> Hi all,
>
> I am trying to develop a PyQt application, and I want to unittest it.
>
> After three false starts, I am plainly unaware of something rather basic
> - can some kind person please help me out?
>
> This is a log to show what I have so far
> D:\work\ian>type testAll.py
> # coding=utf8
> # testAll.py - run all tests.
> import unittest
> import sys
> from PyQt4.QtGui import *
> from testCubic import testCubic
> global app
> def main():
> suite = unittest.TestLoader()
> suite.loadTestsFromTestCase(testCubic)
> unittest.TextTestRunner().run(suite)
> if __name__=="__main__":
> global app
> app = QApplication(sys.argv) # set gloabl app
> unittest.main()
>
> D:\work\ian>type testCubic.py
> # coding=utf8
> # testCubic.py - tests the top level module
> import unittest
> global app
> class testCubic(unittest.TestCase):
> def test001_walkingSkeleton(self):
> global app # use global version
> app.processEvents() # fails
> D:\work\ian>python testAll.py
> E
> ======================================================================
> ERROR: test001_walkingSkeleton (testCubic.testCubic)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
> File "D:\work\ian\testCubic.py", line 8, in test001_walkingSkeleton
> app.processEvents() # fails
> NameError: global name 'app' is not defined
>

You have two global variables called app, one is in the testCubic module 
and the other is in the testAll.py script.  They do not automatically 
refer to the same thing.

To use a global from another module, you can either do an import, or you 
can pass it as an argument.  But beware of mutual imports.

DaveA




More information about the Python-list mailing list