Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="prev" href="testresult-objects.html" /> | |
13 | <link rel="parent" href="module-unittest.html" /> | |
14 | <link rel="next" href="module-test.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>5.3.8 TestLoader Objects </title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="5.3.7 TestResult Objects" | |
24 | href="testresult-objects.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="5.3 unittest " | |
27 | href="module-unittest.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="5.4 test " | |
30 | href="module-test.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python Library Reference</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
37 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="testresult-objects.html">5.3.7 TestResult Objects</A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="module-unittest.html">5.3 unittest </A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="module-test.html">5.4 test </A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H2><A NAME="SECTION007380000000000000000"></A><A NAME="testloader-objects"></A> | |
55 | <BR> | |
56 | 5.3.8 TestLoader Objects | |
57 | ||
58 | </H2> | |
59 | ||
60 | <P> | |
61 | The <tt class="class">TestLoader</tt> class is used to create test suites from | |
62 | classes and modules. Normally, there is no need to create an instance | |
63 | of this class; the <tt class="module"><a href="module-unittest.html">unittest</a></tt> module provides an instance | |
64 | that can be shared as the <code>defaultTestLoader</code> module attribute. | |
65 | Using a subclass or instance would allow customization of some | |
66 | configurable properties. | |
67 | ||
68 | <P> | |
69 | <tt class="class">TestLoader</tt> objects have the following methods: | |
70 | ||
71 | <P> | |
72 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
73 | <td><nobr><b><tt id='l2h-1171' xml:id='l2h-1171' class="method">loadTestsFromTestCase</tt></b>(</nobr></td> | |
74 | <td><var>testCaseClass</var>)</td></tr></table></dt> | |
75 | <dd> | |
76 | Return a suite of all tests cases contained in the | |
77 | <tt class="class">TestCase</tt>-derived class <tt class="class">testCaseClass</tt>. | |
78 | </dl> | |
79 | ||
80 | <P> | |
81 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
82 | <td><nobr><b><tt id='l2h-1172' xml:id='l2h-1172' class="method">loadTestsFromModule</tt></b>(</nobr></td> | |
83 | <td><var>module</var>)</td></tr></table></dt> | |
84 | <dd> | |
85 | Return a suite of all tests cases contained in the given module. | |
86 | This method searches <var>module</var> for classes derived from | |
87 | <tt class="class">TestCase</tt> and creates an instance of the class for each test | |
88 | method defined for the class. | |
89 | ||
90 | <P> | |
91 | <span class="warning"><b class="label">Warning:</b> | |
92 | While using a hierarchy of | |
93 | <tt class="class">Testcase</tt>-derived classes can be convenient in sharing | |
94 | fixtures and helper functions, defining test methods on base classes | |
95 | that are not intended to be instantiated directly does not play well | |
96 | with this method. Doing so, however, can be useful when the | |
97 | fixtures are different and defined in subclasses.</span> | |
98 | </dl> | |
99 | ||
100 | <P> | |
101 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
102 | <td><nobr><b><tt id='l2h-1173' xml:id='l2h-1173' class="method">loadTestsFromName</tt></b>(</nobr></td> | |
103 | <td><var>name</var><big>[</big><var>, module</var><big>]</big><var></var>)</td></tr></table></dt> | |
104 | <dd> | |
105 | Return a suite of all tests cases given a string specifier. | |
106 | ||
107 | <P> | |
108 | The specifier <var>name</var> is a ``dotted name'' that may resolve | |
109 | either to a module, a test case class, a test method within a test | |
110 | case class, or a callable object which returns a <tt class="class">TestCase</tt> or | |
111 | <tt class="class">TestSuite</tt> instance. For example, if you have a module | |
112 | <tt class="module">SampleTests</tt> containing a <tt class="class">TestCase</tt>-derived class | |
113 | <tt class="class">SampleTestCase</tt> with three test methods (<tt class="method">test_one()</tt>, | |
114 | <tt class="method">test_two()</tt>, and <tt class="method">test_three()</tt>), the specifier | |
115 | <code>'SampleTests.SampleTestCase'</code> would cause this method to | |
116 | return a suite which will run all three test methods. Using the | |
117 | specifier <code>'SampleTests.SampleTestCase.test_two'</code> would cause | |
118 | it to return a test suite which will run only the | |
119 | <tt class="method">test_two()</tt> test method. The specifier can refer to modules | |
120 | and packages which have not been imported; they will be imported as | |
121 | a side-effect. | |
122 | ||
123 | <P> | |
124 | The method optionally resolves <var>name</var> relative to a given module. | |
125 | </dl> | |
126 | ||
127 | <P> | |
128 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
129 | <td><nobr><b><tt id='l2h-1174' xml:id='l2h-1174' class="method">loadTestsFromNames</tt></b>(</nobr></td> | |
130 | <td><var>names</var><big>[</big><var>, module</var><big>]</big><var></var>)</td></tr></table></dt> | |
131 | <dd> | |
132 | Similar to <tt class="method">loadTestsFromName()</tt>, but takes a sequence of | |
133 | names rather than a single name. The return value is a test suite | |
134 | which supports all the tests defined for each name. | |
135 | </dl> | |
136 | ||
137 | <P> | |
138 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
139 | <td><nobr><b><tt id='l2h-1175' xml:id='l2h-1175' class="method">getTestCaseNames</tt></b>(</nobr></td> | |
140 | <td><var>testCaseClass</var>)</td></tr></table></dt> | |
141 | <dd> | |
142 | Return a sorted sequence of method names found within | |
143 | <var>testCaseClass</var>. | |
144 | </dl> | |
145 | ||
146 | <P> | |
147 | The following attributes of a <tt class="class">TestLoader</tt> can be configured | |
148 | either by subclassing or assignment on an instance: | |
149 | ||
150 | <P> | |
151 | <dl><dt><b><tt id='l2h-1176' xml:id='l2h-1176' class="member">testMethodPrefix</tt></b></dt> | |
152 | <dd> | |
153 | String giving the prefix of method names which will be interpreted | |
154 | as test methods. The default value is <code>'test'</code>. | |
155 | </dl> | |
156 | ||
157 | <P> | |
158 | <dl><dt><b><tt id='l2h-1177' xml:id='l2h-1177' class="member">sortTestMethodsUsing</tt></b></dt> | |
159 | <dd> | |
160 | Function to be used to compare method names when sorting them in | |
161 | <tt class="method">getTestCaseNames()</tt>. The default value is the built-in | |
162 | <tt class="function">cmp()</tt> function; it can be set to <tt class="constant">None</tt> to disable | |
163 | the sort. | |
164 | </dl> | |
165 | ||
166 | <P> | |
167 | <dl><dt><b><tt id='l2h-1178' xml:id='l2h-1178' class="member">suiteClass</tt></b></dt> | |
168 | <dd> | |
169 | Callable object that constructs a test suite from a list of tests. | |
170 | No methods on the resulting object are needed. The default value is | |
171 | the <tt class="class">TestSuite</tt> class. | |
172 | </dl> | |
173 | ||
174 | <DIV CLASS="navigation"> | |
175 | <div class='online-navigation'> | |
176 | <p></p><hr /> | |
177 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
178 | <tr> | |
179 | <td class='online-navigation'><a rel="prev" title="5.3.7 TestResult Objects" | |
180 | href="testresult-objects.html"><img src='../icons/previous.png' | |
181 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
182 | <td class='online-navigation'><a rel="parent" title="5.3 unittest " | |
183 | href="module-unittest.html"><img src='../icons/up.png' | |
184 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
185 | <td class='online-navigation'><a rel="next" title="5.4 test " | |
186 | href="module-test.html"><img src='../icons/next.png' | |
187 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
188 | <td align="center" width="100%">Python Library Reference</td> | |
189 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
190 | href="contents.html"><img src='../icons/contents.png' | |
191 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
192 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
193 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
194 | <td class='online-navigation'><a rel="index" title="Index" | |
195 | href="genindex.html"><img src='../icons/index.png' | |
196 | border='0' height='32' alt='Index' width='32' /></A></td> | |
197 | </tr></table> | |
198 | <div class='online-navigation'> | |
199 | <b class="navlabel">Previous:</b> | |
200 | <a class="sectref" rel="prev" href="testresult-objects.html">5.3.7 TestResult Objects</A> | |
201 | <b class="navlabel">Up:</b> | |
202 | <a class="sectref" rel="parent" href="module-unittest.html">5.3 unittest </A> | |
203 | <b class="navlabel">Next:</b> | |
204 | <a class="sectref" rel="next" href="module-test.html">5.4 test </A> | |
205 | </div> | |
206 | </div> | |
207 | <hr /> | |
208 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
209 | </DIV> | |
210 | <!--End of Navigation Panel--> | |
211 | <ADDRESS> | |
212 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
213 | </ADDRESS> | |
214 | </BODY> | |
215 | </HTML> |