[PyQt] unittestign a PyQt app.

Ian hobson42 at gmail.com
Sun Dec 26 22:28:17 GMT 2010


Hi all,

I need some advice. I am trying to create a unittest script for a text 
edior, that

a) changes the text so the buffer needs saving
b) Clicks the cross in the top corner of the window
c) Detects that a modal windwo appears title "Text Editor - Unsaved 
Changes"
d) Find and click on the "Yes" button
e) Check that both windows has been closed and no longer exist
f) reads the file to prove the change was saved.

What I have so far is:

import os
import sys
import unittest
from PyQt4 import QtTest
from PyQt4.QtCore import *   # so no internal routines may start  with Q!
from PyQt4.QtGui import *
sys.path.insert(1,os.path.abspath('..'))

from cubic import Cubic

class testCubic(unittest.TestCase):

# three tests removed
     def test004_alterTextInDocument(self):
         ''' load the cubicNotes.txt document and add something and close.
             check it prompts to save '''
         global app
         f = open('cubicNotes.txt', 'w')
         f.write("Line one\n")
         f.close()
         cub = Cubic()
         co = cub.noterNew()
         co.editor.append("A new line of text\n")
         co.close()
         popup = app.activeModalWidget()
         #self.assertEqual(popup.windowTitle(),'Text Editor - Unsaved 
Changes')
         # popup.buttonShowing('Yes').click()

and the main routine is

# coding=utf8
#
#   testAll.py - run all tests.
import unittest
import sys
from PyQt4.QtGui import *
from testCubic import testCubic
from testCompanyListModel import testCompanyListModel
from testCompany import testCompany
from testCompanyWindow import testCompanyWindow
global app

def main():
     suite = unittest.TestLoader()
     #suite.loadTestsFromName(testCompanyListModel)
     #suite.loadTestsFromModule(testCompany)
     #suite.loadTestsFromModule(testCompanyWindow)
     suite.loadTestsFromTestCase(testCubic)
     unittest.TextTestRunner().run(suite)

if __name__=="__main__":
     global app
     app = QApplication(sys.argv)
     app.setWindowIcon(QIcon(":/icon.png"))
     unittest.main()

The problems I have hit so far:

1) There appears to be no way to load just SOME of the tests. The 
testCompanyListModel,
testCompany and testCompanyWindow tests are run anyway!

Why? I've tried loadTestsFromTestCase, loadTestsFromModule and 
loadTestsFromName aln they all appear to run all the tests.

2) The append does not append, but replaces the text.

Append should - well append. No?

3) As written the close() closes the window waiting for user input. The 
script continues after
I click on the "Yes" button . How can I get the screen updated and the 
script to continue after updating.

4) app is undefined - however it is declared as global both here and in 
the start up routine.

If I can't get app, how can I find the pop-up window?

5) How can I can find the "yes" button, when I have the pop-up window - 
and then click it?

Help on any question gratefully received.

If the excess of problem means I am approaching this is the wrong way, 
what is the correct way?

Thanks

Ian


More information about the PyQt mailing list