index.lxp@lxpwrap=x5171_252ehtm.htm
来自「GUI Programming with Python」· HTM 代码 · 共 277 行
HTM
277 行
<table border="0" cellspacing="0" cellpadding="3" width="100%"><tr><td> <div align="center" id="bldcontent"> <a href="../default.htm"><img src="../images/opendocs.png" width="63" height="76" border="0"></a> <br> <div class="symbol">Your OpenSource Publisher™</div> </div> </td></tr></table> <div align="center" class="author"> <a href="../products.lxp">Products</a> | <a href="../wheretobuy.lxp">Where to buy</a> | <a href="../bookstore.lxp">Retailers</a> | <a href="../faq.lxp">FAQ</a> | <a href="../writeforus.lxp">Write for Us.</a> | <a href="#contact">Contact Us.</a> </div> <table border="0" cellspacing="3" cellpadding="0" width="100%"><tr><td width="100%"> <div class="content"> <table border="0" cellspacing="2" cellpadding="0" width="100%"><tr><td width="100%"> <div align="center"><H4 CLASS="AUTHOR"><A NAME="AEN5">Boudewijn Rempt</A><br><a href="../../https@secure.linuxports.com/opendocs/default.htm"><img src=odpyqt125.png></a><br>ISBN: 0-97003300-4-4<br><a href="../../https@secure.linuxports.com/opendocs/default.htm">Available from bookstores everywhere or you can order it here.</a><p>You can download the source files for the book <a href="pyqtsrc.tgz">(code / eps) here.</a><hr></div> <HTML><HEAD><TITLE>Collecting tests in a test suite</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.72"><LINKREL="HOME"TITLE="GUI Programming with Python: QT Edition"HREF="book1.htm"><LINKREL="UP"TITLE="Automatic testing with PyUnit"HREF="c5064.htm"><LINKREL="PREVIOUS"TITLE="A first testcase"HREF="x5120.htm"><LINKREL="NEXT"TITLE="A more complicated test"HREF="x5202.htm"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GUI Programming with Python: QT Edition</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><A accesskey="P" href="index.lxp@lxpwrap=x5120_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 14. Automatic testing with PyUnit</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x5202_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">Collecting tests in a test suite</A></H1><P>Executing this script isn't much use. First, the test must be collected in a testsuite, and the testsuite must then be run. A testsuite is a collection of related tests, of the type <TTCLASS="CLASSNAME">TestSuite</TT>.</P><P>It is customary to create a function <TTCLASS="FUNCTION">suite()</TT> in every testfile that return a suite with all the tests the file contains. This function is then called by a <TTCLASS="CLASSNAME">TestRunner</TT> object. This will be either a <TTCLASS="FUNCTION">TextTestRunner</TT>, if you run your tests without a gui, or a gui TestRunner application, such as the <SPANCLASS="APPLICATION">QtTestRunner</SPAN>. Defining a testsuite is a simple matter of creating a <TTCLASS="CLASSNAME">TestSuite</TT> object and adding tests: </P><PRECLASS="PROGRAMLISTING">def suite(): testSuite=unittest.TestSuite() testSuite.addTest(DocviewDocTestCase()) return testSuite </PRE><P>In order to be able to execute this file it's handy to add a bit of executable code to the end:</P><PRECLASS="PROGRAMLISTING">def main(): runner = unittest.TextTestRunner() runner.run(suite())if __name__=="__main__": main() </PRE><P>Running the test will give you the satisfaction of knowing that at least one piece of your application is working:</P><PRECLASS="SCREEN">boud@calcifer:~/doc/pyqt > python ch9/dvt1.pyrunTest (__main__.DocviewDocTestCase) ...ok------------------------------------------------------------------------------Ran 1 test in 0.035sOK </PRE><P>Or, if you want to see the green and pleasant bar, you can run the gui testrunner. Enter the name of the testmodule (<TTCLASS="FILENAME">dvt1</TT> in this case) in the textbox, followed by the name of the function that returns the testsuite:</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Preparing a GUI test run.</P></DIV></P></DIV><P>Then press run, and sit back. Please note that the actual output you get might differ. The python unittesting framework is in constant development. For the screen output in this chapter, I used version 1.3.0, which is included with the other sources that belong to this book. The unittest gui has been brought up to date to the version of <TTCLASS="FILENAME">unittest.py</TT> that's included with Python 2.2.</P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><A accesskey="P" href="index.lxp@lxpwrap=x5120_252ehtm.htm">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="H" href="index.lxp@lxpwrap=book1_252ehtm">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><A accesskey="N" href="index.lxp@lxpwrap=x5202_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">A first testcase</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c5064_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">A more complicated test</TD></TR></TABLE></DIV></BODY></HTML> </td> </tr> </table> </td> </tr> </table>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?