Since any PyQt widget, including any dialog, can be used as a top-level window in its own right, it is easy to test a dialog by instantiating it and starting the event loop.* Often, though, we need to do a bit more. For example, we may need to set up some initial data, or provide methods to receive the dialog's signals so that we can see that they are working correctly.
In the case of the Find and Replace dialog, we need some initial text, and we need to check that the connections work and that the find and replace methods work.
So, at the end of the findandreplacedlg.py file, we have added some extra code. This code is executed only if the file is run stand-alone, so it does not affect performance or interfere with the use of the dialog when it is used in an application.
import sys text = US experience shows that, unlike traditional patents, software patents do not encourage innovation and R&D, quite the contrary. In particular they hurt small and medium-sized enterprises and generally newcomers in the market. They will just weaken the market and increase spending on patents and litigation, at the expense of technological innovation and research. Especially dangerous are attempts to abuse the patent system by preventing interoperability as a means of avoiding competition with technological ability. — Extract quoted from Linus Torvalds and Alan Cox's letter
★When using pyuic4 we can specify a command-line option of -x to get the dialog generated with a bit of extra code so that it can be tested stand-alone.
to the President of the European Parliament http://www.effi.org/patentit/patents_torvalds_cox.html
print "Found at %d" % where def nomore():
print "No more found"
app = QApplication(sys.argv)
form = FindAndReplaceDlg(text)
form.connect(form, SIGNAL("found"), found)
form.connect(form, SIGNAL("notfound"), nomore)
We begin by importing the sys module, and then we create a piece of text to work on. Next, we create a couple of simple functions for the dialog's signals to be connected to.
We create the QApplication object in the normal way, and then we create an instance of our dialog, passing it our test text. We connect the dialog's two signals to our slots, and call show(). Then we start off the event loop. When the event loop terminates we print the dialog's text: This will be different from the original text if the user replaced some text.
The dialog can now be run from a console and tested.
Unless using automated testing tools, it is often helpful to add testing functionality to dialogs. It does not take too much time or effort to write them, and running them whenever a change is made to the dialog's logic will help minimize the introduction of bugs.
Sometimes we pass complex objects to dialogs that may appear to make testing impossible. But thanks to Python's duck typing we can always create a fake class that simulates enough behavior to be usable for testing. For example, in Chapter 12, we use a property editor dialog. This dialog operates on "Node" objects, so in the testing code we create a FakeNode class that provides the methods for setting and getting a node's properties that the dialog makes use of. (The relevant files are chap12/propertiesdlg.ui, from which ui_propertiesdlg.py is generated, and chap12/propertiesdlg.py where the PropertiesDlg is implemented.)
Was this article helpful?
Download Tube Jacker And Discover Everything You Need To Know About Jacking Unlimited Traffic From The Video Giant. The drop dead easy way to create winning video campaigns that will FLOOD your website with unstoppable FREE traffic, all on complete and total autopilot. How to exploit a sneaky method of boosting exposure and getting your videos to the top of Google within 72 hours, guaranteed.