<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <link rel="STYLESHEET" href="lib.css" type='text/css' /> <link rel="SHORTCUT ICON" href="../icons/pyfav.gif" /> <link rel='start' href='../index.html' title='Python Documentation Index' /> <link rel="first" href="lib.html" title='Python Library Reference' /> <link rel='contents' href='contents.html' title="Contents" /> <link rel='index' href='genindex.html' title='Index' /> <link rel='last' href='about.html' title='About this document...' /> <link rel='help' href='about.html' title='About this document...' /> <LINK rel="prev" href="testresult-objects.html"> <LINK rel="parent" href="module-unittest.html"> <LINK rel="next" href="module-test.html"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta name='aesop' content='information' /> <META name="description" content="TestLoader Objects "> <META name="keywords" content="lib"> <META name="resource-type" content="document"> <META name="distribution" content="global"> <title>5.3.8 TestLoader Objects </title> </head> <body> <DIV CLASS="navigation"> <div id='top-navigation-panel'> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td class='online-navigation'><a rel="prev" title="5.3.7 TestResult Objects" href="testresult-objects.html"><img src='../icons/previous.png' border='0' height='32' alt='Previous Page' width='32' /></A></td> <td class='online-navigation'><a rel="parent" title="5.3 unittest " href="module-unittest.html"><img src='../icons/up.png' border='0' height='32' alt='Up One Level' width='32' /></A></td> <td class='online-navigation'><a rel="next" title="5.4 test " href="module-test.html"><img src='../icons/next.png' border='0' height='32' alt='Next Page' width='32' /></A></td> <td align="center" width="100%">Python Library Reference</td> <td class='online-navigation'><a rel="contents" title="Table of Contents" href="contents.html"><img src='../icons/contents.png' border='0' height='32' alt='Contents' width='32' /></A></td> <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' border='0' height='32' alt='Module Index' width='32' /></a></td> <td class='online-navigation'><a rel="index" title="Index" href="genindex.html"><img src='../icons/index.png' border='0' height='32' alt='Index' width='32' /></A></td> </tr></table> <div class='online-navigation'> <b class="navlabel">Previous:</b> <a class="sectref" rel="prev" href="testresult-objects.html">5.3.7 TestResult Objects</A> <b class="navlabel">Up:</b> <a class="sectref" rel="parent" href="module-unittest.html">5.3 unittest </A> <b class="navlabel">Next:</b> <a class="sectref" rel="next" href="module-test.html">5.4 test </A> </div> <hr /></div> </DIV> <!--End of Navigation Panel--> <H2><A NAME="SECTION007380000000000000000"><!--x--></A><A NAME="testloader-objects"><!--z--></A> <BR> 5.3.8 TestLoader Objects </H2> <P> The <tt class="class">TestLoader</tt> class is used to create test suites from classes and modules. Normally, there is no need to create an instance of this class; the <tt class="module"><a href="module-unittest.html">unittest</a></tt> module provides an instance that can be shared as the <code>defaultTestLoader</code> module attribute. Using a subclass or instance would allow customization of some configurable properties. <P> <tt class="class">TestLoader</tt> objects have the following methods: <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-1071' class="method">loadTestsFromTestCase</tt></b>(</nobr></td> <td><var>testCaseClass</var>)</td></tr></table></dt> <dd> Return a suite of all tests cases contained in the <tt class="class">TestCase</tt>-derived class <tt class="class">testCaseClass</tt>. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-1072' class="method">loadTestsFromModule</tt></b>(</nobr></td> <td><var>module</var>)</td></tr></table></dt> <dd> Return a suite of all tests cases contained in the given module. This method searches <var>module</var> for classes derived from <tt class="class">TestCase</tt> and creates an instance of the class for each test method defined for the class. <P> <span class="warning"><b class="label">Warning:</b> While using a hierarchy of <tt class="class">Testcase</tt>-derived classes can be convenient in sharing fixtures and helper functions, defining test methods on base classes that are not intended to be instantiated directly does not play well with this method. Doing so, however, can be useful when the fixtures are different and defined in subclasses.</span> </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-1073' class="method">loadTestsFromName</tt></b>(</nobr></td> <td><var>name</var><big>[</big><var>, module</var><big>]</big>)</td></tr></table></dt> <dd> Return a suite of all tests cases given a string specifier. <P> The specifier <var>name</var> is a ``dotted name'' that may resolve either to a module, a test case class, a test method within a test case class, or a callable object which returns a <tt class="class">TestCase</tt> or <tt class="class">TestSuite</tt> instance. For example, if you have a module <tt class="module">SampleTests</tt> containing a <tt class="class">TestCase</tt>-derived class <tt class="class">SampleTestCase</tt> with three test methods (<tt class="method">test_one()</tt>, <tt class="method">test_two()</tt>, and <tt class="method">test_three()</tt>), the specifier <code>'SampleTests.SampleTestCase'</code> would cause this method to return a suite which will run all three test methods. Using the specifier <code>'SampleTests.SampleTestCase.test_two'</code> would cause it to return a test suite which will run only the <tt class="method">test_two()</tt> test method. The specifier can refer to modules and packages which have not been imported; they will be imported as a side-effect. <P> The method optionally resolves <var>name</var> relative to a given module. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-1074' class="method">loadTestsFromNames</tt></b>(</nobr></td> <td><var>names</var><big>[</big><var>, module</var><big>]</big>)</td></tr></table></dt> <dd> Similar to <tt class="method">loadTestsFromName()</tt>, but takes a sequence of names rather than a single name. The return value is a test suite which supports all the tests defined for each name. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-1075' class="method">getTestCaseNames</tt></b>(</nobr></td> <td><var>testCaseClass</var>)</td></tr></table></dt> <dd> Return a sorted sequence of method names found within <var>testCaseClass</var>. </dl> <P> The following attributes of a <tt class="class">TestLoader</tt> can be configured either by subclassing or assignment on an instance: <P> <dl><dt><b><tt id='l2h-1076' class="member">testMethodPrefix</tt></b></dt> <dd> String giving the prefix of method names which will be interpreted as test methods. The default value is <code>'test'</code>. </dl> <P> <dl><dt><b><tt id='l2h-1077' class="member">sortTestMethodsUsing</tt></b></dt> <dd> Function to be used to compare method names when sorting them in <tt class="method">getTestCaseNames()</tt>. The default value is the built-in <tt class="function">cmp()</tt> function; it can be set to <code>None</code> to disable the sort. </dl> <P> <dl><dt><b><tt id='l2h-1078' class="member">suiteClass</tt></b></dt> <dd> Callable object that constructs a test suite from a list of tests. No methods on the resulting object are needed. The default value is the <tt class="class">TestSuite</tt> class. </dl> <DIV CLASS="navigation"> <div class='online-navigation'><hr /> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td class='online-navigation'><a rel="prev" title="5.3.7 TestResult Objects" rel="prev" title="5.3.7 TestResult Objects" href="testresult-objects.html"><img src='../icons/previous.png' border='0' height='32' alt='Previous Page' width='32' /></A></td> <td class='online-navigation'><a rel="parent" title="5.3 unittest " rel="parent" title="5.3 unittest " href="module-unittest.html"><img src='../icons/up.png' border='0' height='32' alt='Up One Level' width='32' /></A></td> <td class='online-navigation'><a rel="next" title="5.4 test " rel="next" title="5.4 test " href="module-test.html"><img src='../icons/next.png' border='0' height='32' alt='Next Page' width='32' /></A></td> <td align="center" width="100%">Python Library Reference</td> <td class='online-navigation'><a rel="contents" title="Table of Contents" rel="contents" title="Table of Contents" href="contents.html"><img src='../icons/contents.png' border='0' height='32' alt='Contents' width='32' /></A></td> <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' border='0' height='32' alt='Module Index' width='32' /></a></td> <td class='online-navigation'><a rel="index" title="Index" rel="index" title="Index" href="genindex.html"><img src='../icons/index.png' border='0' height='32' alt='Index' width='32' /></A></td> </tr></table> <div class='online-navigation'> <b class="navlabel">Previous:</b> <a class="sectref" rel="prev" href="testresult-objects.html">5.3.7 TestResult Objects</A> <b class="navlabel">Up:</b> <a class="sectref" rel="parent" href="module-unittest.html">5.3 unittest </A> <b class="navlabel">Next:</b> <a class="sectref" rel="next" href="module-test.html">5.4 test </A> </div> </div> <hr /> <span class="release-info">Release 2.3.4, documentation updated on May 20, 2004.</span> </DIV> <!--End of Navigation Panel--> <ADDRESS> See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. </ADDRESS> </BODY> </HTML>