index.lxp@lxpwrap=x5120_252ehtm.htm
来自「GUI Programming with Python」· HTM 代码 · 共 348 行
HTM
348 行
<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>A first testcase</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="Starting out"HREF="x5102.htm"><LINKREL="NEXT"TITLE="Collecting tests in a test suite"HREF="x5171.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=x5102_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=x5171_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">A first testcase</A></H1><P>A testcase is a single class that contains one or more methods that perform a single check each. It is possible to check two different behaviors in one test method, but that's a particularly bad idea— if the test fails, you won't know exactly what went wrong, and where.</P><P>The example class we want to test, <TTCLASS="FILENAME">DocviewDoc</TT>, is to show a very simple behavior: it must be creatable, and it must know whether it is modified. That makes for two tests. </P><P>To create a test, we have to subclass the <TTCLASS="CLASSNAME">unittest.TestCase</TT> class. This class provides some methods for the setting up and destroying of a test environment and a default method, <TTCLASS="FUNCTION">runTest</TT>, that can contain the testing code. A very simple test is defined as follows:</P><PRECLASS="PROGRAMLISTING">class SimpleTest(unittest.TestCase): def runTest(self): assert 1=2, 'One is not two' </PRE><P>We want to check whether the <TTCLASS="CLASSNAME">DocviewDoc</TT> class can be instantiated. This is not as trivial and silly as it may sound: a Python class constructor can have a great and variable number of arguments, and keeping classes instantiatable is quite important. First, let's define a testcase:</P><PRECLASS="PROGRAMLISTING">## dvt1.py - a simple test of instantiating a document#import unittestfrom docviewdoc import DocviewDocclass DocviewDocTestCase(unittest.TestCase): """DocviewDocTestCase test the DocviewDoc class. """ def setUp(self): print "setUp called" def tearDown(self): print "tearDown called" def runTest(self): """Check whether the document could be instantiated""" doc=None doc=DocviewDoc() assert doc!=null, 'Could not instantiate DocviewDoc' </PRE><P>Adding a docstring, always a good idea, is particularly useful for <TTCLASS="CLASSNAME">TestCase</TT> classes, since this text will be available through the <TTCLASS="FUNCTION">shortDescription()</TT> function. It will be displayed when the test is run - either on the command line or in the gui.</P><P>The <TTCLASS="FUNCTION">setUp</TT> method of the <TTCLASS="CLASSNAME">TestCase</TT> class is executed before <TTCLASS="FUNCTION">runTest</TT>, and can be used to prepare the environment. The <TTCLASS="FUNCTION">tearDown</TT> method is called after the test has run, whether successfully or not. You can use it to clear away garbage that is left by the test. On the other hand, if <TTCLASS="FUNCTION">setUp</TT> fails, the entire test will not run. I have included the functions here; you don't have to write them if you don't need them.</P><P>The test itself is very simple. First, we give the variable <TTCLASS="VARNAME">doc</TT> a known value, namely None. After that, a <TTCLASS="CLASSNAME">DocviewDoc</TT> is instantiated. Using Python's standard <TTCLASS="FUNCTION">assert</TT> statement, a check is made to see whether the variable <TTCLASS="VARNAME">doc</TT> still points to None—if so, instantiation failed. </P><P>Note that use of <TTCLASS="FUNCTION">assert</TT> means that running unit tests with optimized bytecode (<TTCLASS="FILENAME">.pyo</TT> files) is useless. Compiling Python to optimized bytecode removes all traces of asserts (and the line-numbers that can be used for stack traces). While the unittest framework includes a special, home-brew, <TTCLASS="FUNCTION">assert</TT> function that isn't affected by optimized compilation, it is still better to test plain Python. The stacktraces are far more useful, and additionally, the unittest assert is less convenient.</P><P>Python's <TTCLASS="FUNCTION">assert</TT> statement is very handy, and quite simple. It takes the form of:</P><PRECLASS="PROGRAMLISTING"> assert expression, message </PRE><P>Where the message will be printed and an AssertionError raised when the expression turns out to be false. <TTCLASS="FUNCTION">assert</TT> is a statement, not a function. This means that you shouldn't encase the expression and the message in brackets. If the statement is too long for the line, you can use a backslash as a line-continuation.</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=x5102_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=x5171_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Starting out</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">Collecting tests in a test suite</TD></TR></TABLE></DIV></BODY></HTML> </td> </tr> </table> </td> </tr> </table>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?