📄 index.lxp@lxpwrap=x5234_252ehtm.htm
字号:
<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>Large projects</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 more complicated test"HREF="x5202.htm"><LINKREL="NEXT"TITLE="Testing signals and slots"HREF="x5255.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=x5202_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=x5255_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">Large projects</A></H1><P>In large projects, where you have many tests, you will want to automate the assembly of testsuite as much as possible. By creating a few Python scripts that work with standardized testsuites (e.g., the function that returns the testsuite is always <TTCLASS="FUNCTION">module.suite()</TT>), you can run all tests as often as you want to.</P><P>You can already nest testsuites out of the box, and by creating a master test class that reads a configuration file and constructs a master test-suite, you can test a whole system in one run.</P><P>Take the following definition file, for instance:</P><PRECLASS="PROGRAMLISTING">## unittests - unittests for the whole system.## dvt1 tests the creation of a docviewdoc#dvt1.suite# dvt2 tests a whole lot moredvt2.suite </PRE><P>If you use the following script, then all tests that are defined in the form of module.function, where module is on the Python path and function returns a <TTCLASS="CLASSNAME">TestSuite</TT> object, will be combined in one mega-<TTCLASS="CLASSNAME">TestSuite</TT>.</P><PRECLASS="PROGRAMLISTING">## systemtest.py - run all tests that are not commented out in unittests#import unittestdef suite(): testSuite=unittest.TestSuite() f=open("unittests") for t in f.readlines(): t=t.strip() # remove all whitespace if t[0]!="#": # a comment testSuite.addTest(unittest.createTestInstance(t)) return testSuitedef main(): runner = unittest.TextTestRunner() runner.run(suite())if __name__=="__main__": main() </PRE><P>Note the use of the function <TTCLASS="FUNCTION">unittest.createTestInstance</TT>, which can create a testcase or testsuite from a simple string. There's an optional second argument, module, which points to the module where the test can be found.</P><P>Another function, <TTCLASS="FUNCTION">unittest.makeSuite()</TT> can scan a class for functions that begin with a certain prefix, and combine them into a testsuite. For instance, we could rewrite <TTCLASS="FILENAME">dvt2.py</TT> into:</P><PRECLASS="PROGRAMLISTING">## dvt3.py - using makeSuite#import sysimport unittestfrom docviewdoc import DocviewDocdef divide(a, b): return a/bclass DocviewDocTestCase(unittest.TestCase): """DocviewDocTestCase test the DocviewDoc class. """ def checkInstantion(self): """Check whether the document could be instantiated""" doc=None doc=DocviewDoc() assert doc!=None, 'Could not instantiate DocviewDoc' def checkModifiable(self): """Check whether the document could be modified""" doc=DocviewDoc() doc.slotModify() assert doc.isModified(), 'Document could not be modified'def suite(): testSuite=unittest.makeSuite(DocviewDocTestCase, "check") return testSuitedef main(): runner = unittest.TextTestRunner() runner.run(suite())if __name__=="__main__": main() </PRE><P>By always prefixing your tests with ‘check', you make sure they are all included. If you had to add every test by hand, it would be only natural to forget one or two over time. Eventually you would notice that a test was not being executed. By that time you might have changed the tested code so the original test fails. The purpose of unit testing is always to be sure that everything works as you think it should.</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=x5202_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=x5255_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">A more complicated test</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">Testing signals and slots</TD></TR></TABLE></DIV></BODY></HTML> </td> </tr> </table> </td> </tr> </table>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -