gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3028 lines
317KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="unittest — Unit testing framework" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/unittest.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/unittest/__init__.py(If you are already familiar with the basic concepts of testing, you might want to skip to the list of assert methods.) The unittest unit testing framework was ..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/unittest/__init__.py(If you are already familiar with the basic concepts of testing, you might want to skip to the list of assert methods.) The unittest unit testing framework was ..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>unittest — Unit testing framework &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="unittest.mock — mock object library" href="unittest.mock.html" />
  33. <link rel="prev" title="doctest — Test interactive Python examples" href="doctest.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/unittest.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code> — Unit testing framework</a><ul>
  86. <li><a class="reference internal" href="#basic-example">Basic example</a></li>
  87. <li><a class="reference internal" href="#command-line-interface">Command-Line Interface</a><ul>
  88. <li><a class="reference internal" href="#command-line-options">Command-line options</a></li>
  89. </ul>
  90. </li>
  91. <li><a class="reference internal" href="#test-discovery">Test Discovery</a></li>
  92. <li><a class="reference internal" href="#organizing-test-code">Organizing test code</a></li>
  93. <li><a class="reference internal" href="#re-using-old-test-code">Re-using old test code</a></li>
  94. <li><a class="reference internal" href="#skipping-tests-and-expected-failures">Skipping tests and expected failures</a></li>
  95. <li><a class="reference internal" href="#distinguishing-test-iterations-using-subtests">Distinguishing test iterations using subtests</a></li>
  96. <li><a class="reference internal" href="#classes-and-functions">Classes and functions</a><ul>
  97. <li><a class="reference internal" href="#test-cases">Test cases</a></li>
  98. <li><a class="reference internal" href="#grouping-tests">Grouping tests</a></li>
  99. <li><a class="reference internal" href="#loading-and-running-tests">Loading and running tests</a><ul>
  100. <li><a class="reference internal" href="#load-tests-protocol">load_tests Protocol</a></li>
  101. </ul>
  102. </li>
  103. </ul>
  104. </li>
  105. <li><a class="reference internal" href="#class-and-module-fixtures">Class and Module Fixtures</a><ul>
  106. <li><a class="reference internal" href="#setupclass-and-teardownclass">setUpClass and tearDownClass</a></li>
  107. <li><a class="reference internal" href="#setupmodule-and-teardownmodule">setUpModule and tearDownModule</a></li>
  108. </ul>
  109. </li>
  110. <li><a class="reference internal" href="#signal-handling">Signal Handling</a></li>
  111. </ul>
  112. </li>
  113. </ul>
  114. </div>
  115. <div>
  116. <h4>Previous topic</h4>
  117. <p class="topless"><a href="doctest.html"
  118. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> — Test interactive Python examples</a></p>
  119. </div>
  120. <div>
  121. <h4>Next topic</h4>
  122. <p class="topless"><a href="unittest.mock.html"
  123. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest.mock</span></code> — mock object library</a></p>
  124. </div>
  125. <div role="note" aria-label="source link">
  126. <h3>This Page</h3>
  127. <ul class="this-page-menu">
  128. <li><a href="../bugs.html">Report a Bug</a></li>
  129. <li>
  130. <a href="https://github.com/python/cpython/blob/main/Doc/library/unittest.rst"
  131. rel="nofollow">Show Source
  132. </a>
  133. </li>
  134. </ul>
  135. </div>
  136. </nav>
  137. </div>
  138. </div>
  139. <div class="related" role="navigation" aria-label="related navigation">
  140. <h3>Navigation</h3>
  141. <ul>
  142. <li class="right" style="margin-right: 10px">
  143. <a href="../genindex.html" title="General Index"
  144. accesskey="I">index</a></li>
  145. <li class="right" >
  146. <a href="../py-modindex.html" title="Python Module Index"
  147. >modules</a> |</li>
  148. <li class="right" >
  149. <a href="unittest.mock.html" title="unittest.mock — mock object library"
  150. accesskey="N">next</a> |</li>
  151. <li class="right" >
  152. <a href="doctest.html" title="doctest — Test interactive Python examples"
  153. accesskey="P">previous</a> |</li>
  154. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  155. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  156. <li class="switchers">
  157. <div class="language_switcher_placeholder"></div>
  158. <div class="version_switcher_placeholder"></div>
  159. </li>
  160. <li>
  161. </li>
  162. <li id="cpython-language-and-version">
  163. <a href="../index.html">3.12.3 Documentation</a> &#187;
  164. </li>
  165. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  166. <li class="nav-item nav-item-2"><a href="development.html" accesskey="U">Development Tools</a> &#187;</li>
  167. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code> — Unit testing framework</a></li>
  168. <li class="right">
  169. <div class="inline-search" role="search">
  170. <form class="inline-search" action="../search.html" method="get">
  171. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  172. <input type="submit" value="Go" />
  173. </form>
  174. </div>
  175. |
  176. </li>
  177. <li class="right">
  178. <label class="theme-selector-label">
  179. Theme
  180. <select class="theme-selector" oninput="activateTheme(this.value)">
  181. <option value="auto" selected>Auto</option>
  182. <option value="light">Light</option>
  183. <option value="dark">Dark</option>
  184. </select>
  185. </label> |</li>
  186. </ul>
  187. </div>
  188. <div class="document">
  189. <div class="documentwrapper">
  190. <div class="bodywrapper">
  191. <div class="body" role="main">
  192. <section id="module-unittest">
  193. <span id="unittest-unit-testing-framework"></span><h1><a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> — Unit testing framework<a class="headerlink" href="#module-unittest" title="Link to this heading">¶</a></h1>
  194. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/unittest/__init__.py">Lib/unittest/__init__.py</a></p>
  195. <hr class="docutils" />
  196. <p>(If you are already familiar with the basic concepts of testing, you might want
  197. to skip to <a class="reference internal" href="#assert-methods"><span class="std std-ref">the list of assert methods</span></a>.)</p>
  198. <p>The <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> unit testing framework was originally inspired by JUnit
  199. and has a similar flavor as major unit testing frameworks in other
  200. languages. It supports test automation, sharing of setup and shutdown code
  201. for tests, aggregation of tests into collections, and independence of the
  202. tests from the reporting framework.</p>
  203. <p>To achieve this, <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> supports some important concepts in an
  204. object-oriented way:</p>
  205. <dl class="simple">
  206. <dt>test fixture</dt><dd><p>A <em class="dfn">test fixture</em> represents the preparation needed to perform one or more
  207. tests, and any associated cleanup actions. This may involve, for example,
  208. creating temporary or proxy databases, directories, or starting a server
  209. process.</p>
  210. </dd>
  211. <dt>test case</dt><dd><p>A <em class="dfn">test case</em> is the individual unit of testing. It checks for a specific
  212. response to a particular set of inputs. <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> provides a base class,
  213. <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a>, which may be used to create new test cases.</p>
  214. </dd>
  215. <dt>test suite</dt><dd><p>A <em class="dfn">test suite</em> is a collection of test cases, test suites, or both. It is
  216. used to aggregate tests that should be executed together.</p>
  217. </dd>
  218. <dt>test runner</dt><dd><p>A <em class="dfn">test runner</em> is a component which orchestrates the execution of tests
  219. and provides the outcome to the user. The runner may use a graphical interface,
  220. a textual interface, or return a special value to indicate the results of
  221. executing the tests.</p>
  222. </dd>
  223. </dl>
  224. <div class="admonition seealso">
  225. <p class="admonition-title">See also</p>
  226. <dl class="simple">
  227. <dt>Module <a class="reference internal" href="doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a></dt><dd><p>Another test-support module with a very different flavor.</p>
  228. </dd>
  229. <dt><a class="reference external" href="https://web.archive.org/web/20150315073817/http://www.xprogramming.com/testfram.htm">Simple Smalltalk Testing: With Patterns</a></dt><dd><p>Kent Beck’s original paper on testing frameworks using the pattern shared
  230. by <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a>.</p>
  231. </dd>
  232. <dt><a class="reference external" href="https://docs.pytest.org/">pytest</a></dt><dd><p>Third-party unittest framework with a lighter-weight syntax for writing
  233. tests. For example, <code class="docutils literal notranslate"><span class="pre">assert</span> <span class="pre">func(10)</span> <span class="pre">==</span> <span class="pre">42</span></code>.</p>
  234. </dd>
  235. <dt><a class="reference external" href="https://wiki.python.org/moin/PythonTestingToolsTaxonomy">The Python Testing Tools Taxonomy</a></dt><dd><p>An extensive list of Python testing tools including functional testing
  236. frameworks and mock object libraries.</p>
  237. </dd>
  238. <dt><a class="reference external" href="http://lists.idyll.org/listinfo/testing-in-python">Testing in Python Mailing List</a></dt><dd><p>A special-interest-group for discussion of testing, and testing tools,
  239. in Python.</p>
  240. </dd>
  241. </dl>
  242. <p>The script <code class="file docutils literal notranslate"><span class="pre">Tools/unittestgui/unittestgui.py</span></code> in the Python source distribution is
  243. a GUI tool for test discovery and execution. This is intended largely for ease of use
  244. for those new to unit testing. For production environments it is
  245. recommended that tests be driven by a continuous integration system such as
  246. <a class="reference external" href="https://buildbot.net/">Buildbot</a>, <a class="reference external" href="https://www.jenkins.io/">Jenkins</a>,
  247. <a class="reference external" href="https://github.com/features/actions">GitHub Actions</a>, or
  248. <a class="reference external" href="https://www.appveyor.com/">AppVeyor</a>.</p>
  249. </div>
  250. <section id="basic-example">
  251. <span id="unittest-minimal-example"></span><h2>Basic example<a class="headerlink" href="#basic-example" title="Link to this heading">¶</a></h2>
  252. <p>The <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> module provides a rich set of tools for constructing and
  253. running tests. This section demonstrates that a small subset of the tools
  254. suffice to meet the needs of most users.</p>
  255. <p>Here is a short script to test three string methods:</p>
  256. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">unittest</span>
  257. <span class="k">class</span> <span class="nc">TestStringMethods</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
  258. <span class="k">def</span> <span class="nf">test_upper</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  259. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="s1">&#39;FOO&#39;</span><span class="p">)</span>
  260. <span class="k">def</span> <span class="nf">test_isupper</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  261. <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</span><span class="p">(</span><span class="s1">&#39;FOO&#39;</span><span class="o">.</span><span class="n">isupper</span><span class="p">())</span>
  262. <span class="bp">self</span><span class="o">.</span><span class="n">assertFalse</span><span class="p">(</span><span class="s1">&#39;Foo&#39;</span><span class="o">.</span><span class="n">isupper</span><span class="p">())</span>
  263. <span class="k">def</span> <span class="nf">test_split</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  264. <span class="n">s</span> <span class="o">=</span> <span class="s1">&#39;hello world&#39;</span>
  265. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">split</span><span class="p">(),</span> <span class="p">[</span><span class="s1">&#39;hello&#39;</span><span class="p">,</span> <span class="s1">&#39;world&#39;</span><span class="p">])</span>
  266. <span class="c1"># check that s.split fails when the separator is not a string</span>
  267. <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="ne">TypeError</span><span class="p">):</span>
  268. <span class="n">s</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
  269. <span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
  270. <span class="n">unittest</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
  271. </pre></div>
  272. </div>
  273. <p>A testcase is created by subclassing <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestCase</span></code></a>. The three
  274. individual tests are defined with methods whose names start with the letters
  275. <code class="docutils literal notranslate"><span class="pre">test</span></code>. This naming convention informs the test runner about which methods
  276. represent tests.</p>
  277. <p>The crux of each test is a call to <a class="reference internal" href="#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual()</span></code></a> to check for an
  278. expected result; <a class="reference internal" href="#unittest.TestCase.assertTrue" title="unittest.TestCase.assertTrue"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertTrue()</span></code></a> or <a class="reference internal" href="#unittest.TestCase.assertFalse" title="unittest.TestCase.assertFalse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertFalse()</span></code></a>
  279. to verify a condition; or <a class="reference internal" href="#unittest.TestCase.assertRaises" title="unittest.TestCase.assertRaises"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRaises()</span></code></a> to verify that a
  280. specific exception gets raised. These methods are used instead of the
  281. <a class="reference internal" href="../reference/simple_stmts.html#assert"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">assert</span></code></a> statement so the test runner can accumulate all test results
  282. and produce a report.</p>
  283. <p>The <a class="reference internal" href="#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUp()</span></code></a> and <a class="reference internal" href="#unittest.TestCase.tearDown" title="unittest.TestCase.tearDown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDown()</span></code></a> methods allow you
  284. to define instructions that will be executed before and after each test method.
  285. They are covered in more detail in the section <a class="reference internal" href="#organizing-tests"><span class="std std-ref">Organizing test code</span></a>.</p>
  286. <p>The final block shows a simple way to run the tests. <a class="reference internal" href="#unittest.main" title="unittest.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">unittest.main()</span></code></a>
  287. provides a command-line interface to the test script. When run from the command
  288. line, the above script produces an output that looks like this:</p>
  289. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">...</span>
  290. <span class="o">----------------------------------------------------------------------</span>
  291. <span class="n">Ran</span> <span class="mi">3</span> <span class="n">tests</span> <span class="ow">in</span> <span class="mf">0.000</span><span class="n">s</span>
  292. <span class="n">OK</span>
  293. </pre></div>
  294. </div>
  295. <p>Passing the <code class="docutils literal notranslate"><span class="pre">-v</span></code> option to your test script will instruct <a class="reference internal" href="#unittest.main" title="unittest.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">unittest.main()</span></code></a>
  296. to enable a higher level of verbosity, and produce the following output:</p>
  297. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">test_isupper</span> <span class="p">(</span><span class="n">__main__</span><span class="o">.</span><span class="n">TestStringMethods</span><span class="o">.</span><span class="n">test_isupper</span><span class="p">)</span> <span class="o">...</span> <span class="n">ok</span>
  298. <span class="n">test_split</span> <span class="p">(</span><span class="n">__main__</span><span class="o">.</span><span class="n">TestStringMethods</span><span class="o">.</span><span class="n">test_split</span><span class="p">)</span> <span class="o">...</span> <span class="n">ok</span>
  299. <span class="n">test_upper</span> <span class="p">(</span><span class="n">__main__</span><span class="o">.</span><span class="n">TestStringMethods</span><span class="o">.</span><span class="n">test_upper</span><span class="p">)</span> <span class="o">...</span> <span class="n">ok</span>
  300. <span class="o">----------------------------------------------------------------------</span>
  301. <span class="n">Ran</span> <span class="mi">3</span> <span class="n">tests</span> <span class="ow">in</span> <span class="mf">0.001</span><span class="n">s</span>
  302. <span class="n">OK</span>
  303. </pre></div>
  304. </div>
  305. <p>The above examples show the most commonly used <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> features which
  306. are sufficient to meet many everyday testing needs. The remainder of the
  307. documentation explores the full feature set from first principles.</p>
  308. <div class="versionchanged">
  309. <p><span class="versionmodified changed">Changed in version 3.11: </span>The behavior of returning a value from a test method (other than the default
  310. <code class="docutils literal notranslate"><span class="pre">None</span></code> value), is now deprecated.</p>
  311. </div>
  312. </section>
  313. <section id="command-line-interface">
  314. <span id="unittest-command-line-interface"></span><h2>Command-Line Interface<a class="headerlink" href="#command-line-interface" title="Link to this heading">¶</a></h2>
  315. <p>The unittest module can be used from the command line to run tests from
  316. modules, classes or even individual test methods:</p>
  317. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">unittest</span> <span class="n">test_module1</span> <span class="n">test_module2</span>
  318. <span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">unittest</span> <span class="n">test_module</span><span class="o">.</span><span class="n">TestClass</span>
  319. <span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">unittest</span> <span class="n">test_module</span><span class="o">.</span><span class="n">TestClass</span><span class="o">.</span><span class="n">test_method</span>
  320. </pre></div>
  321. </div>
  322. <p>You can pass in a list with any combination of module names, and fully
  323. qualified class or method names.</p>
  324. <p>Test modules can be specified by file path as well:</p>
  325. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">unittest</span> <span class="n">tests</span><span class="o">/</span><span class="n">test_something</span><span class="o">.</span><span class="n">py</span>
  326. </pre></div>
  327. </div>
  328. <p>This allows you to use the shell filename completion to specify the test module.
  329. The file specified must still be importable as a module. The path is converted
  330. to a module name by removing the ‘.py’ and converting path separators into ‘.’.
  331. If you want to execute a test file that isn’t importable as a module you should
  332. execute the file directly instead.</p>
  333. <p>You can run tests with more detail (higher verbosity) by passing in the -v flag:</p>
  334. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">unittest</span> <span class="o">-</span><span class="n">v</span> <span class="n">test_module</span>
  335. </pre></div>
  336. </div>
  337. <p>When executed without arguments <a class="reference internal" href="#unittest-test-discovery"><span class="std std-ref">Test Discovery</span></a> is started:</p>
  338. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">unittest</span>
  339. </pre></div>
  340. </div>
  341. <p>For a list of all the command-line options:</p>
  342. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">unittest</span> <span class="o">-</span><span class="n">h</span>
  343. </pre></div>
  344. </div>
  345. <div class="versionchanged">
  346. <p><span class="versionmodified changed">Changed in version 3.2: </span>In earlier versions it was only possible to run individual test methods and
  347. not modules or classes.</p>
  348. </div>
  349. <section id="command-line-options">
  350. <h3>Command-line options<a class="headerlink" href="#command-line-options" title="Link to this heading">¶</a></h3>
  351. <p><strong class="program">unittest</strong> supports these command-line options:</p>
  352. <dl class="std option">
  353. <dt class="sig sig-object std" id="cmdoption-unittest-b">
  354. <span id="cmdoption-unittest-buffer"></span><span class="sig-name descname"><span class="pre">-b</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--buffer</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-unittest-b" title="Link to this definition">¶</a></dt>
  355. <dd><p>The standard output and standard error streams are buffered during the test
  356. run. Output during a passing test is discarded. Output is echoed normally
  357. on test fail or error and is added to the failure messages.</p>
  358. </dd></dl>
  359. <dl class="std option">
  360. <dt class="sig sig-object std" id="cmdoption-unittest-c">
  361. <span id="cmdoption-unittest-catch"></span><span class="sig-name descname"><span class="pre">-c</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--catch</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-unittest-c" title="Link to this definition">¶</a></dt>
  362. <dd><p><kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">C</kbd></kbd> during the test run waits for the current test to end and then
  363. reports all the results so far. A second <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">C</kbd></kbd> raises the normal
  364. <a class="reference internal" href="exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a> exception.</p>
  365. <p>See <a class="reference internal" href="#signal-handling">Signal Handling</a> for the functions that provide this functionality.</p>
  366. </dd></dl>
  367. <dl class="std option">
  368. <dt class="sig sig-object std" id="cmdoption-unittest-f">
  369. <span id="cmdoption-unittest-failfast"></span><span class="sig-name descname"><span class="pre">-f</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--failfast</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-unittest-f" title="Link to this definition">¶</a></dt>
  370. <dd><p>Stop the test run on the first error or failure.</p>
  371. </dd></dl>
  372. <dl class="std option">
  373. <dt class="sig sig-object std" id="cmdoption-unittest-k">
  374. <span class="sig-name descname"><span class="pre">-k</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-unittest-k" title="Link to this definition">¶</a></dt>
  375. <dd><p>Only run test methods and classes that match the pattern or substring.
  376. This option may be used multiple times, in which case all test cases that
  377. match any of the given patterns are included.</p>
  378. <p>Patterns that contain a wildcard character (<code class="docutils literal notranslate"><span class="pre">*</span></code>) are matched against the
  379. test name using <a class="reference internal" href="fnmatch.html#fnmatch.fnmatchcase" title="fnmatch.fnmatchcase"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fnmatch.fnmatchcase()</span></code></a>; otherwise simple case-sensitive
  380. substring matching is used.</p>
  381. <p>Patterns are matched against the fully qualified test method name as
  382. imported by the test loader.</p>
  383. <p>For example, <code class="docutils literal notranslate"><span class="pre">-k</span> <span class="pre">foo</span></code> matches <code class="docutils literal notranslate"><span class="pre">foo_tests.SomeTest.test_something</span></code>,
  384. <code class="docutils literal notranslate"><span class="pre">bar_tests.SomeTest.test_foo</span></code>, but not <code class="docutils literal notranslate"><span class="pre">bar_tests.FooTest.test_something</span></code>.</p>
  385. </dd></dl>
  386. <dl class="std option">
  387. <dt class="sig sig-object std" id="cmdoption-unittest-locals">
  388. <span class="sig-name descname"><span class="pre">--locals</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-unittest-locals" title="Link to this definition">¶</a></dt>
  389. <dd><p>Show local variables in tracebacks.</p>
  390. </dd></dl>
  391. <dl class="std option">
  392. <dt class="sig sig-object std" id="cmdoption-unittest-durations">
  393. <span class="sig-name descname"><span class="pre">--durations</span></span><span class="sig-prename descclassname"> <span class="pre">N</span></span><a class="headerlink" href="#cmdoption-unittest-durations" title="Link to this definition">¶</a></dt>
  394. <dd><p>Show the N slowest test cases (N=0 for all).</p>
  395. </dd></dl>
  396. <div class="versionadded">
  397. <p><span class="versionmodified added">New in version 3.2: </span>The command-line options <code class="docutils literal notranslate"><span class="pre">-b</span></code>, <code class="docutils literal notranslate"><span class="pre">-c</span></code> and <code class="docutils literal notranslate"><span class="pre">-f</span></code> were added.</p>
  398. </div>
  399. <div class="versionadded">
  400. <p><span class="versionmodified added">New in version 3.5: </span>The command-line option <code class="docutils literal notranslate"><span class="pre">--locals</span></code>.</p>
  401. </div>
  402. <div class="versionadded">
  403. <p><span class="versionmodified added">New in version 3.7: </span>The command-line option <code class="docutils literal notranslate"><span class="pre">-k</span></code>.</p>
  404. </div>
  405. <div class="versionadded">
  406. <p><span class="versionmodified added">New in version 3.12: </span>The command-line option <code class="docutils literal notranslate"><span class="pre">--durations</span></code>.</p>
  407. </div>
  408. <p>The command line can also be used for test discovery, for running all of the
  409. tests in a project or just a subset.</p>
  410. </section>
  411. </section>
  412. <section id="test-discovery">
  413. <span id="unittest-test-discovery"></span><h2>Test Discovery<a class="headerlink" href="#test-discovery" title="Link to this heading">¶</a></h2>
  414. <div class="versionadded">
  415. <p><span class="versionmodified added">New in version 3.2.</span></p>
  416. </div>
  417. <p>Unittest supports simple test discovery. In order to be compatible with test
  418. discovery, all of the test files must be <a class="reference internal" href="../tutorial/modules.html#tut-modules"><span class="std std-ref">modules</span></a> or
  419. <a class="reference internal" href="../tutorial/modules.html#tut-packages"><span class="std std-ref">packages</span></a> importable from the top-level directory of
  420. the project (this means that their filenames must be valid <a class="reference internal" href="../reference/lexical_analysis.html#identifiers"><span class="std std-ref">identifiers</span></a>).</p>
  421. <p>Test discovery is implemented in <a class="reference internal" href="#unittest.TestLoader.discover" title="unittest.TestLoader.discover"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TestLoader.discover()</span></code></a>, but can also be
  422. used from the command line. The basic command-line usage is:</p>
  423. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">cd</span> <span class="n">project_directory</span>
  424. <span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">unittest</span> <span class="n">discover</span>
  425. </pre></div>
  426. </div>
  427. <div class="admonition note">
  428. <p class="admonition-title">Note</p>
  429. <p>As a shortcut, <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">unittest</span></code> is the equivalent of
  430. <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">unittest</span> <span class="pre">discover</span></code>. If you want to pass arguments to test
  431. discovery the <code class="docutils literal notranslate"><span class="pre">discover</span></code> sub-command must be used explicitly.</p>
  432. </div>
  433. <p>The <code class="docutils literal notranslate"><span class="pre">discover</span></code> sub-command has the following options:</p>
  434. <dl class="std option">
  435. <dt class="sig sig-object std" id="cmdoption-unittest-discover-v">
  436. <span id="cmdoption-unittest-discover-verbose"></span><span class="sig-name descname"><span class="pre">-v</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--verbose</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-unittest-discover-v" title="Link to this definition">¶</a></dt>
  437. <dd><p>Verbose output</p>
  438. </dd></dl>
  439. <dl class="std option">
  440. <dt class="sig sig-object std" id="cmdoption-unittest-discover-s">
  441. <span id="cmdoption-unittest-discover-start-directory"></span><span class="sig-name descname"><span class="pre">-s</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--start-directory</span></span><span class="sig-prename descclassname"> <span class="pre">directory</span></span><a class="headerlink" href="#cmdoption-unittest-discover-s" title="Link to this definition">¶</a></dt>
  442. <dd><p>Directory to start discovery (<code class="docutils literal notranslate"><span class="pre">.</span></code> default)</p>
  443. </dd></dl>
  444. <dl class="std option">
  445. <dt class="sig sig-object std" id="cmdoption-unittest-discover-p">
  446. <span id="cmdoption-unittest-discover-pattern"></span><span class="sig-name descname"><span class="pre">-p</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--pattern</span></span><span class="sig-prename descclassname"> <span class="pre">pattern</span></span><a class="headerlink" href="#cmdoption-unittest-discover-p" title="Link to this definition">¶</a></dt>
  447. <dd><p>Pattern to match test files (<code class="docutils literal notranslate"><span class="pre">test*.py</span></code> default)</p>
  448. </dd></dl>
  449. <dl class="std option">
  450. <dt class="sig sig-object std" id="cmdoption-unittest-discover-t">
  451. <span id="cmdoption-unittest-discover-top-level-directory"></span><span class="sig-name descname"><span class="pre">-t</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--top-level-directory</span></span><span class="sig-prename descclassname"> <span class="pre">directory</span></span><a class="headerlink" href="#cmdoption-unittest-discover-t" title="Link to this definition">¶</a></dt>
  452. <dd><p>Top level directory of project (defaults to start directory)</p>
  453. </dd></dl>
  454. <p>The <a class="reference internal" href="#cmdoption-unittest-discover-s"><code class="xref std std-option docutils literal notranslate"><span class="pre">-s</span></code></a>, <a class="reference internal" href="#cmdoption-unittest-discover-p"><code class="xref std std-option docutils literal notranslate"><span class="pre">-p</span></code></a>, and <a class="reference internal" href="#cmdoption-unittest-discover-t"><code class="xref std std-option docutils literal notranslate"><span class="pre">-t</span></code></a> options can be passed in
  455. as positional arguments in that order. The following two command lines
  456. are equivalent:</p>
  457. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">unittest</span> <span class="n">discover</span> <span class="o">-</span><span class="n">s</span> <span class="n">project_directory</span> <span class="o">-</span><span class="n">p</span> <span class="s2">&quot;*_test.py&quot;</span>
  458. <span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">unittest</span> <span class="n">discover</span> <span class="n">project_directory</span> <span class="s2">&quot;*_test.py&quot;</span>
  459. </pre></div>
  460. </div>
  461. <p>As well as being a path it is possible to pass a package name, for example
  462. <code class="docutils literal notranslate"><span class="pre">myproject.subpackage.test</span></code>, as the start directory. The package name you
  463. supply will then be imported and its location on the filesystem will be used
  464. as the start directory.</p>
  465. <div class="admonition caution">
  466. <p class="admonition-title">Caution</p>
  467. <p>Test discovery loads tests by importing them. Once test discovery has found
  468. all the test files from the start directory you specify it turns the paths
  469. into package names to import. For example <code class="file docutils literal notranslate"><span class="pre">foo/bar/baz.py</span></code> will be
  470. imported as <code class="docutils literal notranslate"><span class="pre">foo.bar.baz</span></code>.</p>
  471. <p>If you have a package installed globally and attempt test discovery on
  472. a different copy of the package then the import <em>could</em> happen from the
  473. wrong place. If this happens test discovery will warn you and exit.</p>
  474. <p>If you supply the start directory as a package name rather than a
  475. path to a directory then discover assumes that whichever location it
  476. imports from is the location you intended, so you will not get the
  477. warning.</p>
  478. </div>
  479. <p>Test modules and packages can customize test loading and discovery by through
  480. the <a class="reference internal" href="#id1">load_tests protocol</a>.</p>
  481. <div class="versionchanged">
  482. <p><span class="versionmodified changed">Changed in version 3.4: </span>Test discovery supports <a class="reference internal" href="../glossary.html#term-namespace-package"><span class="xref std std-term">namespace packages</span></a>
  483. for the start directory. Note that you need to specify the top level
  484. directory too (e.g.
  485. <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">unittest</span> <span class="pre">discover</span> <span class="pre">-s</span> <span class="pre">root/namespace</span> <span class="pre">-t</span> <span class="pre">root</span></code>).</p>
  486. </div>
  487. <div class="versionchanged">
  488. <p><span class="versionmodified changed">Changed in version 3.11: </span><a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> dropped the <a class="reference internal" href="../glossary.html#term-namespace-package"><span class="xref std std-term">namespace packages</span></a>
  489. support in Python 3.11. It has been broken since Python 3.7. Start directory and
  490. subdirectories containing tests must be regular package that have
  491. <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> file.</p>
  492. <p>Directories containing start directory still can be a namespace package.
  493. In this case, you need to specify start directory as dotted package name,
  494. and target directory explicitly. For example:</p>
  495. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># proj/ &lt;-- current directory</span>
  496. <span class="c1"># namespace/</span>
  497. <span class="c1"># mypkg/</span>
  498. <span class="c1"># __init__.py</span>
  499. <span class="c1"># test_mypkg.py</span>
  500. <span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">unittest</span> <span class="n">discover</span> <span class="o">-</span><span class="n">s</span> <span class="n">namespace</span><span class="o">.</span><span class="n">mypkg</span> <span class="o">-</span><span class="n">t</span> <span class="o">.</span>
  501. </pre></div>
  502. </div>
  503. </div>
  504. </section>
  505. <section id="organizing-test-code">
  506. <span id="organizing-tests"></span><h2>Organizing test code<a class="headerlink" href="#organizing-test-code" title="Link to this heading">¶</a></h2>
  507. <p>The basic building blocks of unit testing are <em class="dfn">test cases</em> — single
  508. scenarios that must be set up and checked for correctness. In <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a>,
  509. test cases are represented by <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestCase</span></code></a> instances.
  510. To make your own test cases you must write subclasses of
  511. <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> or use <a class="reference internal" href="#unittest.FunctionTestCase" title="unittest.FunctionTestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">FunctionTestCase</span></code></a>.</p>
  512. <p>The testing code of a <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> instance should be entirely self
  513. contained, such that it can be run either in isolation or in arbitrary
  514. combination with any number of other test cases.</p>
  515. <p>The simplest <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> subclass will simply implement a test method
  516. (i.e. a method whose name starts with <code class="docutils literal notranslate"><span class="pre">test</span></code>) in order to perform specific
  517. testing code:</p>
  518. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">unittest</span>
  519. <span class="k">class</span> <span class="nc">DefaultWidgetSizeTestCase</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
  520. <span class="k">def</span> <span class="nf">test_default_widget_size</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  521. <span class="n">widget</span> <span class="o">=</span> <span class="n">Widget</span><span class="p">(</span><span class="s1">&#39;The widget&#39;</span><span class="p">)</span>
  522. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">widget</span><span class="o">.</span><span class="n">size</span><span class="p">(),</span> <span class="p">(</span><span class="mi">50</span><span class="p">,</span> <span class="mi">50</span><span class="p">))</span>
  523. </pre></div>
  524. </div>
  525. <p>Note that in order to test something, we use one of the <a class="reference internal" href="#assert-methods"><span class="std std-ref">assert* methods</span></a>
  526. provided by the <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> base class. If the test fails, an
  527. exception will be raised with an explanatory message, and <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a>
  528. will identify the test case as a <em class="dfn">failure</em>. Any other exceptions will be
  529. treated as <em class="dfn">errors</em>.</p>
  530. <p>Tests can be numerous, and their set-up can be repetitive. Luckily, we
  531. can factor out set-up code by implementing a method called
  532. <a class="reference internal" href="#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUp()</span></code></a>, which the testing framework will automatically
  533. call for every single test we run:</p>
  534. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">unittest</span>
  535. <span class="k">class</span> <span class="nc">WidgetTestCase</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
  536. <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  537. <span class="bp">self</span><span class="o">.</span><span class="n">widget</span> <span class="o">=</span> <span class="n">Widget</span><span class="p">(</span><span class="s1">&#39;The widget&#39;</span><span class="p">)</span>
  538. <span class="k">def</span> <span class="nf">test_default_widget_size</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  539. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">size</span><span class="p">(),</span> <span class="p">(</span><span class="mi">50</span><span class="p">,</span><span class="mi">50</span><span class="p">),</span>
  540. <span class="s1">&#39;incorrect default size&#39;</span><span class="p">)</span>
  541. <span class="k">def</span> <span class="nf">test_widget_resize</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  542. <span class="bp">self</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">resize</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span><span class="mi">150</span><span class="p">)</span>
  543. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">size</span><span class="p">(),</span> <span class="p">(</span><span class="mi">100</span><span class="p">,</span><span class="mi">150</span><span class="p">),</span>
  544. <span class="s1">&#39;wrong size after resize&#39;</span><span class="p">)</span>
  545. </pre></div>
  546. </div>
  547. <div class="admonition note">
  548. <p class="admonition-title">Note</p>
  549. <p>The order in which the various tests will be run is determined
  550. by sorting the test method names with respect to the built-in
  551. ordering for strings.</p>
  552. </div>
  553. <p>If the <a class="reference internal" href="#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUp()</span></code></a> method raises an exception while the test is
  554. running, the framework will consider the test to have suffered an error, and
  555. the test method will not be executed.</p>
  556. <p>Similarly, we can provide a <a class="reference internal" href="#unittest.TestCase.tearDown" title="unittest.TestCase.tearDown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDown()</span></code></a> method that tidies up
  557. after the test method has been run:</p>
  558. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">unittest</span>
  559. <span class="k">class</span> <span class="nc">WidgetTestCase</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
  560. <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  561. <span class="bp">self</span><span class="o">.</span><span class="n">widget</span> <span class="o">=</span> <span class="n">Widget</span><span class="p">(</span><span class="s1">&#39;The widget&#39;</span><span class="p">)</span>
  562. <span class="k">def</span> <span class="nf">tearDown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  563. <span class="bp">self</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">dispose</span><span class="p">()</span>
  564. </pre></div>
  565. </div>
  566. <p>If <a class="reference internal" href="#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUp()</span></code></a> succeeded, <a class="reference internal" href="#unittest.TestCase.tearDown" title="unittest.TestCase.tearDown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDown()</span></code></a> will be
  567. run whether the test method succeeded or not.</p>
  568. <p>Such a working environment for the testing code is called a
  569. <em class="dfn">test fixture</em>. A new TestCase instance is created as a unique
  570. test fixture used to execute each individual test method. Thus
  571. <a class="reference internal" href="#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUp()</span></code></a>, <a class="reference internal" href="#unittest.TestCase.tearDown" title="unittest.TestCase.tearDown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDown()</span></code></a>, and <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code>
  572. will be called once per test.</p>
  573. <p>It is recommended that you use TestCase implementations to group tests together
  574. according to the features they test. <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> provides a mechanism for
  575. this: the <em class="dfn">test suite</em>, represented by <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a>’s
  576. <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a> class. In most cases, calling <a class="reference internal" href="#unittest.main" title="unittest.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">unittest.main()</span></code></a> will do
  577. the right thing and collect all the module’s test cases for you and execute
  578. them.</p>
  579. <p>However, should you want to customize the building of your test suite,
  580. you can do it yourself:</p>
  581. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">suite</span><span class="p">():</span>
  582. <span class="n">suite</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestSuite</span><span class="p">()</span>
  583. <span class="n">suite</span><span class="o">.</span><span class="n">addTest</span><span class="p">(</span><span class="n">WidgetTestCase</span><span class="p">(</span><span class="s1">&#39;test_default_widget_size&#39;</span><span class="p">))</span>
  584. <span class="n">suite</span><span class="o">.</span><span class="n">addTest</span><span class="p">(</span><span class="n">WidgetTestCase</span><span class="p">(</span><span class="s1">&#39;test_widget_resize&#39;</span><span class="p">))</span>
  585. <span class="k">return</span> <span class="n">suite</span>
  586. <span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
  587. <span class="n">runner</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TextTestRunner</span><span class="p">()</span>
  588. <span class="n">runner</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">suite</span><span class="p">())</span>
  589. </pre></div>
  590. </div>
  591. <p>You can place the definitions of test cases and test suites in the same modules
  592. as the code they are to test (such as <code class="file docutils literal notranslate"><span class="pre">widget.py</span></code>), but there are several
  593. advantages to placing the test code in a separate module, such as
  594. <code class="file docutils literal notranslate"><span class="pre">test_widget.py</span></code>:</p>
  595. <ul class="simple">
  596. <li><p>The test module can be run standalone from the command line.</p></li>
  597. <li><p>The test code can more easily be separated from shipped code.</p></li>
  598. <li><p>There is less temptation to change test code to fit the code it tests without
  599. a good reason.</p></li>
  600. <li><p>Test code should be modified much less frequently than the code it tests.</p></li>
  601. <li><p>Tested code can be refactored more easily.</p></li>
  602. <li><p>Tests for modules written in C must be in separate modules anyway, so why not
  603. be consistent?</p></li>
  604. <li><p>If the testing strategy changes, there is no need to change the source code.</p></li>
  605. </ul>
  606. </section>
  607. <section id="re-using-old-test-code">
  608. <span id="legacy-unit-tests"></span><h2>Re-using old test code<a class="headerlink" href="#re-using-old-test-code" title="Link to this heading">¶</a></h2>
  609. <p>Some users will find that they have existing test code that they would like to
  610. run from <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a>, without converting every old test function to a
  611. <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> subclass.</p>
  612. <p>For this reason, <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> provides a <a class="reference internal" href="#unittest.FunctionTestCase" title="unittest.FunctionTestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">FunctionTestCase</span></code></a> class.
  613. This subclass of <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> can be used to wrap an existing test
  614. function. Set-up and tear-down functions can also be provided.</p>
  615. <p>Given the following test function:</p>
  616. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">testSomething</span><span class="p">():</span>
  617. <span class="n">something</span> <span class="o">=</span> <span class="n">makeSomething</span><span class="p">()</span>
  618. <span class="k">assert</span> <span class="n">something</span><span class="o">.</span><span class="n">name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
  619. <span class="c1"># ...</span>
  620. </pre></div>
  621. </div>
  622. <p>one can create an equivalent test case instance as follows, with optional
  623. set-up and tear-down methods:</p>
  624. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">testcase</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">FunctionTestCase</span><span class="p">(</span><span class="n">testSomething</span><span class="p">,</span>
  625. <span class="n">setUp</span><span class="o">=</span><span class="n">makeSomethingDB</span><span class="p">,</span>
  626. <span class="n">tearDown</span><span class="o">=</span><span class="n">deleteSomethingDB</span><span class="p">)</span>
  627. </pre></div>
  628. </div>
  629. <div class="admonition note">
  630. <p class="admonition-title">Note</p>
  631. <p>Even though <a class="reference internal" href="#unittest.FunctionTestCase" title="unittest.FunctionTestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">FunctionTestCase</span></code></a> can be used to quickly convert an
  632. existing test base over to a <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a>-based system, this approach is
  633. not recommended. Taking the time to set up proper <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a>
  634. subclasses will make future test refactorings infinitely easier.</p>
  635. </div>
  636. <p>In some cases, the existing tests may have been written using the <a class="reference internal" href="doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a>
  637. module. If so, <a class="reference internal" href="doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> provides a <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestSuite</span></code> class that can
  638. automatically build <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a> instances from the existing
  639. <a class="reference internal" href="doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a>-based tests.</p>
  640. </section>
  641. <section id="skipping-tests-and-expected-failures">
  642. <span id="unittest-skipping"></span><h2>Skipping tests and expected failures<a class="headerlink" href="#skipping-tests-and-expected-failures" title="Link to this heading">¶</a></h2>
  643. <div class="versionadded">
  644. <p><span class="versionmodified added">New in version 3.1.</span></p>
  645. </div>
  646. <p>Unittest supports skipping individual test methods and even whole classes of
  647. tests. In addition, it supports marking a test as an “expected failure,” a test
  648. that is broken and will fail, but shouldn’t be counted as a failure on a
  649. <a class="reference internal" href="#unittest.TestResult" title="unittest.TestResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResult</span></code></a>.</p>
  650. <p>Skipping a test is simply a matter of using the <a class="reference internal" href="#unittest.skip" title="unittest.skip"><code class="xref py py-func docutils literal notranslate"><span class="pre">skip()</span></code></a> <a class="reference internal" href="../glossary.html#term-decorator"><span class="xref std std-term">decorator</span></a>
  651. or one of its conditional variants, calling <a class="reference internal" href="#unittest.TestCase.skipTest" title="unittest.TestCase.skipTest"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TestCase.skipTest()</span></code></a> within a
  652. <a class="reference internal" href="#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUp()</span></code></a> or test method, or raising <a class="reference internal" href="#unittest.SkipTest" title="unittest.SkipTest"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SkipTest</span></code></a> directly.</p>
  653. <p>Basic skipping looks like this:</p>
  654. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyTestCase</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
  655. <span class="nd">@unittest</span><span class="o">.</span><span class="n">skip</span><span class="p">(</span><span class="s2">&quot;demonstrating skipping&quot;</span><span class="p">)</span>
  656. <span class="k">def</span> <span class="nf">test_nothing</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  657. <span class="bp">self</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="s2">&quot;shouldn&#39;t happen&quot;</span><span class="p">)</span>
  658. <span class="nd">@unittest</span><span class="o">.</span><span class="n">skipIf</span><span class="p">(</span><span class="n">mylib</span><span class="o">.</span><span class="n">__version__</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span>
  659. <span class="s2">&quot;not supported in this library version&quot;</span><span class="p">)</span>
  660. <span class="k">def</span> <span class="nf">test_format</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  661. <span class="c1"># Tests that work for only a certain version of the library.</span>
  662. <span class="k">pass</span>
  663. <span class="nd">@unittest</span><span class="o">.</span><span class="n">skipUnless</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">platform</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;win&quot;</span><span class="p">),</span> <span class="s2">&quot;requires Windows&quot;</span><span class="p">)</span>
  664. <span class="k">def</span> <span class="nf">test_windows_support</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  665. <span class="c1"># windows specific testing code</span>
  666. <span class="k">pass</span>
  667. <span class="k">def</span> <span class="nf">test_maybe_skipped</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  668. <span class="k">if</span> <span class="ow">not</span> <span class="n">external_resource_available</span><span class="p">():</span>
  669. <span class="bp">self</span><span class="o">.</span><span class="n">skipTest</span><span class="p">(</span><span class="s2">&quot;external resource not available&quot;</span><span class="p">)</span>
  670. <span class="c1"># test code that depends on the external resource</span>
  671. <span class="k">pass</span>
  672. </pre></div>
  673. </div>
  674. <p>This is the output of running the example above in verbose mode:</p>
  675. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">test_format</span> <span class="p">(</span><span class="n">__main__</span><span class="o">.</span><span class="n">MyTestCase</span><span class="o">.</span><span class="n">test_format</span><span class="p">)</span> <span class="o">...</span> <span class="n">skipped</span> <span class="s1">&#39;not supported in this library version&#39;</span>
  676. <span class="n">test_nothing</span> <span class="p">(</span><span class="n">__main__</span><span class="o">.</span><span class="n">MyTestCase</span><span class="o">.</span><span class="n">test_nothing</span><span class="p">)</span> <span class="o">...</span> <span class="n">skipped</span> <span class="s1">&#39;demonstrating skipping&#39;</span>
  677. <span class="n">test_maybe_skipped</span> <span class="p">(</span><span class="n">__main__</span><span class="o">.</span><span class="n">MyTestCase</span><span class="o">.</span><span class="n">test_maybe_skipped</span><span class="p">)</span> <span class="o">...</span> <span class="n">skipped</span> <span class="s1">&#39;external resource not available&#39;</span>
  678. <span class="n">test_windows_support</span> <span class="p">(</span><span class="n">__main__</span><span class="o">.</span><span class="n">MyTestCase</span><span class="o">.</span><span class="n">test_windows_support</span><span class="p">)</span> <span class="o">...</span> <span class="n">skipped</span> <span class="s1">&#39;requires Windows&#39;</span>
  679. <span class="o">----------------------------------------------------------------------</span>
  680. <span class="n">Ran</span> <span class="mi">4</span> <span class="n">tests</span> <span class="ow">in</span> <span class="mf">0.005</span><span class="n">s</span>
  681. <span class="n">OK</span> <span class="p">(</span><span class="n">skipped</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
  682. </pre></div>
  683. </div>
  684. <p>Classes can be skipped just like methods:</p>
  685. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@unittest</span><span class="o">.</span><span class="n">skip</span><span class="p">(</span><span class="s2">&quot;showing class skipping&quot;</span><span class="p">)</span>
  686. <span class="k">class</span> <span class="nc">MySkippedTestCase</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
  687. <span class="k">def</span> <span class="nf">test_not_run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  688. <span class="k">pass</span>
  689. </pre></div>
  690. </div>
  691. <p><a class="reference internal" href="#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TestCase.setUp()</span></code></a> can also skip the test. This is useful when a resource
  692. that needs to be set up is not available.</p>
  693. <p>Expected failures use the <a class="reference internal" href="#unittest.expectedFailure" title="unittest.expectedFailure"><code class="xref py py-func docutils literal notranslate"><span class="pre">expectedFailure()</span></code></a> decorator.</p>
  694. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ExpectedFailureTestCase</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
  695. <span class="nd">@unittest</span><span class="o">.</span><span class="n">expectedFailure</span>
  696. <span class="k">def</span> <span class="nf">test_fail</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  697. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;broken&quot;</span><span class="p">)</span>
  698. </pre></div>
  699. </div>
  700. <p>It’s easy to roll your own skipping decorators by making a decorator that calls
  701. <a class="reference internal" href="#unittest.skip" title="unittest.skip"><code class="xref py py-func docutils literal notranslate"><span class="pre">skip()</span></code></a> on the test when it wants it to be skipped. This decorator skips
  702. the test unless the passed object has a certain attribute:</p>
  703. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">skipUnlessHasattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">attr</span><span class="p">):</span>
  704. <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">attr</span><span class="p">):</span>
  705. <span class="k">return</span> <span class="k">lambda</span> <span class="n">func</span><span class="p">:</span> <span class="n">func</span>
  706. <span class="k">return</span> <span class="n">unittest</span><span class="o">.</span><span class="n">skip</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{!r}</span><span class="s2"> doesn&#39;t have </span><span class="si">{!r}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">attr</span><span class="p">))</span>
  707. </pre></div>
  708. </div>
  709. <p>The following decorators and exception implement test skipping and expected failures:</p>
  710. <dl class="py function">
  711. <dt class="sig sig-object py" id="unittest.skip">
  712. <span class="sig-prename descclassname"><span class="pre">&#64;</span></span><span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">skip</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">reason</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.skip" title="Link to this definition">¶</a></dt>
  713. <dd><p>Unconditionally skip the decorated test. <em>reason</em> should describe why the
  714. test is being skipped.</p>
  715. </dd></dl>
  716. <dl class="py function">
  717. <dt class="sig sig-object py" id="unittest.skipIf">
  718. <span class="sig-prename descclassname"><span class="pre">&#64;</span></span><span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">skipIf</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">condition</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">reason</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.skipIf" title="Link to this definition">¶</a></dt>
  719. <dd><p>Skip the decorated test if <em>condition</em> is true.</p>
  720. </dd></dl>
  721. <dl class="py function">
  722. <dt class="sig sig-object py" id="unittest.skipUnless">
  723. <span class="sig-prename descclassname"><span class="pre">&#64;</span></span><span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">skipUnless</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">condition</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">reason</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.skipUnless" title="Link to this definition">¶</a></dt>
  724. <dd><p>Skip the decorated test unless <em>condition</em> is true.</p>
  725. </dd></dl>
  726. <dl class="py function">
  727. <dt class="sig sig-object py" id="unittest.expectedFailure">
  728. <span class="sig-prename descclassname"><span class="pre">&#64;</span></span><span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">expectedFailure</span></span><a class="headerlink" href="#unittest.expectedFailure" title="Link to this definition">¶</a></dt>
  729. <dd><p>Mark the test as an expected failure or error. If the test fails or errors
  730. in the test function itself (rather than in one of the <em class="dfn">test fixture</em>
  731. methods) then it will be considered a success. If the test passes, it will
  732. be considered a failure.</p>
  733. </dd></dl>
  734. <dl class="py exception">
  735. <dt class="sig sig-object py" id="unittest.SkipTest">
  736. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">SkipTest</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">reason</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.SkipTest" title="Link to this definition">¶</a></dt>
  737. <dd><p>This exception is raised to skip a test.</p>
  738. <p>Usually you can use <a class="reference internal" href="#unittest.TestCase.skipTest" title="unittest.TestCase.skipTest"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TestCase.skipTest()</span></code></a> or one of the skipping
  739. decorators instead of raising this directly.</p>
  740. </dd></dl>
  741. <p>Skipped tests will not have <a class="reference internal" href="#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUp()</span></code></a> or <a class="reference internal" href="#unittest.TestCase.tearDown" title="unittest.TestCase.tearDown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDown()</span></code></a> run around them.
  742. Skipped classes will not have <a class="reference internal" href="#unittest.TestCase.setUpClass" title="unittest.TestCase.setUpClass"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUpClass()</span></code></a> or <a class="reference internal" href="#unittest.TestCase.tearDownClass" title="unittest.TestCase.tearDownClass"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDownClass()</span></code></a> run.
  743. Skipped modules will not have <code class="xref py py-func docutils literal notranslate"><span class="pre">setUpModule()</span></code> or <code class="xref py py-func docutils literal notranslate"><span class="pre">tearDownModule()</span></code> run.</p>
  744. </section>
  745. <section id="distinguishing-test-iterations-using-subtests">
  746. <span id="subtests"></span><h2>Distinguishing test iterations using subtests<a class="headerlink" href="#distinguishing-test-iterations-using-subtests" title="Link to this heading">¶</a></h2>
  747. <div class="versionadded">
  748. <p><span class="versionmodified added">New in version 3.4.</span></p>
  749. </div>
  750. <p>When there are very small differences among your tests, for
  751. instance some parameters, unittest allows you to distinguish them inside
  752. the body of a test method using the <a class="reference internal" href="#unittest.TestCase.subTest" title="unittest.TestCase.subTest"><code class="xref py py-meth docutils literal notranslate"><span class="pre">subTest()</span></code></a> context manager.</p>
  753. <p>For example, the following test:</p>
  754. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">NumbersTest</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
  755. <span class="k">def</span> <span class="nf">test_even</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  756. <span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
  757. <span class="sd"> Test that numbers between 0 and 5 are all even.</span>
  758. <span class="sd"> &quot;&quot;&quot;</span>
  759. <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">6</span><span class="p">):</span>
  760. <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">subTest</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="n">i</span><span class="p">):</span>
  761. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">i</span> <span class="o">%</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
  762. </pre></div>
  763. </div>
  764. <p>will produce the following output:</p>
  765. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">======================================================================</span>
  766. <span class="n">FAIL</span><span class="p">:</span> <span class="n">test_even</span> <span class="p">(</span><span class="n">__main__</span><span class="o">.</span><span class="n">NumbersTest</span><span class="o">.</span><span class="n">test_even</span><span class="p">)</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
  767. <span class="n">Test</span> <span class="n">that</span> <span class="n">numbers</span> <span class="n">between</span> <span class="mi">0</span> <span class="ow">and</span> <span class="mi">5</span> <span class="n">are</span> <span class="nb">all</span> <span class="n">even</span><span class="o">.</span>
  768. <span class="o">----------------------------------------------------------------------</span>
  769. <span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
  770. <span class="n">File</span> <span class="s2">&quot;subtests.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">11</span><span class="p">,</span> <span class="ow">in</span> <span class="n">test_even</span>
  771. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">i</span> <span class="o">%</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
  772. <span class="o">^^^^^^^^^^^^^^^^^^^^^^^^^^</span>
  773. <span class="ne">AssertionError</span><span class="p">:</span> <span class="mi">1</span> <span class="o">!=</span> <span class="mi">0</span>
  774. <span class="o">======================================================================</span>
  775. <span class="n">FAIL</span><span class="p">:</span> <span class="n">test_even</span> <span class="p">(</span><span class="n">__main__</span><span class="o">.</span><span class="n">NumbersTest</span><span class="o">.</span><span class="n">test_even</span><span class="p">)</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
  776. <span class="n">Test</span> <span class="n">that</span> <span class="n">numbers</span> <span class="n">between</span> <span class="mi">0</span> <span class="ow">and</span> <span class="mi">5</span> <span class="n">are</span> <span class="nb">all</span> <span class="n">even</span><span class="o">.</span>
  777. <span class="o">----------------------------------------------------------------------</span>
  778. <span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
  779. <span class="n">File</span> <span class="s2">&quot;subtests.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">11</span><span class="p">,</span> <span class="ow">in</span> <span class="n">test_even</span>
  780. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">i</span> <span class="o">%</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
  781. <span class="o">^^^^^^^^^^^^^^^^^^^^^^^^^^</span>
  782. <span class="ne">AssertionError</span><span class="p">:</span> <span class="mi">1</span> <span class="o">!=</span> <span class="mi">0</span>
  783. <span class="o">======================================================================</span>
  784. <span class="n">FAIL</span><span class="p">:</span> <span class="n">test_even</span> <span class="p">(</span><span class="n">__main__</span><span class="o">.</span><span class="n">NumbersTest</span><span class="o">.</span><span class="n">test_even</span><span class="p">)</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
  785. <span class="n">Test</span> <span class="n">that</span> <span class="n">numbers</span> <span class="n">between</span> <span class="mi">0</span> <span class="ow">and</span> <span class="mi">5</span> <span class="n">are</span> <span class="nb">all</span> <span class="n">even</span><span class="o">.</span>
  786. <span class="o">----------------------------------------------------------------------</span>
  787. <span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
  788. <span class="n">File</span> <span class="s2">&quot;subtests.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">11</span><span class="p">,</span> <span class="ow">in</span> <span class="n">test_even</span>
  789. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">i</span> <span class="o">%</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
  790. <span class="o">^^^^^^^^^^^^^^^^^^^^^^^^^^</span>
  791. <span class="ne">AssertionError</span><span class="p">:</span> <span class="mi">1</span> <span class="o">!=</span> <span class="mi">0</span>
  792. </pre></div>
  793. </div>
  794. <p>Without using a subtest, execution would stop after the first failure,
  795. and the error would be less easy to diagnose because the value of <code class="docutils literal notranslate"><span class="pre">i</span></code>
  796. wouldn’t be displayed:</p>
  797. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">======================================================================</span>
  798. <span class="n">FAIL</span><span class="p">:</span> <span class="n">test_even</span> <span class="p">(</span><span class="n">__main__</span><span class="o">.</span><span class="n">NumbersTest</span><span class="o">.</span><span class="n">test_even</span><span class="p">)</span>
  799. <span class="o">----------------------------------------------------------------------</span>
  800. <span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
  801. <span class="n">File</span> <span class="s2">&quot;subtests.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">32</span><span class="p">,</span> <span class="ow">in</span> <span class="n">test_even</span>
  802. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">i</span> <span class="o">%</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
  803. <span class="ne">AssertionError</span><span class="p">:</span> <span class="mi">1</span> <span class="o">!=</span> <span class="mi">0</span>
  804. </pre></div>
  805. </div>
  806. </section>
  807. <section id="classes-and-functions">
  808. <span id="unittest-contents"></span><h2>Classes and functions<a class="headerlink" href="#classes-and-functions" title="Link to this heading">¶</a></h2>
  809. <p>This section describes in depth the API of <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a>.</p>
  810. <section id="test-cases">
  811. <span id="testcase-objects"></span><h3>Test cases<a class="headerlink" href="#test-cases" title="Link to this heading">¶</a></h3>
  812. <dl class="py class">
  813. <dt class="sig sig-object py" id="unittest.TestCase">
  814. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">TestCase</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">methodName</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'runTest'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase" title="Link to this definition">¶</a></dt>
  815. <dd><p>Instances of the <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> class represent the logical test units
  816. in the <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> universe. This class is intended to be used as a base
  817. class, with specific tests being implemented by concrete subclasses. This class
  818. implements the interface needed by the test runner to allow it to drive the
  819. tests, and methods that the test code can use to check for and report various
  820. kinds of failure.</p>
  821. <p>Each instance of <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> will run a single base method: the method
  822. named <em>methodName</em>.
  823. In most uses of <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a>, you will neither change
  824. the <em>methodName</em> nor reimplement the default <code class="docutils literal notranslate"><span class="pre">runTest()</span></code> method.</p>
  825. <div class="versionchanged">
  826. <p><span class="versionmodified changed">Changed in version 3.2: </span><a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> can be instantiated successfully without providing a
  827. <em>methodName</em>. This makes it easier to experiment with <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a>
  828. from the interactive interpreter.</p>
  829. </div>
  830. <p><a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> instances provide three groups of methods: one group used
  831. to run the test, another used by the test implementation to check conditions
  832. and report failures, and some inquiry methods allowing information about the
  833. test itself to be gathered.</p>
  834. <p>Methods in the first group (running the test) are:</p>
  835. <dl class="py method">
  836. <dt class="sig sig-object py" id="unittest.TestCase.setUp">
  837. <span class="sig-name descname"><span class="pre">setUp</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.setUp" title="Link to this definition">¶</a></dt>
  838. <dd><p>Method called to prepare the test fixture. This is called immediately
  839. before calling the test method; other than <a class="reference internal" href="exceptions.html#AssertionError" title="AssertionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AssertionError</span></code></a> or <a class="reference internal" href="#unittest.SkipTest" title="unittest.SkipTest"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SkipTest</span></code></a>,
  840. any exception raised by this method will be considered an error rather than
  841. a test failure. The default implementation does nothing.</p>
  842. </dd></dl>
  843. <dl class="py method">
  844. <dt class="sig sig-object py" id="unittest.TestCase.tearDown">
  845. <span class="sig-name descname"><span class="pre">tearDown</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.tearDown" title="Link to this definition">¶</a></dt>
  846. <dd><p>Method called immediately after the test method has been called and the
  847. result recorded. This is called even if the test method raised an
  848. exception, so the implementation in subclasses may need to be particularly
  849. careful about checking internal state. Any exception, other than
  850. <a class="reference internal" href="exceptions.html#AssertionError" title="AssertionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AssertionError</span></code></a> or <a class="reference internal" href="#unittest.SkipTest" title="unittest.SkipTest"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SkipTest</span></code></a>, raised by this method will be
  851. considered an additional error rather than a test failure (thus increasing
  852. the total number of reported errors). This method will only be called if
  853. the <a class="reference internal" href="#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUp()</span></code></a> succeeds, regardless of the outcome of the test method.
  854. The default implementation does nothing.</p>
  855. </dd></dl>
  856. <dl class="py method">
  857. <dt class="sig sig-object py" id="unittest.TestCase.setUpClass">
  858. <span class="sig-name descname"><span class="pre">setUpClass</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.setUpClass" title="Link to this definition">¶</a></dt>
  859. <dd><p>A class method called before tests in an individual class are run.
  860. <code class="docutils literal notranslate"><span class="pre">setUpClass</span></code> is called with the class as the only argument
  861. and must be decorated as a <a class="reference internal" href="functions.html#classmethod" title="classmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">classmethod()</span></code></a>:</p>
  862. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@classmethod</span>
  863. <span class="k">def</span> <span class="nf">setUpClass</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
  864. <span class="o">...</span>
  865. </pre></div>
  866. </div>
  867. <p>See <a class="reference internal" href="#class-and-module-fixtures">Class and Module Fixtures</a> for more details.</p>
  868. <div class="versionadded">
  869. <p><span class="versionmodified added">New in version 3.2.</span></p>
  870. </div>
  871. </dd></dl>
  872. <dl class="py method">
  873. <dt class="sig sig-object py" id="unittest.TestCase.tearDownClass">
  874. <span class="sig-name descname"><span class="pre">tearDownClass</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.tearDownClass" title="Link to this definition">¶</a></dt>
  875. <dd><p>A class method called after tests in an individual class have run.
  876. <code class="docutils literal notranslate"><span class="pre">tearDownClass</span></code> is called with the class as the only argument
  877. and must be decorated as a <a class="reference internal" href="functions.html#classmethod" title="classmethod"><code class="xref py py-meth docutils literal notranslate"><span class="pre">classmethod()</span></code></a>:</p>
  878. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@classmethod</span>
  879. <span class="k">def</span> <span class="nf">tearDownClass</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
  880. <span class="o">...</span>
  881. </pre></div>
  882. </div>
  883. <p>See <a class="reference internal" href="#class-and-module-fixtures">Class and Module Fixtures</a> for more details.</p>
  884. <div class="versionadded">
  885. <p><span class="versionmodified added">New in version 3.2.</span></p>
  886. </div>
  887. </dd></dl>
  888. <dl class="py method">
  889. <dt class="sig sig-object py" id="unittest.TestCase.run">
  890. <span class="sig-name descname"><span class="pre">run</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">result</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.run" title="Link to this definition">¶</a></dt>
  891. <dd><p>Run the test, collecting the result into the <a class="reference internal" href="#unittest.TestResult" title="unittest.TestResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResult</span></code></a> object
  892. passed as <em>result</em>. If <em>result</em> is omitted or <code class="docutils literal notranslate"><span class="pre">None</span></code>, a temporary
  893. result object is created (by calling the <a class="reference internal" href="#unittest.TestCase.defaultTestResult" title="unittest.TestCase.defaultTestResult"><code class="xref py py-meth docutils literal notranslate"><span class="pre">defaultTestResult()</span></code></a>
  894. method) and used. The result object is returned to <a class="reference internal" href="#unittest.TestCase.run" title="unittest.TestCase.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a>’s
  895. caller.</p>
  896. <p>The same effect may be had by simply calling the <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a>
  897. instance.</p>
  898. <div class="versionchanged">
  899. <p><span class="versionmodified changed">Changed in version 3.3: </span>Previous versions of <code class="docutils literal notranslate"><span class="pre">run</span></code> did not return the result. Neither did
  900. calling an instance.</p>
  901. </div>
  902. </dd></dl>
  903. <dl class="py method">
  904. <dt class="sig sig-object py" id="unittest.TestCase.skipTest">
  905. <span class="sig-name descname"><span class="pre">skipTest</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">reason</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.skipTest" title="Link to this definition">¶</a></dt>
  906. <dd><p>Calling this during a test method or <a class="reference internal" href="#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUp()</span></code></a> skips the current
  907. test. See <a class="reference internal" href="#unittest-skipping"><span class="std std-ref">Skipping tests and expected failures</span></a> for more information.</p>
  908. <div class="versionadded">
  909. <p><span class="versionmodified added">New in version 3.1.</span></p>
  910. </div>
  911. </dd></dl>
  912. <dl class="py method">
  913. <dt class="sig sig-object py" id="unittest.TestCase.subTest">
  914. <span class="sig-name descname"><span class="pre">subTest</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">params</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.subTest" title="Link to this definition">¶</a></dt>
  915. <dd><p>Return a context manager which executes the enclosed code block as a
  916. subtest. <em>msg</em> and <em>params</em> are optional, arbitrary values which are
  917. displayed whenever a subtest fails, allowing you to identify them
  918. clearly.</p>
  919. <p>A test case can contain any number of subtest declarations, and
  920. they can be arbitrarily nested.</p>
  921. <p>See <a class="reference internal" href="#subtests"><span class="std std-ref">Distinguishing test iterations using subtests</span></a> for more information.</p>
  922. <div class="versionadded">
  923. <p><span class="versionmodified added">New in version 3.4.</span></p>
  924. </div>
  925. </dd></dl>
  926. <dl class="py method">
  927. <dt class="sig sig-object py" id="unittest.TestCase.debug">
  928. <span class="sig-name descname"><span class="pre">debug</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.debug" title="Link to this definition">¶</a></dt>
  929. <dd><p>Run the test without collecting the result. This allows exceptions raised
  930. by the test to be propagated to the caller, and can be used to support
  931. running tests under a debugger.</p>
  932. </dd></dl>
  933. <p id="assert-methods">The <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> class provides several assert methods to check for and
  934. report failures. The following table lists the most commonly used methods
  935. (see the tables below for more assert methods):</p>
  936. <table class="docutils align-default">
  937. <thead>
  938. <tr class="row-odd"><th class="head"><p>Method</p></th>
  939. <th class="head"><p>Checks that</p></th>
  940. <th class="head"><p>New in</p></th>
  941. </tr>
  942. </thead>
  943. <tbody>
  944. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual(a,</span> <span class="pre">b)</span></code></a></p></td>
  945. <td><p><code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">==</span> <span class="pre">b</span></code></p></td>
  946. <td></td>
  947. </tr>
  948. <tr class="row-odd"><td><p><a class="reference internal" href="#unittest.TestCase.assertNotEqual" title="unittest.TestCase.assertNotEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotEqual(a,</span> <span class="pre">b)</span></code></a></p></td>
  949. <td><p><code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">!=</span> <span class="pre">b</span></code></p></td>
  950. <td></td>
  951. </tr>
  952. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertTrue" title="unittest.TestCase.assertTrue"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertTrue(x)</span></code></a></p></td>
  953. <td><p><code class="docutils literal notranslate"><span class="pre">bool(x)</span> <span class="pre">is</span> <span class="pre">True</span></code></p></td>
  954. <td></td>
  955. </tr>
  956. <tr class="row-odd"><td><p><a class="reference internal" href="#unittest.TestCase.assertFalse" title="unittest.TestCase.assertFalse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertFalse(x)</span></code></a></p></td>
  957. <td><p><code class="docutils literal notranslate"><span class="pre">bool(x)</span> <span class="pre">is</span> <span class="pre">False</span></code></p></td>
  958. <td></td>
  959. </tr>
  960. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertIs" title="unittest.TestCase.assertIs"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertIs(a,</span> <span class="pre">b)</span></code></a></p></td>
  961. <td><p><code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">is</span> <span class="pre">b</span></code></p></td>
  962. <td><p>3.1</p></td>
  963. </tr>
  964. <tr class="row-odd"><td><p><a class="reference internal" href="#unittest.TestCase.assertIsNot" title="unittest.TestCase.assertIsNot"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertIsNot(a,</span> <span class="pre">b)</span></code></a></p></td>
  965. <td><p><code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">is</span> <span class="pre">not</span> <span class="pre">b</span></code></p></td>
  966. <td><p>3.1</p></td>
  967. </tr>
  968. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertIsNone" title="unittest.TestCase.assertIsNone"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertIsNone(x)</span></code></a></p></td>
  969. <td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">is</span> <span class="pre">None</span></code></p></td>
  970. <td><p>3.1</p></td>
  971. </tr>
  972. <tr class="row-odd"><td><p><a class="reference internal" href="#unittest.TestCase.assertIsNotNone" title="unittest.TestCase.assertIsNotNone"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertIsNotNone(x)</span></code></a></p></td>
  973. <td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">is</span> <span class="pre">not</span> <span class="pre">None</span></code></p></td>
  974. <td><p>3.1</p></td>
  975. </tr>
  976. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertIn" title="unittest.TestCase.assertIn"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertIn(a,</span> <span class="pre">b)</span></code></a></p></td>
  977. <td><p><code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">in</span> <span class="pre">b</span></code></p></td>
  978. <td><p>3.1</p></td>
  979. </tr>
  980. <tr class="row-odd"><td><p><a class="reference internal" href="#unittest.TestCase.assertNotIn" title="unittest.TestCase.assertNotIn"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotIn(a,</span> <span class="pre">b)</span></code></a></p></td>
  981. <td><p><code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">not</span> <span class="pre">in</span> <span class="pre">b</span></code></p></td>
  982. <td><p>3.1</p></td>
  983. </tr>
  984. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertIsInstance" title="unittest.TestCase.assertIsInstance"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertIsInstance(a,</span> <span class="pre">b)</span></code></a></p></td>
  985. <td><p><code class="docutils literal notranslate"><span class="pre">isinstance(a,</span> <span class="pre">b)</span></code></p></td>
  986. <td><p>3.2</p></td>
  987. </tr>
  988. <tr class="row-odd"><td><p><a class="reference internal" href="#unittest.TestCase.assertNotIsInstance" title="unittest.TestCase.assertNotIsInstance"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotIsInstance(a,</span> <span class="pre">b)</span></code></a></p></td>
  989. <td><p><code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">isinstance(a,</span> <span class="pre">b)</span></code></p></td>
  990. <td><p>3.2</p></td>
  991. </tr>
  992. </tbody>
  993. </table>
  994. <p>All the assert methods accept a <em>msg</em> argument that, if specified, is used
  995. as the error message on failure (see also <a class="reference internal" href="#unittest.TestCase.longMessage" title="unittest.TestCase.longMessage"><code class="xref py py-data docutils literal notranslate"><span class="pre">longMessage</span></code></a>).
  996. Note that the <em>msg</em> keyword argument can be passed to <a class="reference internal" href="#unittest.TestCase.assertRaises" title="unittest.TestCase.assertRaises"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRaises()</span></code></a>,
  997. <a class="reference internal" href="#unittest.TestCase.assertRaisesRegex" title="unittest.TestCase.assertRaisesRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRaisesRegex()</span></code></a>, <a class="reference internal" href="#unittest.TestCase.assertWarns" title="unittest.TestCase.assertWarns"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertWarns()</span></code></a>, <a class="reference internal" href="#unittest.TestCase.assertWarnsRegex" title="unittest.TestCase.assertWarnsRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertWarnsRegex()</span></code></a>
  998. only when they are used as a context manager.</p>
  999. <dl class="py method">
  1000. <dt class="sig sig-object py" id="unittest.TestCase.assertEqual">
  1001. <span class="sig-name descname"><span class="pre">assertEqual</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertEqual" title="Link to this definition">¶</a></dt>
  1002. <dd><p>Test that <em>first</em> and <em>second</em> are equal. If the values do not
  1003. compare equal, the test will fail.</p>
  1004. <p>In addition, if <em>first</em> and <em>second</em> are the exact same type and one of
  1005. list, tuple, dict, set, frozenset or str or any type that a subclass
  1006. registers with <a class="reference internal" href="#unittest.TestCase.addTypeEqualityFunc" title="unittest.TestCase.addTypeEqualityFunc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addTypeEqualityFunc()</span></code></a> the type-specific equality
  1007. function will be called in order to generate a more useful default
  1008. error message (see also the <a class="reference internal" href="#type-specific-methods"><span class="std std-ref">list of type-specific methods</span></a>).</p>
  1009. <div class="versionchanged">
  1010. <p><span class="versionmodified changed">Changed in version 3.1: </span>Added the automatic calling of type-specific equality function.</p>
  1011. </div>
  1012. <div class="versionchanged">
  1013. <p><span class="versionmodified changed">Changed in version 3.2: </span><a class="reference internal" href="#unittest.TestCase.assertMultiLineEqual" title="unittest.TestCase.assertMultiLineEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertMultiLineEqual()</span></code></a> added as the default type equality
  1014. function for comparing strings.</p>
  1015. </div>
  1016. </dd></dl>
  1017. <dl class="py method">
  1018. <dt class="sig sig-object py" id="unittest.TestCase.assertNotEqual">
  1019. <span class="sig-name descname"><span class="pre">assertNotEqual</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertNotEqual" title="Link to this definition">¶</a></dt>
  1020. <dd><p>Test that <em>first</em> and <em>second</em> are not equal. If the values do
  1021. compare equal, the test will fail.</p>
  1022. </dd></dl>
  1023. <dl class="py method">
  1024. <dt class="sig sig-object py" id="unittest.TestCase.assertTrue">
  1025. <span class="sig-name descname"><span class="pre">assertTrue</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertTrue" title="Link to this definition">¶</a></dt>
  1026. <dt class="sig sig-object py" id="unittest.TestCase.assertFalse">
  1027. <span class="sig-name descname"><span class="pre">assertFalse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertFalse" title="Link to this definition">¶</a></dt>
  1028. <dd><p>Test that <em>expr</em> is true (or false).</p>
  1029. <p>Note that this is equivalent to <code class="docutils literal notranslate"><span class="pre">bool(expr)</span> <span class="pre">is</span> <span class="pre">True</span></code> and not to <code class="docutils literal notranslate"><span class="pre">expr</span>
  1030. <span class="pre">is</span> <span class="pre">True</span></code> (use <code class="docutils literal notranslate"><span class="pre">assertIs(expr,</span> <span class="pre">True)</span></code> for the latter). This method
  1031. should also be avoided when more specific methods are available (e.g.
  1032. <code class="docutils literal notranslate"><span class="pre">assertEqual(a,</span> <span class="pre">b)</span></code> instead of <code class="docutils literal notranslate"><span class="pre">assertTrue(a</span> <span class="pre">==</span> <span class="pre">b)</span></code>), because they
  1033. provide a better error message in case of failure.</p>
  1034. </dd></dl>
  1035. <dl class="py method">
  1036. <dt class="sig sig-object py" id="unittest.TestCase.assertIs">
  1037. <span class="sig-name descname"><span class="pre">assertIs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertIs" title="Link to this definition">¶</a></dt>
  1038. <dt class="sig sig-object py" id="unittest.TestCase.assertIsNot">
  1039. <span class="sig-name descname"><span class="pre">assertIsNot</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertIsNot" title="Link to this definition">¶</a></dt>
  1040. <dd><p>Test that <em>first</em> and <em>second</em> are (or are not) the same object.</p>
  1041. <div class="versionadded">
  1042. <p><span class="versionmodified added">New in version 3.1.</span></p>
  1043. </div>
  1044. </dd></dl>
  1045. <dl class="py method">
  1046. <dt class="sig sig-object py" id="unittest.TestCase.assertIsNone">
  1047. <span class="sig-name descname"><span class="pre">assertIsNone</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertIsNone" title="Link to this definition">¶</a></dt>
  1048. <dt class="sig sig-object py" id="unittest.TestCase.assertIsNotNone">
  1049. <span class="sig-name descname"><span class="pre">assertIsNotNone</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertIsNotNone" title="Link to this definition">¶</a></dt>
  1050. <dd><p>Test that <em>expr</em> is (or is not) <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1051. <div class="versionadded">
  1052. <p><span class="versionmodified added">New in version 3.1.</span></p>
  1053. </div>
  1054. </dd></dl>
  1055. <dl class="py method">
  1056. <dt class="sig sig-object py" id="unittest.TestCase.assertIn">
  1057. <span class="sig-name descname"><span class="pre">assertIn</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">member</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">container</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertIn" title="Link to this definition">¶</a></dt>
  1058. <dt class="sig sig-object py" id="unittest.TestCase.assertNotIn">
  1059. <span class="sig-name descname"><span class="pre">assertNotIn</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">member</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">container</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertNotIn" title="Link to this definition">¶</a></dt>
  1060. <dd><p>Test that <em>member</em> is (or is not) in <em>container</em>.</p>
  1061. <div class="versionadded">
  1062. <p><span class="versionmodified added">New in version 3.1.</span></p>
  1063. </div>
  1064. </dd></dl>
  1065. <dl class="py method">
  1066. <dt class="sig sig-object py" id="unittest.TestCase.assertIsInstance">
  1067. <span class="sig-name descname"><span class="pre">assertIsInstance</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cls</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertIsInstance" title="Link to this definition">¶</a></dt>
  1068. <dt class="sig sig-object py" id="unittest.TestCase.assertNotIsInstance">
  1069. <span class="sig-name descname"><span class="pre">assertNotIsInstance</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cls</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertNotIsInstance" title="Link to this definition">¶</a></dt>
  1070. <dd><p>Test that <em>obj</em> is (or is not) an instance of <em>cls</em> (which can be a
  1071. class or a tuple of classes, as supported by <a class="reference internal" href="functions.html#isinstance" title="isinstance"><code class="xref py py-func docutils literal notranslate"><span class="pre">isinstance()</span></code></a>).
  1072. To check for the exact type, use <a class="reference internal" href="#unittest.TestCase.assertIs" title="unittest.TestCase.assertIs"><code class="xref py py-func docutils literal notranslate"><span class="pre">assertIs(type(obj),</span> <span class="pre">cls)</span></code></a>.</p>
  1073. <div class="versionadded">
  1074. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1075. </div>
  1076. </dd></dl>
  1077. <p>It is also possible to check the production of exceptions, warnings, and
  1078. log messages using the following methods:</p>
  1079. <table class="docutils align-default">
  1080. <thead>
  1081. <tr class="row-odd"><th class="head"><p>Method</p></th>
  1082. <th class="head"><p>Checks that</p></th>
  1083. <th class="head"><p>New in</p></th>
  1084. </tr>
  1085. </thead>
  1086. <tbody>
  1087. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertRaises" title="unittest.TestCase.assertRaises"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRaises(exc,</span> <span class="pre">fun,</span> <span class="pre">*args,</span> <span class="pre">**kwds)</span></code></a></p></td>
  1088. <td><p><code class="docutils literal notranslate"><span class="pre">fun(*args,</span> <span class="pre">**kwds)</span></code> raises <em>exc</em></p></td>
  1089. <td></td>
  1090. </tr>
  1091. <tr class="row-odd"><td><p><a class="reference internal" href="#unittest.TestCase.assertRaisesRegex" title="unittest.TestCase.assertRaisesRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRaisesRegex(exc,</span> <span class="pre">r,</span> <span class="pre">fun,</span> <span class="pre">*args,</span> <span class="pre">**kwds)</span></code></a></p></td>
  1092. <td><p><code class="docutils literal notranslate"><span class="pre">fun(*args,</span> <span class="pre">**kwds)</span></code> raises <em>exc</em>
  1093. and the message matches regex <em>r</em></p></td>
  1094. <td><p>3.1</p></td>
  1095. </tr>
  1096. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertWarns" title="unittest.TestCase.assertWarns"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertWarns(warn,</span> <span class="pre">fun,</span> <span class="pre">*args,</span> <span class="pre">**kwds)</span></code></a></p></td>
  1097. <td><p><code class="docutils literal notranslate"><span class="pre">fun(*args,</span> <span class="pre">**kwds)</span></code> raises <em>warn</em></p></td>
  1098. <td><p>3.2</p></td>
  1099. </tr>
  1100. <tr class="row-odd"><td><p><a class="reference internal" href="#unittest.TestCase.assertWarnsRegex" title="unittest.TestCase.assertWarnsRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertWarnsRegex(warn,</span> <span class="pre">r,</span> <span class="pre">fun,</span> <span class="pre">*args,</span> <span class="pre">**kwds)</span></code></a></p></td>
  1101. <td><p><code class="docutils literal notranslate"><span class="pre">fun(*args,</span> <span class="pre">**kwds)</span></code> raises <em>warn</em>
  1102. and the message matches regex <em>r</em></p></td>
  1103. <td><p>3.2</p></td>
  1104. </tr>
  1105. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertLogs" title="unittest.TestCase.assertLogs"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertLogs(logger,</span> <span class="pre">level)</span></code></a></p></td>
  1106. <td><p>The <code class="docutils literal notranslate"><span class="pre">with</span></code> block logs on <em>logger</em>
  1107. with minimum <em>level</em></p></td>
  1108. <td><p>3.4</p></td>
  1109. </tr>
  1110. <tr class="row-odd"><td><p><a class="reference internal" href="#unittest.TestCase.assertNoLogs" title="unittest.TestCase.assertNoLogs"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNoLogs(logger,</span> <span class="pre">level)</span></code></a></p></td>
  1111. <td><dl class="simple">
  1112. <dt>The <code class="docutils literal notranslate"><span class="pre">with</span></code> block does not log on</dt><dd><p><em>logger</em> with minimum <em>level</em></p>
  1113. </dd>
  1114. </dl>
  1115. </td>
  1116. <td><p>3.10</p></td>
  1117. </tr>
  1118. </tbody>
  1119. </table>
  1120. <dl class="py method">
  1121. <dt class="sig sig-object py" id="unittest.TestCase.assertRaises">
  1122. <span class="sig-name descname"><span class="pre">assertRaises</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exception</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">callable</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwds</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertRaises" title="Link to this definition">¶</a></dt>
  1123. <dt class="sig sig-object py">
  1124. <span class="sig-name descname"><span class="pre">assertRaises</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exception</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span></dt>
  1125. <dd><p>Test that an exception is raised when <em>callable</em> is called with any
  1126. positional or keyword arguments that are also passed to
  1127. <a class="reference internal" href="#unittest.TestCase.assertRaises" title="unittest.TestCase.assertRaises"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRaises()</span></code></a>. The test passes if <em>exception</em> is raised, is an
  1128. error if another exception is raised, or fails if no exception is raised.
  1129. To catch any of a group of exceptions, a tuple containing the exception
  1130. classes may be passed as <em>exception</em>.</p>
  1131. <p>If only the <em>exception</em> and possibly the <em>msg</em> arguments are given,
  1132. return a context manager so that the code under test can be written
  1133. inline rather than as a function:</p>
  1134. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="n">SomeException</span><span class="p">):</span>
  1135. <span class="n">do_something</span><span class="p">()</span>
  1136. </pre></div>
  1137. </div>
  1138. <p>When used as a context manager, <a class="reference internal" href="#unittest.TestCase.assertRaises" title="unittest.TestCase.assertRaises"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRaises()</span></code></a> accepts the
  1139. additional keyword argument <em>msg</em>.</p>
  1140. <p>The context manager will store the caught exception object in its
  1141. <code class="xref py py-attr docutils literal notranslate"><span class="pre">exception</span></code> attribute. This can be useful if the intention
  1142. is to perform additional checks on the exception raised:</p>
  1143. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="n">SomeException</span><span class="p">)</span> <span class="k">as</span> <span class="n">cm</span><span class="p">:</span>
  1144. <span class="n">do_something</span><span class="p">()</span>
  1145. <span class="n">the_exception</span> <span class="o">=</span> <span class="n">cm</span><span class="o">.</span><span class="n">exception</span>
  1146. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">the_exception</span><span class="o">.</span><span class="n">error_code</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
  1147. </pre></div>
  1148. </div>
  1149. <div class="versionchanged">
  1150. <p><span class="versionmodified changed">Changed in version 3.1: </span>Added the ability to use <a class="reference internal" href="#unittest.TestCase.assertRaises" title="unittest.TestCase.assertRaises"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRaises()</span></code></a> as a context manager.</p>
  1151. </div>
  1152. <div class="versionchanged">
  1153. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added the <code class="xref py py-attr docutils literal notranslate"><span class="pre">exception</span></code> attribute.</p>
  1154. </div>
  1155. <div class="versionchanged">
  1156. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>msg</em> keyword argument when used as a context manager.</p>
  1157. </div>
  1158. </dd></dl>
  1159. <dl class="py method">
  1160. <dt class="sig sig-object py" id="unittest.TestCase.assertRaisesRegex">
  1161. <span class="sig-name descname"><span class="pre">assertRaisesRegex</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exception</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">regex</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">callable</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwds</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertRaisesRegex" title="Link to this definition">¶</a></dt>
  1162. <dt class="sig sig-object py">
  1163. <span class="sig-name descname"><span class="pre">assertRaisesRegex</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exception</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">regex</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span></dt>
  1164. <dd><p>Like <a class="reference internal" href="#unittest.TestCase.assertRaises" title="unittest.TestCase.assertRaises"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRaises()</span></code></a> but also tests that <em>regex</em> matches
  1165. on the string representation of the raised exception. <em>regex</em> may be
  1166. a regular expression object or a string containing a regular expression
  1167. suitable for use by <a class="reference internal" href="re.html#re.search" title="re.search"><code class="xref py py-func docutils literal notranslate"><span class="pre">re.search()</span></code></a>. Examples:</p>
  1168. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="bp">self</span><span class="o">.</span><span class="n">assertRaisesRegex</span><span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="s2">&quot;invalid literal for.*XYZ&#39;$&quot;</span><span class="p">,</span>
  1169. <span class="nb">int</span><span class="p">,</span> <span class="s1">&#39;XYZ&#39;</span><span class="p">)</span>
  1170. </pre></div>
  1171. </div>
  1172. <p>or:</p>
  1173. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaisesRegex</span><span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="s1">&#39;literal&#39;</span><span class="p">):</span>
  1174. <span class="nb">int</span><span class="p">(</span><span class="s1">&#39;XYZ&#39;</span><span class="p">)</span>
  1175. </pre></div>
  1176. </div>
  1177. <div class="versionadded">
  1178. <p><span class="versionmodified added">New in version 3.1: </span>Added under the name <code class="docutils literal notranslate"><span class="pre">assertRaisesRegexp</span></code>.</p>
  1179. </div>
  1180. <div class="versionchanged">
  1181. <p><span class="versionmodified changed">Changed in version 3.2: </span>Renamed to <a class="reference internal" href="#unittest.TestCase.assertRaisesRegex" title="unittest.TestCase.assertRaisesRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRaisesRegex()</span></code></a>.</p>
  1182. </div>
  1183. <div class="versionchanged">
  1184. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>msg</em> keyword argument when used as a context manager.</p>
  1185. </div>
  1186. </dd></dl>
  1187. <dl class="py method">
  1188. <dt class="sig sig-object py" id="unittest.TestCase.assertWarns">
  1189. <span class="sig-name descname"><span class="pre">assertWarns</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">warning</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">callable</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwds</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertWarns" title="Link to this definition">¶</a></dt>
  1190. <dt class="sig sig-object py">
  1191. <span class="sig-name descname"><span class="pre">assertWarns</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">warning</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span></dt>
  1192. <dd><p>Test that a warning is triggered when <em>callable</em> is called with any
  1193. positional or keyword arguments that are also passed to
  1194. <a class="reference internal" href="#unittest.TestCase.assertWarns" title="unittest.TestCase.assertWarns"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertWarns()</span></code></a>. The test passes if <em>warning</em> is triggered and
  1195. fails if it isn’t. Any exception is an error.
  1196. To catch any of a group of warnings, a tuple containing the warning
  1197. classes may be passed as <em>warnings</em>.</p>
  1198. <p>If only the <em>warning</em> and possibly the <em>msg</em> arguments are given,
  1199. return a context manager so that the code under test can be written
  1200. inline rather than as a function:</p>
  1201. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertWarns</span><span class="p">(</span><span class="n">SomeWarning</span><span class="p">):</span>
  1202. <span class="n">do_something</span><span class="p">()</span>
  1203. </pre></div>
  1204. </div>
  1205. <p>When used as a context manager, <a class="reference internal" href="#unittest.TestCase.assertWarns" title="unittest.TestCase.assertWarns"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertWarns()</span></code></a> accepts the
  1206. additional keyword argument <em>msg</em>.</p>
  1207. <p>The context manager will store the caught warning object in its
  1208. <code class="xref py py-attr docutils literal notranslate"><span class="pre">warning</span></code> attribute, and the source line which triggered the
  1209. warnings in the <code class="xref py py-attr docutils literal notranslate"><span class="pre">filename</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">lineno</span></code> attributes.
  1210. This can be useful if the intention is to perform additional checks
  1211. on the warning caught:</p>
  1212. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertWarns</span><span class="p">(</span><span class="n">SomeWarning</span><span class="p">)</span> <span class="k">as</span> <span class="n">cm</span><span class="p">:</span>
  1213. <span class="n">do_something</span><span class="p">()</span>
  1214. <span class="bp">self</span><span class="o">.</span><span class="n">assertIn</span><span class="p">(</span><span class="s1">&#39;myfile.py&#39;</span><span class="p">,</span> <span class="n">cm</span><span class="o">.</span><span class="n">filename</span><span class="p">)</span>
  1215. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="mi">320</span><span class="p">,</span> <span class="n">cm</span><span class="o">.</span><span class="n">lineno</span><span class="p">)</span>
  1216. </pre></div>
  1217. </div>
  1218. <p>This method works regardless of the warning filters in place when it
  1219. is called.</p>
  1220. <div class="versionadded">
  1221. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1222. </div>
  1223. <div class="versionchanged">
  1224. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>msg</em> keyword argument when used as a context manager.</p>
  1225. </div>
  1226. </dd></dl>
  1227. <dl class="py method">
  1228. <dt class="sig sig-object py" id="unittest.TestCase.assertWarnsRegex">
  1229. <span class="sig-name descname"><span class="pre">assertWarnsRegex</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">warning</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">regex</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">callable</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwds</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertWarnsRegex" title="Link to this definition">¶</a></dt>
  1230. <dt class="sig sig-object py">
  1231. <span class="sig-name descname"><span class="pre">assertWarnsRegex</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">warning</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">regex</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span></dt>
  1232. <dd><p>Like <a class="reference internal" href="#unittest.TestCase.assertWarns" title="unittest.TestCase.assertWarns"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertWarns()</span></code></a> but also tests that <em>regex</em> matches on the
  1233. message of the triggered warning. <em>regex</em> may be a regular expression
  1234. object or a string containing a regular expression suitable for use
  1235. by <a class="reference internal" href="re.html#re.search" title="re.search"><code class="xref py py-func docutils literal notranslate"><span class="pre">re.search()</span></code></a>. Example:</p>
  1236. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="bp">self</span><span class="o">.</span><span class="n">assertWarnsRegex</span><span class="p">(</span><span class="ne">DeprecationWarning</span><span class="p">,</span>
  1237. <span class="sa">r</span><span class="s1">&#39;legacy_function\(\) is deprecated&#39;</span><span class="p">,</span>
  1238. <span class="n">legacy_function</span><span class="p">,</span> <span class="s1">&#39;XYZ&#39;</span><span class="p">)</span>
  1239. </pre></div>
  1240. </div>
  1241. <p>or:</p>
  1242. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertWarnsRegex</span><span class="p">(</span><span class="ne">RuntimeWarning</span><span class="p">,</span> <span class="s1">&#39;unsafe frobnicating&#39;</span><span class="p">):</span>
  1243. <span class="n">frobnicate</span><span class="p">(</span><span class="s1">&#39;/etc/passwd&#39;</span><span class="p">)</span>
  1244. </pre></div>
  1245. </div>
  1246. <div class="versionadded">
  1247. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1248. </div>
  1249. <div class="versionchanged">
  1250. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>msg</em> keyword argument when used as a context manager.</p>
  1251. </div>
  1252. </dd></dl>
  1253. <dl class="py method">
  1254. <dt class="sig sig-object py" id="unittest.TestCase.assertLogs">
  1255. <span class="sig-name descname"><span class="pre">assertLogs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">logger</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">level</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertLogs" title="Link to this definition">¶</a></dt>
  1256. <dd><p>A context manager to test that at least one message is logged on
  1257. the <em>logger</em> or one of its children, with at least the given
  1258. <em>level</em>.</p>
  1259. <p>If given, <em>logger</em> should be a <a class="reference internal" href="logging.html#logging.Logger" title="logging.Logger"><code class="xref py py-class docutils literal notranslate"><span class="pre">logging.Logger</span></code></a> object or a
  1260. <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> giving the name of a logger. The default is the root
  1261. logger, which will catch all messages that were not blocked by a
  1262. non-propagating descendent logger.</p>
  1263. <p>If given, <em>level</em> should be either a numeric logging level or
  1264. its string equivalent (for example either <code class="docutils literal notranslate"><span class="pre">&quot;ERROR&quot;</span></code> or
  1265. <a class="reference internal" href="logging.html#logging.ERROR" title="logging.ERROR"><code class="xref py py-const docutils literal notranslate"><span class="pre">logging.ERROR</span></code></a>). The default is <a class="reference internal" href="logging.html#logging.INFO" title="logging.INFO"><code class="xref py py-const docutils literal notranslate"><span class="pre">logging.INFO</span></code></a>.</p>
  1266. <p>The test passes if at least one message emitted inside the <code class="docutils literal notranslate"><span class="pre">with</span></code>
  1267. block matches the <em>logger</em> and <em>level</em> conditions, otherwise it fails.</p>
  1268. <p>The object returned by the context manager is a recording helper
  1269. which keeps tracks of the matching log messages. It has two
  1270. attributes:</p>
  1271. <dl class="py attribute">
  1272. <dt class="sig sig-object py" id="unittest.TestCase.records">
  1273. <span class="sig-name descname"><span class="pre">records</span></span><a class="headerlink" href="#unittest.TestCase.records" title="Link to this definition">¶</a></dt>
  1274. <dd><p>A list of <a class="reference internal" href="logging.html#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">logging.LogRecord</span></code></a> objects of the matching
  1275. log messages.</p>
  1276. </dd></dl>
  1277. <dl class="py attribute">
  1278. <dt class="sig sig-object py" id="unittest.TestCase.output">
  1279. <span class="sig-name descname"><span class="pre">output</span></span><a class="headerlink" href="#unittest.TestCase.output" title="Link to this definition">¶</a></dt>
  1280. <dd><p>A list of <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> objects with the formatted output of
  1281. matching messages.</p>
  1282. </dd></dl>
  1283. <p>Example:</p>
  1284. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertLogs</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="s1">&#39;INFO&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">cm</span><span class="p">:</span>
  1285. <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;first message&#39;</span><span class="p">)</span>
  1286. <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;foo.bar&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;second message&#39;</span><span class="p">)</span>
  1287. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">cm</span><span class="o">.</span><span class="n">output</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;INFO:foo:first message&#39;</span><span class="p">,</span>
  1288. <span class="s1">&#39;ERROR:foo.bar:second message&#39;</span><span class="p">])</span>
  1289. </pre></div>
  1290. </div>
  1291. <div class="versionadded">
  1292. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1293. </div>
  1294. </dd></dl>
  1295. <dl class="py method">
  1296. <dt class="sig sig-object py" id="unittest.TestCase.assertNoLogs">
  1297. <span class="sig-name descname"><span class="pre">assertNoLogs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">logger</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">level</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertNoLogs" title="Link to this definition">¶</a></dt>
  1298. <dd><p>A context manager to test that no messages are logged on
  1299. the <em>logger</em> or one of its children, with at least the given
  1300. <em>level</em>.</p>
  1301. <p>If given, <em>logger</em> should be a <a class="reference internal" href="logging.html#logging.Logger" title="logging.Logger"><code class="xref py py-class docutils literal notranslate"><span class="pre">logging.Logger</span></code></a> object or a
  1302. <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> giving the name of a logger. The default is the root
  1303. logger, which will catch all messages.</p>
  1304. <p>If given, <em>level</em> should be either a numeric logging level or
  1305. its string equivalent (for example either <code class="docutils literal notranslate"><span class="pre">&quot;ERROR&quot;</span></code> or
  1306. <a class="reference internal" href="logging.html#logging.ERROR" title="logging.ERROR"><code class="xref py py-const docutils literal notranslate"><span class="pre">logging.ERROR</span></code></a>). The default is <a class="reference internal" href="logging.html#logging.INFO" title="logging.INFO"><code class="xref py py-const docutils literal notranslate"><span class="pre">logging.INFO</span></code></a>.</p>
  1307. <p>Unlike <a class="reference internal" href="#unittest.TestCase.assertLogs" title="unittest.TestCase.assertLogs"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertLogs()</span></code></a>, nothing will be returned by the context
  1308. manager.</p>
  1309. <div class="versionadded">
  1310. <p><span class="versionmodified added">New in version 3.10.</span></p>
  1311. </div>
  1312. </dd></dl>
  1313. <p>There are also other methods used to perform more specific checks, such as:</p>
  1314. <table class="docutils align-default">
  1315. <thead>
  1316. <tr class="row-odd"><th class="head"><p>Method</p></th>
  1317. <th class="head"><p>Checks that</p></th>
  1318. <th class="head"><p>New in</p></th>
  1319. </tr>
  1320. </thead>
  1321. <tbody>
  1322. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertAlmostEqual" title="unittest.TestCase.assertAlmostEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertAlmostEqual(a,</span> <span class="pre">b)</span></code></a></p></td>
  1323. <td><p><code class="docutils literal notranslate"><span class="pre">round(a-b,</span> <span class="pre">7)</span> <span class="pre">==</span> <span class="pre">0</span></code></p></td>
  1324. <td></td>
  1325. </tr>
  1326. <tr class="row-odd"><td><p><a class="reference internal" href="#unittest.TestCase.assertNotAlmostEqual" title="unittest.TestCase.assertNotAlmostEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotAlmostEqual(a,</span> <span class="pre">b)</span></code></a></p></td>
  1327. <td><p><code class="docutils literal notranslate"><span class="pre">round(a-b,</span> <span class="pre">7)</span> <span class="pre">!=</span> <span class="pre">0</span></code></p></td>
  1328. <td></td>
  1329. </tr>
  1330. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertGreater" title="unittest.TestCase.assertGreater"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertGreater(a,</span> <span class="pre">b)</span></code></a></p></td>
  1331. <td><p><code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">&gt;</span> <span class="pre">b</span></code></p></td>
  1332. <td><p>3.1</p></td>
  1333. </tr>
  1334. <tr class="row-odd"><td><p><a class="reference internal" href="#unittest.TestCase.assertGreaterEqual" title="unittest.TestCase.assertGreaterEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertGreaterEqual(a,</span> <span class="pre">b)</span></code></a></p></td>
  1335. <td><p><code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">&gt;=</span> <span class="pre">b</span></code></p></td>
  1336. <td><p>3.1</p></td>
  1337. </tr>
  1338. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertLess" title="unittest.TestCase.assertLess"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertLess(a,</span> <span class="pre">b)</span></code></a></p></td>
  1339. <td><p><code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">&lt;</span> <span class="pre">b</span></code></p></td>
  1340. <td><p>3.1</p></td>
  1341. </tr>
  1342. <tr class="row-odd"><td><p><a class="reference internal" href="#unittest.TestCase.assertLessEqual" title="unittest.TestCase.assertLessEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertLessEqual(a,</span> <span class="pre">b)</span></code></a></p></td>
  1343. <td><p><code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">&lt;=</span> <span class="pre">b</span></code></p></td>
  1344. <td><p>3.1</p></td>
  1345. </tr>
  1346. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertRegex" title="unittest.TestCase.assertRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRegex(s,</span> <span class="pre">r)</span></code></a></p></td>
  1347. <td><p><code class="docutils literal notranslate"><span class="pre">r.search(s)</span></code></p></td>
  1348. <td><p>3.1</p></td>
  1349. </tr>
  1350. <tr class="row-odd"><td><p><a class="reference internal" href="#unittest.TestCase.assertNotRegex" title="unittest.TestCase.assertNotRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotRegex(s,</span> <span class="pre">r)</span></code></a></p></td>
  1351. <td><p><code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">r.search(s)</span></code></p></td>
  1352. <td><p>3.2</p></td>
  1353. </tr>
  1354. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertCountEqual" title="unittest.TestCase.assertCountEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertCountEqual(a,</span> <span class="pre">b)</span></code></a></p></td>
  1355. <td><p><em>a</em> and <em>b</em> have the same
  1356. elements in the same number,
  1357. regardless of their order.</p></td>
  1358. <td><p>3.2</p></td>
  1359. </tr>
  1360. </tbody>
  1361. </table>
  1362. <dl class="py method">
  1363. <dt class="sig sig-object py" id="unittest.TestCase.assertAlmostEqual">
  1364. <span class="sig-name descname"><span class="pre">assertAlmostEqual</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">places</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">7</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">delta</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertAlmostEqual" title="Link to this definition">¶</a></dt>
  1365. <dt class="sig sig-object py" id="unittest.TestCase.assertNotAlmostEqual">
  1366. <span class="sig-name descname"><span class="pre">assertNotAlmostEqual</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">places</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">7</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">delta</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertNotAlmostEqual" title="Link to this definition">¶</a></dt>
  1367. <dd><p>Test that <em>first</em> and <em>second</em> are approximately (or not approximately)
  1368. equal by computing the difference, rounding to the given number of
  1369. decimal <em>places</em> (default 7), and comparing to zero. Note that these
  1370. methods round the values to the given number of <em>decimal places</em> (i.e.
  1371. like the <a class="reference internal" href="functions.html#round" title="round"><code class="xref py py-func docutils literal notranslate"><span class="pre">round()</span></code></a> function) and not <em>significant digits</em>.</p>
  1372. <p>If <em>delta</em> is supplied instead of <em>places</em> then the difference
  1373. between <em>first</em> and <em>second</em> must be less or equal to (or greater than) <em>delta</em>.</p>
  1374. <p>Supplying both <em>delta</em> and <em>places</em> raises a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.</p>
  1375. <div class="versionchanged">
  1376. <p><span class="versionmodified changed">Changed in version 3.2: </span><a class="reference internal" href="#unittest.TestCase.assertAlmostEqual" title="unittest.TestCase.assertAlmostEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertAlmostEqual()</span></code></a> automatically considers almost equal objects
  1377. that compare equal. <a class="reference internal" href="#unittest.TestCase.assertNotAlmostEqual" title="unittest.TestCase.assertNotAlmostEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotAlmostEqual()</span></code></a> automatically fails
  1378. if the objects compare equal. Added the <em>delta</em> keyword argument.</p>
  1379. </div>
  1380. </dd></dl>
  1381. <dl class="py method">
  1382. <dt class="sig sig-object py" id="unittest.TestCase.assertGreater">
  1383. <span class="sig-name descname"><span class="pre">assertGreater</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertGreater" title="Link to this definition">¶</a></dt>
  1384. <dt class="sig sig-object py" id="unittest.TestCase.assertGreaterEqual">
  1385. <span class="sig-name descname"><span class="pre">assertGreaterEqual</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertGreaterEqual" title="Link to this definition">¶</a></dt>
  1386. <dt class="sig sig-object py" id="unittest.TestCase.assertLess">
  1387. <span class="sig-name descname"><span class="pre">assertLess</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertLess" title="Link to this definition">¶</a></dt>
  1388. <dt class="sig sig-object py" id="unittest.TestCase.assertLessEqual">
  1389. <span class="sig-name descname"><span class="pre">assertLessEqual</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertLessEqual" title="Link to this definition">¶</a></dt>
  1390. <dd><p>Test that <em>first</em> is respectively &gt;, &gt;=, &lt; or &lt;= than <em>second</em> depending
  1391. on the method name. If not, the test will fail:</p>
  1392. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="bp">self</span><span class="o">.</span><span class="n">assertGreaterEqual</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
  1393. <span class="go">AssertionError: &quot;3&quot; unexpectedly not greater than or equal to &quot;4&quot;</span>
  1394. </pre></div>
  1395. </div>
  1396. <div class="versionadded">
  1397. <p><span class="versionmodified added">New in version 3.1.</span></p>
  1398. </div>
  1399. </dd></dl>
  1400. <dl class="py method">
  1401. <dt class="sig sig-object py" id="unittest.TestCase.assertRegex">
  1402. <span class="sig-name descname"><span class="pre">assertRegex</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">regex</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertRegex" title="Link to this definition">¶</a></dt>
  1403. <dt class="sig sig-object py" id="unittest.TestCase.assertNotRegex">
  1404. <span class="sig-name descname"><span class="pre">assertNotRegex</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">regex</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertNotRegex" title="Link to this definition">¶</a></dt>
  1405. <dd><p>Test that a <em>regex</em> search matches (or does not match) <em>text</em>. In case
  1406. of failure, the error message will include the pattern and the <em>text</em> (or
  1407. the pattern and the part of <em>text</em> that unexpectedly matched). <em>regex</em>
  1408. may be a regular expression object or a string containing a regular
  1409. expression suitable for use by <a class="reference internal" href="re.html#re.search" title="re.search"><code class="xref py py-func docutils literal notranslate"><span class="pre">re.search()</span></code></a>.</p>
  1410. <div class="versionadded">
  1411. <p><span class="versionmodified added">New in version 3.1: </span>Added under the name <code class="docutils literal notranslate"><span class="pre">assertRegexpMatches</span></code>.</p>
  1412. </div>
  1413. <div class="versionchanged">
  1414. <p><span class="versionmodified changed">Changed in version 3.2: </span>The method <code class="docutils literal notranslate"><span class="pre">assertRegexpMatches()</span></code> has been renamed to
  1415. <a class="reference internal" href="#unittest.TestCase.assertRegex" title="unittest.TestCase.assertRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRegex()</span></code></a>.</p>
  1416. </div>
  1417. <div class="versionadded">
  1418. <p><span class="versionmodified added">New in version 3.2: </span><a class="reference internal" href="#unittest.TestCase.assertNotRegex" title="unittest.TestCase.assertNotRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotRegex()</span></code></a>.</p>
  1419. </div>
  1420. </dd></dl>
  1421. <dl class="py method">
  1422. <dt class="sig sig-object py" id="unittest.TestCase.assertCountEqual">
  1423. <span class="sig-name descname"><span class="pre">assertCountEqual</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertCountEqual" title="Link to this definition">¶</a></dt>
  1424. <dd><p>Test that sequence <em>first</em> contains the same elements as <em>second</em>,
  1425. regardless of their order. When they don’t, an error message listing the
  1426. differences between the sequences will be generated.</p>
  1427. <p>Duplicate elements are <em>not</em> ignored when comparing <em>first</em> and
  1428. <em>second</em>. It verifies whether each element has the same count in both
  1429. sequences. Equivalent to:
  1430. <code class="docutils literal notranslate"><span class="pre">assertEqual(Counter(list(first)),</span> <span class="pre">Counter(list(second)))</span></code>
  1431. but works with sequences of unhashable objects as well.</p>
  1432. <div class="versionadded">
  1433. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1434. </div>
  1435. </dd></dl>
  1436. <p id="type-specific-methods">The <a class="reference internal" href="#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual()</span></code></a> method dispatches the equality check for objects of
  1437. the same type to different type-specific methods. These methods are already
  1438. implemented for most of the built-in types, but it’s also possible to
  1439. register new methods using <a class="reference internal" href="#unittest.TestCase.addTypeEqualityFunc" title="unittest.TestCase.addTypeEqualityFunc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addTypeEqualityFunc()</span></code></a>:</p>
  1440. <dl class="py method">
  1441. <dt class="sig sig-object py" id="unittest.TestCase.addTypeEqualityFunc">
  1442. <span class="sig-name descname"><span class="pre">addTypeEqualityFunc</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">typeobj</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">function</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.addTypeEqualityFunc" title="Link to this definition">¶</a></dt>
  1443. <dd><p>Registers a type-specific method called by <a class="reference internal" href="#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual()</span></code></a> to check
  1444. if two objects of exactly the same <em>typeobj</em> (not subclasses) compare
  1445. equal. <em>function</em> must take two positional arguments and a third msg=None
  1446. keyword argument just as <a class="reference internal" href="#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual()</span></code></a> does. It must raise
  1447. <a class="reference internal" href="#unittest.TestCase.failureException" title="unittest.TestCase.failureException"><code class="xref py py-data docutils literal notranslate"><span class="pre">self.failureException(msg)</span></code></a> when inequality
  1448. between the first two parameters is detected – possibly providing useful
  1449. information and explaining the inequalities in details in the error
  1450. message.</p>
  1451. <div class="versionadded">
  1452. <p><span class="versionmodified added">New in version 3.1.</span></p>
  1453. </div>
  1454. </dd></dl>
  1455. <p>The list of type-specific methods automatically used by
  1456. <a class="reference internal" href="#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual()</span></code></a> are summarized in the following table. Note
  1457. that it’s usually not necessary to invoke these methods directly.</p>
  1458. <table class="docutils align-default">
  1459. <thead>
  1460. <tr class="row-odd"><th class="head"><p>Method</p></th>
  1461. <th class="head"><p>Used to compare</p></th>
  1462. <th class="head"><p>New in</p></th>
  1463. </tr>
  1464. </thead>
  1465. <tbody>
  1466. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertMultiLineEqual" title="unittest.TestCase.assertMultiLineEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertMultiLineEqual(a,</span> <span class="pre">b)</span></code></a></p></td>
  1467. <td><p>strings</p></td>
  1468. <td><p>3.1</p></td>
  1469. </tr>
  1470. <tr class="row-odd"><td><p><a class="reference internal" href="#unittest.TestCase.assertSequenceEqual" title="unittest.TestCase.assertSequenceEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertSequenceEqual(a,</span> <span class="pre">b)</span></code></a></p></td>
  1471. <td><p>sequences</p></td>
  1472. <td><p>3.1</p></td>
  1473. </tr>
  1474. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertListEqual" title="unittest.TestCase.assertListEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertListEqual(a,</span> <span class="pre">b)</span></code></a></p></td>
  1475. <td><p>lists</p></td>
  1476. <td><p>3.1</p></td>
  1477. </tr>
  1478. <tr class="row-odd"><td><p><a class="reference internal" href="#unittest.TestCase.assertTupleEqual" title="unittest.TestCase.assertTupleEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertTupleEqual(a,</span> <span class="pre">b)</span></code></a></p></td>
  1479. <td><p>tuples</p></td>
  1480. <td><p>3.1</p></td>
  1481. </tr>
  1482. <tr class="row-even"><td><p><a class="reference internal" href="#unittest.TestCase.assertSetEqual" title="unittest.TestCase.assertSetEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertSetEqual(a,</span> <span class="pre">b)</span></code></a></p></td>
  1483. <td><p>sets or frozensets</p></td>
  1484. <td><p>3.1</p></td>
  1485. </tr>
  1486. <tr class="row-odd"><td><p><a class="reference internal" href="#unittest.TestCase.assertDictEqual" title="unittest.TestCase.assertDictEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertDictEqual(a,</span> <span class="pre">b)</span></code></a></p></td>
  1487. <td><p>dicts</p></td>
  1488. <td><p>3.1</p></td>
  1489. </tr>
  1490. </tbody>
  1491. </table>
  1492. <dl class="py method">
  1493. <dt class="sig sig-object py" id="unittest.TestCase.assertMultiLineEqual">
  1494. <span class="sig-name descname"><span class="pre">assertMultiLineEqual</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertMultiLineEqual" title="Link to this definition">¶</a></dt>
  1495. <dd><p>Test that the multiline string <em>first</em> is equal to the string <em>second</em>.
  1496. When not equal a diff of the two strings highlighting the differences
  1497. will be included in the error message. This method is used by default
  1498. when comparing strings with <a class="reference internal" href="#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual()</span></code></a>.</p>
  1499. <div class="versionadded">
  1500. <p><span class="versionmodified added">New in version 3.1.</span></p>
  1501. </div>
  1502. </dd></dl>
  1503. <dl class="py method">
  1504. <dt class="sig sig-object py" id="unittest.TestCase.assertSequenceEqual">
  1505. <span class="sig-name descname"><span class="pre">assertSequenceEqual</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">seq_type</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertSequenceEqual" title="Link to this definition">¶</a></dt>
  1506. <dd><p>Tests that two sequences are equal. If a <em>seq_type</em> is supplied, both
  1507. <em>first</em> and <em>second</em> must be instances of <em>seq_type</em> or a failure will
  1508. be raised. If the sequences are different an error message is
  1509. constructed that shows the difference between the two.</p>
  1510. <p>This method is not called directly by <a class="reference internal" href="#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual()</span></code></a>, but
  1511. it’s used to implement <a class="reference internal" href="#unittest.TestCase.assertListEqual" title="unittest.TestCase.assertListEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertListEqual()</span></code></a> and
  1512. <a class="reference internal" href="#unittest.TestCase.assertTupleEqual" title="unittest.TestCase.assertTupleEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertTupleEqual()</span></code></a>.</p>
  1513. <div class="versionadded">
  1514. <p><span class="versionmodified added">New in version 3.1.</span></p>
  1515. </div>
  1516. </dd></dl>
  1517. <dl class="py method">
  1518. <dt class="sig sig-object py" id="unittest.TestCase.assertListEqual">
  1519. <span class="sig-name descname"><span class="pre">assertListEqual</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertListEqual" title="Link to this definition">¶</a></dt>
  1520. <dt class="sig sig-object py" id="unittest.TestCase.assertTupleEqual">
  1521. <span class="sig-name descname"><span class="pre">assertTupleEqual</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertTupleEqual" title="Link to this definition">¶</a></dt>
  1522. <dd><p>Tests that two lists or tuples are equal. If not, an error message is
  1523. constructed that shows only the differences between the two. An error
  1524. is also raised if either of the parameters are of the wrong type.
  1525. These methods are used by default when comparing lists or tuples with
  1526. <a class="reference internal" href="#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual()</span></code></a>.</p>
  1527. <div class="versionadded">
  1528. <p><span class="versionmodified added">New in version 3.1.</span></p>
  1529. </div>
  1530. </dd></dl>
  1531. <dl class="py method">
  1532. <dt class="sig sig-object py" id="unittest.TestCase.assertSetEqual">
  1533. <span class="sig-name descname"><span class="pre">assertSetEqual</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertSetEqual" title="Link to this definition">¶</a></dt>
  1534. <dd><p>Tests that two sets are equal. If not, an error message is constructed
  1535. that lists the differences between the sets. This method is used by
  1536. default when comparing sets or frozensets with <a class="reference internal" href="#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual()</span></code></a>.</p>
  1537. <p>Fails if either of <em>first</em> or <em>second</em> does not have a <code class="xref py py-meth docutils literal notranslate"><span class="pre">set.difference()</span></code>
  1538. method.</p>
  1539. <div class="versionadded">
  1540. <p><span class="versionmodified added">New in version 3.1.</span></p>
  1541. </div>
  1542. </dd></dl>
  1543. <dl class="py method">
  1544. <dt class="sig sig-object py" id="unittest.TestCase.assertDictEqual">
  1545. <span class="sig-name descname"><span class="pre">assertDictEqual</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">first</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">second</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.assertDictEqual" title="Link to this definition">¶</a></dt>
  1546. <dd><p>Test that two dictionaries are equal. If not, an error message is
  1547. constructed that shows the differences in the dictionaries. This
  1548. method will be used by default to compare dictionaries in
  1549. calls to <a class="reference internal" href="#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual()</span></code></a>.</p>
  1550. <div class="versionadded">
  1551. <p><span class="versionmodified added">New in version 3.1.</span></p>
  1552. </div>
  1553. </dd></dl>
  1554. <p id="other-methods-and-attrs">Finally the <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> provides the following methods and attributes:</p>
  1555. <dl class="py method">
  1556. <dt class="sig sig-object py" id="unittest.TestCase.fail">
  1557. <span class="sig-name descname"><span class="pre">fail</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.fail" title="Link to this definition">¶</a></dt>
  1558. <dd><p>Signals a test failure unconditionally, with <em>msg</em> or <code class="docutils literal notranslate"><span class="pre">None</span></code> for
  1559. the error message.</p>
  1560. </dd></dl>
  1561. <dl class="py attribute">
  1562. <dt class="sig sig-object py" id="unittest.TestCase.failureException">
  1563. <span class="sig-name descname"><span class="pre">failureException</span></span><a class="headerlink" href="#unittest.TestCase.failureException" title="Link to this definition">¶</a></dt>
  1564. <dd><p>This class attribute gives the exception raised by the test method. If a
  1565. test framework needs to use a specialized exception, possibly to carry
  1566. additional information, it must subclass this exception in order to “play
  1567. fair” with the framework. The initial value of this attribute is
  1568. <a class="reference internal" href="exceptions.html#AssertionError" title="AssertionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AssertionError</span></code></a>.</p>
  1569. </dd></dl>
  1570. <dl class="py attribute">
  1571. <dt class="sig sig-object py" id="unittest.TestCase.longMessage">
  1572. <span class="sig-name descname"><span class="pre">longMessage</span></span><a class="headerlink" href="#unittest.TestCase.longMessage" title="Link to this definition">¶</a></dt>
  1573. <dd><p>This class attribute determines what happens when a custom failure message
  1574. is passed as the msg argument to an assertXYY call that fails.
  1575. <code class="docutils literal notranslate"><span class="pre">True</span></code> is the default value. In this case, the custom message is appended
  1576. to the end of the standard failure message.
  1577. When set to <code class="docutils literal notranslate"><span class="pre">False</span></code>, the custom message replaces the standard message.</p>
  1578. <p>The class setting can be overridden in individual test methods by assigning
  1579. an instance attribute, self.longMessage, to <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code> before
  1580. calling the assert methods.</p>
  1581. <p>The class setting gets reset before each test call.</p>
  1582. <div class="versionadded">
  1583. <p><span class="versionmodified added">New in version 3.1.</span></p>
  1584. </div>
  1585. </dd></dl>
  1586. <dl class="py attribute">
  1587. <dt class="sig sig-object py" id="unittest.TestCase.maxDiff">
  1588. <span class="sig-name descname"><span class="pre">maxDiff</span></span><a class="headerlink" href="#unittest.TestCase.maxDiff" title="Link to this definition">¶</a></dt>
  1589. <dd><p>This attribute controls the maximum length of diffs output by assert
  1590. methods that report diffs on failure. It defaults to 80*8 characters.
  1591. Assert methods affected by this attribute are
  1592. <a class="reference internal" href="#unittest.TestCase.assertSequenceEqual" title="unittest.TestCase.assertSequenceEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertSequenceEqual()</span></code></a> (including all the sequence comparison
  1593. methods that delegate to it), <a class="reference internal" href="#unittest.TestCase.assertDictEqual" title="unittest.TestCase.assertDictEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertDictEqual()</span></code></a> and
  1594. <a class="reference internal" href="#unittest.TestCase.assertMultiLineEqual" title="unittest.TestCase.assertMultiLineEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertMultiLineEqual()</span></code></a>.</p>
  1595. <p>Setting <code class="docutils literal notranslate"><span class="pre">maxDiff</span></code> to <code class="docutils literal notranslate"><span class="pre">None</span></code> means that there is no maximum length of
  1596. diffs.</p>
  1597. <div class="versionadded">
  1598. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1599. </div>
  1600. </dd></dl>
  1601. <p>Testing frameworks can use the following methods to collect information on
  1602. the test:</p>
  1603. <dl class="py method">
  1604. <dt class="sig sig-object py" id="unittest.TestCase.countTestCases">
  1605. <span class="sig-name descname"><span class="pre">countTestCases</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.countTestCases" title="Link to this definition">¶</a></dt>
  1606. <dd><p>Return the number of tests represented by this test object. For
  1607. <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> instances, this will always be <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p>
  1608. </dd></dl>
  1609. <dl class="py method">
  1610. <dt class="sig sig-object py" id="unittest.TestCase.defaultTestResult">
  1611. <span class="sig-name descname"><span class="pre">defaultTestResult</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.defaultTestResult" title="Link to this definition">¶</a></dt>
  1612. <dd><p>Return an instance of the test result class that should be used for this
  1613. test case class (if no other result instance is provided to the
  1614. <a class="reference internal" href="#unittest.TestCase.run" title="unittest.TestCase.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method).</p>
  1615. <p>For <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> instances, this will always be an instance of
  1616. <a class="reference internal" href="#unittest.TestResult" title="unittest.TestResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResult</span></code></a>; subclasses of <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> should override this
  1617. as necessary.</p>
  1618. </dd></dl>
  1619. <dl class="py method">
  1620. <dt class="sig sig-object py" id="unittest.TestCase.id">
  1621. <span class="sig-name descname"><span class="pre">id</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.id" title="Link to this definition">¶</a></dt>
  1622. <dd><p>Return a string identifying the specific test case. This is usually the
  1623. full name of the test method, including the module and class name.</p>
  1624. </dd></dl>
  1625. <dl class="py method">
  1626. <dt class="sig sig-object py" id="unittest.TestCase.shortDescription">
  1627. <span class="sig-name descname"><span class="pre">shortDescription</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.shortDescription" title="Link to this definition">¶</a></dt>
  1628. <dd><p>Returns a description of the test, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if no description
  1629. has been provided. The default implementation of this method
  1630. returns the first line of the test method’s docstring, if available,
  1631. or <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1632. <div class="versionchanged">
  1633. <p><span class="versionmodified changed">Changed in version 3.1: </span>In 3.1 this was changed to add the test name to the short description
  1634. even in the presence of a docstring. This caused compatibility issues
  1635. with unittest extensions and adding the test name was moved to the
  1636. <a class="reference internal" href="#unittest.TextTestResult" title="unittest.TextTestResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextTestResult</span></code></a> in Python 3.2.</p>
  1637. </div>
  1638. </dd></dl>
  1639. <dl class="py method">
  1640. <dt class="sig sig-object py" id="unittest.TestCase.addCleanup">
  1641. <span class="sig-name descname"><span class="pre">addCleanup</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">function</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.addCleanup" title="Link to this definition">¶</a></dt>
  1642. <dd><p>Add a function to be called after <a class="reference internal" href="#unittest.TestCase.tearDown" title="unittest.TestCase.tearDown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDown()</span></code></a> to cleanup resources
  1643. used during the test. Functions will be called in reverse order to the
  1644. order they are added (<abbr title="last-in, first-out">LIFO</abbr>). They
  1645. are called with any arguments and keyword arguments passed into
  1646. <a class="reference internal" href="#unittest.TestCase.addCleanup" title="unittest.TestCase.addCleanup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addCleanup()</span></code></a> when they are added.</p>
  1647. <p>If <a class="reference internal" href="#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUp()</span></code></a> fails, meaning that <a class="reference internal" href="#unittest.TestCase.tearDown" title="unittest.TestCase.tearDown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDown()</span></code></a> is not called,
  1648. then any cleanup functions added will still be called.</p>
  1649. <div class="versionadded">
  1650. <p><span class="versionmodified added">New in version 3.1.</span></p>
  1651. </div>
  1652. </dd></dl>
  1653. <dl class="py method">
  1654. <dt class="sig sig-object py" id="unittest.TestCase.enterContext">
  1655. <span class="sig-name descname"><span class="pre">enterContext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cm</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.enterContext" title="Link to this definition">¶</a></dt>
  1656. <dd><p>Enter the supplied <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a>. If successful, also
  1657. add its <a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__exit__()</span></code></a> method as a cleanup function by
  1658. <a class="reference internal" href="#unittest.TestCase.addCleanup" title="unittest.TestCase.addCleanup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addCleanup()</span></code></a> and return the result of the
  1659. <a class="reference internal" href="../reference/datamodel.html#object.__enter__" title="object.__enter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__enter__()</span></code></a> method.</p>
  1660. <div class="versionadded">
  1661. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1662. </div>
  1663. </dd></dl>
  1664. <dl class="py method">
  1665. <dt class="sig sig-object py" id="unittest.TestCase.doCleanups">
  1666. <span class="sig-name descname"><span class="pre">doCleanups</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.doCleanups" title="Link to this definition">¶</a></dt>
  1667. <dd><p>This method is called unconditionally after <a class="reference internal" href="#unittest.TestCase.tearDown" title="unittest.TestCase.tearDown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDown()</span></code></a>, or
  1668. after <a class="reference internal" href="#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUp()</span></code></a> if <a class="reference internal" href="#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUp()</span></code></a> raises an exception.</p>
  1669. <p>It is responsible for calling all the cleanup functions added by
  1670. <a class="reference internal" href="#unittest.TestCase.addCleanup" title="unittest.TestCase.addCleanup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addCleanup()</span></code></a>. If you need cleanup functions to be called
  1671. <em>prior</em> to <a class="reference internal" href="#unittest.TestCase.tearDown" title="unittest.TestCase.tearDown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDown()</span></code></a> then you can call <a class="reference internal" href="#unittest.TestCase.doCleanups" title="unittest.TestCase.doCleanups"><code class="xref py py-meth docutils literal notranslate"><span class="pre">doCleanups()</span></code></a>
  1672. yourself.</p>
  1673. <p><a class="reference internal" href="#unittest.TestCase.doCleanups" title="unittest.TestCase.doCleanups"><code class="xref py py-meth docutils literal notranslate"><span class="pre">doCleanups()</span></code></a> pops methods off the stack of cleanup
  1674. functions one at a time, so it can be called at any time.</p>
  1675. <div class="versionadded">
  1676. <p><span class="versionmodified added">New in version 3.1.</span></p>
  1677. </div>
  1678. </dd></dl>
  1679. <dl class="py method">
  1680. <dt class="sig sig-object py" id="unittest.TestCase.addClassCleanup">
  1681. <em class="property"><span class="pre">classmethod</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">addClassCleanup</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">function</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.addClassCleanup" title="Link to this definition">¶</a></dt>
  1682. <dd><p>Add a function to be called after <a class="reference internal" href="#unittest.TestCase.tearDownClass" title="unittest.TestCase.tearDownClass"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDownClass()</span></code></a> to cleanup
  1683. resources used during the test class. Functions will be called in reverse
  1684. order to the order they are added (<abbr title="last-in, first-out">LIFO</abbr>).
  1685. They are called with any arguments and keyword arguments passed into
  1686. <a class="reference internal" href="#unittest.TestCase.addClassCleanup" title="unittest.TestCase.addClassCleanup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addClassCleanup()</span></code></a> when they are added.</p>
  1687. <p>If <a class="reference internal" href="#unittest.TestCase.setUpClass" title="unittest.TestCase.setUpClass"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUpClass()</span></code></a> fails, meaning that <a class="reference internal" href="#unittest.TestCase.tearDownClass" title="unittest.TestCase.tearDownClass"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDownClass()</span></code></a> is not
  1688. called, then any cleanup functions added will still be called.</p>
  1689. <div class="versionadded">
  1690. <p><span class="versionmodified added">New in version 3.8.</span></p>
  1691. </div>
  1692. </dd></dl>
  1693. <dl class="py method">
  1694. <dt class="sig sig-object py" id="unittest.TestCase.enterClassContext">
  1695. <em class="property"><span class="pre">classmethod</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">enterClassContext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cm</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.enterClassContext" title="Link to this definition">¶</a></dt>
  1696. <dd><p>Enter the supplied <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a>. If successful, also
  1697. add its <a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__exit__()</span></code></a> method as a cleanup function by
  1698. <a class="reference internal" href="#unittest.TestCase.addClassCleanup" title="unittest.TestCase.addClassCleanup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addClassCleanup()</span></code></a> and return the result of the
  1699. <a class="reference internal" href="../reference/datamodel.html#object.__enter__" title="object.__enter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__enter__()</span></code></a> method.</p>
  1700. <div class="versionadded">
  1701. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1702. </div>
  1703. </dd></dl>
  1704. <dl class="py method">
  1705. <dt class="sig sig-object py" id="unittest.TestCase.doClassCleanups">
  1706. <em class="property"><span class="pre">classmethod</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">doClassCleanups</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestCase.doClassCleanups" title="Link to this definition">¶</a></dt>
  1707. <dd><p>This method is called unconditionally after <a class="reference internal" href="#unittest.TestCase.tearDownClass" title="unittest.TestCase.tearDownClass"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDownClass()</span></code></a>, or
  1708. after <a class="reference internal" href="#unittest.TestCase.setUpClass" title="unittest.TestCase.setUpClass"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUpClass()</span></code></a> if <a class="reference internal" href="#unittest.TestCase.setUpClass" title="unittest.TestCase.setUpClass"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUpClass()</span></code></a> raises an exception.</p>
  1709. <p>It is responsible for calling all the cleanup functions added by
  1710. <a class="reference internal" href="#unittest.TestCase.addClassCleanup" title="unittest.TestCase.addClassCleanup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addClassCleanup()</span></code></a>. If you need cleanup functions to be called
  1711. <em>prior</em> to <a class="reference internal" href="#unittest.TestCase.tearDownClass" title="unittest.TestCase.tearDownClass"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDownClass()</span></code></a> then you can call
  1712. <a class="reference internal" href="#unittest.TestCase.doClassCleanups" title="unittest.TestCase.doClassCleanups"><code class="xref py py-meth docutils literal notranslate"><span class="pre">doClassCleanups()</span></code></a> yourself.</p>
  1713. <p><a class="reference internal" href="#unittest.TestCase.doClassCleanups" title="unittest.TestCase.doClassCleanups"><code class="xref py py-meth docutils literal notranslate"><span class="pre">doClassCleanups()</span></code></a> pops methods off the stack of cleanup
  1714. functions one at a time, so it can be called at any time.</p>
  1715. <div class="versionadded">
  1716. <p><span class="versionmodified added">New in version 3.8.</span></p>
  1717. </div>
  1718. </dd></dl>
  1719. </dd></dl>
  1720. <dl class="py class">
  1721. <dt class="sig sig-object py" id="unittest.IsolatedAsyncioTestCase">
  1722. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">IsolatedAsyncioTestCase</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">methodName</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'runTest'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.IsolatedAsyncioTestCase" title="Link to this definition">¶</a></dt>
  1723. <dd><p>This class provides an API similar to <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> and also accepts
  1724. coroutines as test functions.</p>
  1725. <div class="versionadded">
  1726. <p><span class="versionmodified added">New in version 3.8.</span></p>
  1727. </div>
  1728. <dl class="py method">
  1729. <dt class="sig sig-object py" id="unittest.IsolatedAsyncioTestCase.asyncSetUp">
  1730. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">asyncSetUp</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.IsolatedAsyncioTestCase.asyncSetUp" title="Link to this definition">¶</a></dt>
  1731. <dd><p>Method called to prepare the test fixture. This is called after <code class="xref py py-meth docutils literal notranslate"><span class="pre">setUp()</span></code>.
  1732. This is called immediately before calling the test method; other than
  1733. <a class="reference internal" href="exceptions.html#AssertionError" title="AssertionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AssertionError</span></code></a> or <a class="reference internal" href="#unittest.SkipTest" title="unittest.SkipTest"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SkipTest</span></code></a>, any exception raised by this method
  1734. will be considered an error rather than a test failure. The default implementation
  1735. does nothing.</p>
  1736. </dd></dl>
  1737. <dl class="py method">
  1738. <dt class="sig sig-object py" id="unittest.IsolatedAsyncioTestCase.asyncTearDown">
  1739. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">asyncTearDown</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.IsolatedAsyncioTestCase.asyncTearDown" title="Link to this definition">¶</a></dt>
  1740. <dd><p>Method called immediately after the test method has been called and the
  1741. result recorded. This is called before <code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDown()</span></code>. This is called even if
  1742. the test method raised an exception, so the implementation in subclasses may need
  1743. to be particularly careful about checking internal state. Any exception, other than
  1744. <a class="reference internal" href="exceptions.html#AssertionError" title="AssertionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AssertionError</span></code></a> or <a class="reference internal" href="#unittest.SkipTest" title="unittest.SkipTest"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SkipTest</span></code></a>, raised by this method will be
  1745. considered an additional error rather than a test failure (thus increasing
  1746. the total number of reported errors). This method will only be called if
  1747. the <a class="reference internal" href="#unittest.IsolatedAsyncioTestCase.asyncSetUp" title="unittest.IsolatedAsyncioTestCase.asyncSetUp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">asyncSetUp()</span></code></a> succeeds, regardless of the outcome of the test method.
  1748. The default implementation does nothing.</p>
  1749. </dd></dl>
  1750. <dl class="py method">
  1751. <dt class="sig sig-object py" id="unittest.IsolatedAsyncioTestCase.addAsyncCleanup">
  1752. <span class="sig-name descname"><span class="pre">addAsyncCleanup</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">function</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.IsolatedAsyncioTestCase.addAsyncCleanup" title="Link to this definition">¶</a></dt>
  1753. <dd><p>This method accepts a coroutine that can be used as a cleanup function.</p>
  1754. </dd></dl>
  1755. <dl class="py method">
  1756. <dt class="sig sig-object py" id="unittest.IsolatedAsyncioTestCase.enterAsyncContext">
  1757. <em class="property"><span class="pre">coroutine</span> </em><span class="sig-name descname"><span class="pre">enterAsyncContext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cm</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.IsolatedAsyncioTestCase.enterAsyncContext" title="Link to this definition">¶</a></dt>
  1758. <dd><p>Enter the supplied <a class="reference internal" href="../glossary.html#term-asynchronous-context-manager"><span class="xref std std-term">asynchronous context manager</span></a>. If successful,
  1759. also add its <a class="reference internal" href="../reference/datamodel.html#object.__aexit__" title="object.__aexit__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__aexit__()</span></code></a> method as a cleanup function by
  1760. <a class="reference internal" href="#unittest.IsolatedAsyncioTestCase.addAsyncCleanup" title="unittest.IsolatedAsyncioTestCase.addAsyncCleanup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addAsyncCleanup()</span></code></a> and return the result of the
  1761. <a class="reference internal" href="../reference/datamodel.html#object.__aenter__" title="object.__aenter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__aenter__()</span></code></a> method.</p>
  1762. <div class="versionadded">
  1763. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1764. </div>
  1765. </dd></dl>
  1766. <dl class="py method">
  1767. <dt class="sig sig-object py" id="unittest.IsolatedAsyncioTestCase.run">
  1768. <span class="sig-name descname"><span class="pre">run</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">result</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.IsolatedAsyncioTestCase.run" title="Link to this definition">¶</a></dt>
  1769. <dd><p>Sets up a new event loop to run the test, collecting the result into
  1770. the <a class="reference internal" href="#unittest.TestResult" title="unittest.TestResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResult</span></code></a> object passed as <em>result</em>. If <em>result</em> is
  1771. omitted or <code class="docutils literal notranslate"><span class="pre">None</span></code>, a temporary result object is created (by calling
  1772. the <code class="xref py py-meth docutils literal notranslate"><span class="pre">defaultTestResult()</span></code> method) and used. The result object is
  1773. returned to <a class="reference internal" href="#unittest.IsolatedAsyncioTestCase.run" title="unittest.IsolatedAsyncioTestCase.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a>’s caller. At the end of the test all the tasks
  1774. in the event loop are cancelled.</p>
  1775. </dd></dl>
  1776. <p>An example illustrating the order:</p>
  1777. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">unittest</span> <span class="kn">import</span> <span class="n">IsolatedAsyncioTestCase</span>
  1778. <span class="n">events</span> <span class="o">=</span> <span class="p">[]</span>
  1779. <span class="k">class</span> <span class="nc">Test</span><span class="p">(</span><span class="n">IsolatedAsyncioTestCase</span><span class="p">):</span>
  1780. <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1781. <span class="n">events</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;setUp&quot;</span><span class="p">)</span>
  1782. <span class="k">async</span> <span class="k">def</span> <span class="nf">asyncSetUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1783. <span class="bp">self</span><span class="o">.</span><span class="n">_async_connection</span> <span class="o">=</span> <span class="k">await</span> <span class="n">AsyncConnection</span><span class="p">()</span>
  1784. <span class="n">events</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;asyncSetUp&quot;</span><span class="p">)</span>
  1785. <span class="k">async</span> <span class="k">def</span> <span class="nf">test_response</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1786. <span class="n">events</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;test_response&quot;</span><span class="p">)</span>
  1787. <span class="n">response</span> <span class="o">=</span> <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">_async_connection</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;https://example.com&quot;</span><span class="p">)</span>
  1788. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">status_code</span><span class="p">,</span> <span class="mi">200</span><span class="p">)</span>
  1789. <span class="bp">self</span><span class="o">.</span><span class="n">addAsyncCleanup</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">on_cleanup</span><span class="p">)</span>
  1790. <span class="k">def</span> <span class="nf">tearDown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1791. <span class="n">events</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;tearDown&quot;</span><span class="p">)</span>
  1792. <span class="k">async</span> <span class="k">def</span> <span class="nf">asyncTearDown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1793. <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">_async_connection</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1794. <span class="n">events</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;asyncTearDown&quot;</span><span class="p">)</span>
  1795. <span class="k">async</span> <span class="k">def</span> <span class="nf">on_cleanup</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1796. <span class="n">events</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;cleanup&quot;</span><span class="p">)</span>
  1797. <span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
  1798. <span class="n">unittest</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
  1799. </pre></div>
  1800. </div>
  1801. <p>After running the test, <code class="docutils literal notranslate"><span class="pre">events</span></code> would contain <code class="docutils literal notranslate"><span class="pre">[&quot;setUp&quot;,</span> <span class="pre">&quot;asyncSetUp&quot;,</span> <span class="pre">&quot;test_response&quot;,</span> <span class="pre">&quot;asyncTearDown&quot;,</span> <span class="pre">&quot;tearDown&quot;,</span> <span class="pre">&quot;cleanup&quot;]</span></code>.</p>
  1802. </dd></dl>
  1803. <dl class="py class">
  1804. <dt class="sig sig-object py" id="unittest.FunctionTestCase">
  1805. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">FunctionTestCase</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">testFunc</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">setUp</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tearDown</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">description</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.FunctionTestCase" title="Link to this definition">¶</a></dt>
  1806. <dd><p>This class implements the portion of the <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> interface which
  1807. allows the test runner to drive the test, but does not provide the methods
  1808. which test code can use to check and report errors. This is used to create
  1809. test cases using legacy test code, allowing it to be integrated into a
  1810. <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a>-based test framework.</p>
  1811. </dd></dl>
  1812. </section>
  1813. <section id="grouping-tests">
  1814. <span id="testsuite-objects"></span><h3>Grouping tests<a class="headerlink" href="#grouping-tests" title="Link to this heading">¶</a></h3>
  1815. <dl class="py class">
  1816. <dt class="sig sig-object py" id="unittest.TestSuite">
  1817. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">TestSuite</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tests</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestSuite" title="Link to this definition">¶</a></dt>
  1818. <dd><p>This class represents an aggregation of individual test cases and test suites.
  1819. The class presents the interface needed by the test runner to allow it to be run
  1820. as any other test case. Running a <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a> instance is the same as
  1821. iterating over the suite, running each test individually.</p>
  1822. <p>If <em>tests</em> is given, it must be an iterable of individual test cases or other
  1823. test suites that will be used to build the suite initially. Additional methods
  1824. are provided to add test cases and suites to the collection later on.</p>
  1825. <p><a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a> objects behave much like <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> objects, except
  1826. they do not actually implement a test. Instead, they are used to aggregate
  1827. tests into groups of tests that should be run together. Some additional
  1828. methods are available to add tests to <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a> instances:</p>
  1829. <dl class="py method">
  1830. <dt class="sig sig-object py" id="unittest.TestSuite.addTest">
  1831. <span class="sig-name descname"><span class="pre">addTest</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestSuite.addTest" title="Link to this definition">¶</a></dt>
  1832. <dd><p>Add a <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> or <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a> to the suite.</p>
  1833. </dd></dl>
  1834. <dl class="py method">
  1835. <dt class="sig sig-object py" id="unittest.TestSuite.addTests">
  1836. <span class="sig-name descname"><span class="pre">addTests</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tests</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestSuite.addTests" title="Link to this definition">¶</a></dt>
  1837. <dd><p>Add all the tests from an iterable of <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> and <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a>
  1838. instances to this test suite.</p>
  1839. <p>This is equivalent to iterating over <em>tests</em>, calling <a class="reference internal" href="#unittest.TestSuite.addTest" title="unittest.TestSuite.addTest"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addTest()</span></code></a> for
  1840. each element.</p>
  1841. </dd></dl>
  1842. <p><a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a> shares the following methods with <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a>:</p>
  1843. <dl class="py method">
  1844. <dt class="sig sig-object py" id="unittest.TestSuite.run">
  1845. <span class="sig-name descname"><span class="pre">run</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">result</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestSuite.run" title="Link to this definition">¶</a></dt>
  1846. <dd><p>Run the tests associated with this suite, collecting the result into the
  1847. test result object passed as <em>result</em>. Note that unlike
  1848. <a class="reference internal" href="#unittest.TestCase.run" title="unittest.TestCase.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TestCase.run()</span></code></a>, <a class="reference internal" href="#unittest.TestSuite.run" title="unittest.TestSuite.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TestSuite.run()</span></code></a> requires the result object to
  1849. be passed in.</p>
  1850. </dd></dl>
  1851. <dl class="py method">
  1852. <dt class="sig sig-object py" id="unittest.TestSuite.debug">
  1853. <span class="sig-name descname"><span class="pre">debug</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestSuite.debug" title="Link to this definition">¶</a></dt>
  1854. <dd><p>Run the tests associated with this suite without collecting the
  1855. result. This allows exceptions raised by the test to be propagated to the
  1856. caller and can be used to support running tests under a debugger.</p>
  1857. </dd></dl>
  1858. <dl class="py method">
  1859. <dt class="sig sig-object py" id="unittest.TestSuite.countTestCases">
  1860. <span class="sig-name descname"><span class="pre">countTestCases</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestSuite.countTestCases" title="Link to this definition">¶</a></dt>
  1861. <dd><p>Return the number of tests represented by this test object, including all
  1862. individual tests and sub-suites.</p>
  1863. </dd></dl>
  1864. <dl class="py method">
  1865. <dt class="sig sig-object py" id="unittest.TestSuite.__iter__">
  1866. <span class="sig-name descname"><span class="pre">__iter__</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestSuite.__iter__" title="Link to this definition">¶</a></dt>
  1867. <dd><p>Tests grouped by a <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a> are always accessed by iteration.
  1868. Subclasses can lazily provide tests by overriding <code class="xref py py-meth docutils literal notranslate"><span class="pre">__iter__()</span></code>. Note
  1869. that this method may be called several times on a single suite (for
  1870. example when counting tests or comparing for equality) so the tests
  1871. returned by repeated iterations before <a class="reference internal" href="#unittest.TestSuite.run" title="unittest.TestSuite.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TestSuite.run()</span></code></a> must be the
  1872. same for each call iteration. After <a class="reference internal" href="#unittest.TestSuite.run" title="unittest.TestSuite.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TestSuite.run()</span></code></a>, callers should
  1873. not rely on the tests returned by this method unless the caller uses a
  1874. subclass that overrides <code class="xref py py-meth docutils literal notranslate"><span class="pre">TestSuite._removeTestAtIndex()</span></code> to preserve
  1875. test references.</p>
  1876. <div class="versionchanged">
  1877. <p><span class="versionmodified changed">Changed in version 3.2: </span>In earlier versions the <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a> accessed tests directly rather
  1878. than through iteration, so overriding <code class="xref py py-meth docutils literal notranslate"><span class="pre">__iter__()</span></code> wasn’t sufficient
  1879. for providing tests.</p>
  1880. </div>
  1881. <div class="versionchanged">
  1882. <p><span class="versionmodified changed">Changed in version 3.4: </span>In earlier versions the <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a> held references to each
  1883. <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> after <a class="reference internal" href="#unittest.TestSuite.run" title="unittest.TestSuite.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TestSuite.run()</span></code></a>. Subclasses can restore
  1884. that behavior by overriding <code class="xref py py-meth docutils literal notranslate"><span class="pre">TestSuite._removeTestAtIndex()</span></code>.</p>
  1885. </div>
  1886. </dd></dl>
  1887. <p>In the typical usage of a <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a> object, the <a class="reference internal" href="#unittest.TestSuite.run" title="unittest.TestSuite.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method
  1888. is invoked by a <code class="xref py py-class docutils literal notranslate"><span class="pre">TestRunner</span></code> rather than by the end-user test harness.</p>
  1889. </dd></dl>
  1890. </section>
  1891. <section id="loading-and-running-tests">
  1892. <h3>Loading and running tests<a class="headerlink" href="#loading-and-running-tests" title="Link to this heading">¶</a></h3>
  1893. <dl class="py class">
  1894. <dt class="sig sig-object py" id="unittest.TestLoader">
  1895. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">TestLoader</span></span><a class="headerlink" href="#unittest.TestLoader" title="Link to this definition">¶</a></dt>
  1896. <dd><p>The <a class="reference internal" href="#unittest.TestLoader" title="unittest.TestLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestLoader</span></code></a> class is used to create test suites from classes and
  1897. modules. Normally, there is no need to create an instance of this class; the
  1898. <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> module provides an instance that can be shared as
  1899. <a class="reference internal" href="#unittest.defaultTestLoader" title="unittest.defaultTestLoader"><code class="xref py py-data docutils literal notranslate"><span class="pre">unittest.defaultTestLoader</span></code></a>. Using a subclass or instance, however,
  1900. allows customization of some configurable properties.</p>
  1901. <p><a class="reference internal" href="#unittest.TestLoader" title="unittest.TestLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestLoader</span></code></a> objects have the following attributes:</p>
  1902. <dl class="py attribute">
  1903. <dt class="sig sig-object py" id="unittest.TestLoader.errors">
  1904. <span class="sig-name descname"><span class="pre">errors</span></span><a class="headerlink" href="#unittest.TestLoader.errors" title="Link to this definition">¶</a></dt>
  1905. <dd><p>A list of the non-fatal errors encountered while loading tests. Not reset
  1906. by the loader at any point. Fatal errors are signalled by the relevant
  1907. method raising an exception to the caller. Non-fatal errors are also
  1908. indicated by a synthetic test that will raise the original error when
  1909. run.</p>
  1910. <div class="versionadded">
  1911. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1912. </div>
  1913. </dd></dl>
  1914. <p><a class="reference internal" href="#unittest.TestLoader" title="unittest.TestLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestLoader</span></code></a> objects have the following methods:</p>
  1915. <dl class="py method">
  1916. <dt class="sig sig-object py" id="unittest.TestLoader.loadTestsFromTestCase">
  1917. <span class="sig-name descname"><span class="pre">loadTestsFromTestCase</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">testCaseClass</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestLoader.loadTestsFromTestCase" title="Link to this definition">¶</a></dt>
  1918. <dd><p>Return a suite of all test cases contained in the <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a>-derived
  1919. <code class="xref py py-class docutils literal notranslate"><span class="pre">testCaseClass</span></code>.</p>
  1920. <p>A test case instance is created for each method named by
  1921. <a class="reference internal" href="#unittest.TestLoader.getTestCaseNames" title="unittest.TestLoader.getTestCaseNames"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getTestCaseNames()</span></code></a>. By default these are the method names
  1922. beginning with <code class="docutils literal notranslate"><span class="pre">test</span></code>. If <a class="reference internal" href="#unittest.TestLoader.getTestCaseNames" title="unittest.TestLoader.getTestCaseNames"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getTestCaseNames()</span></code></a> returns no
  1923. methods, but the <code class="xref py py-meth docutils literal notranslate"><span class="pre">runTest()</span></code> method is implemented, a single test
  1924. case is created for that method instead.</p>
  1925. </dd></dl>
  1926. <dl class="py method">
  1927. <dt class="sig sig-object py" id="unittest.TestLoader.loadTestsFromModule">
  1928. <span class="sig-name descname"><span class="pre">loadTestsFromModule</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pattern</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestLoader.loadTestsFromModule" title="Link to this definition">¶</a></dt>
  1929. <dd><p>Return a suite of all test cases contained in the given module. This
  1930. method searches <em>module</em> for classes derived from <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> and
  1931. creates an instance of the class for each test method defined for the
  1932. class.</p>
  1933. <div class="admonition note">
  1934. <p class="admonition-title">Note</p>
  1935. <p>While using a hierarchy of <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a>-derived classes can be
  1936. convenient in sharing fixtures and helper functions, defining test
  1937. methods on base classes that are not intended to be instantiated
  1938. directly does not play well with this method. Doing so, however, can
  1939. be useful when the fixtures are different and defined in subclasses.</p>
  1940. </div>
  1941. <p>If a module provides a <code class="docutils literal notranslate"><span class="pre">load_tests</span></code> function it will be called to
  1942. load the tests. This allows modules to customize test loading.
  1943. This is the <a class="reference internal" href="#id1">load_tests protocol</a>. The <em>pattern</em> argument is passed as
  1944. the third argument to <code class="docutils literal notranslate"><span class="pre">load_tests</span></code>.</p>
  1945. <div class="versionchanged">
  1946. <p><span class="versionmodified changed">Changed in version 3.2: </span>Support for <code class="docutils literal notranslate"><span class="pre">load_tests</span></code> added.</p>
  1947. </div>
  1948. <div class="versionchanged">
  1949. <p><span class="versionmodified changed">Changed in version 3.5: </span>Support for a keyword-only argument <em>pattern</em> has been added.</p>
  1950. </div>
  1951. <div class="versionchanged">
  1952. <p><span class="versionmodified changed">Changed in version 3.12: </span>The undocumented and unofficial <em>use_load_tests</em> parameter has been
  1953. removed.</p>
  1954. </div>
  1955. </dd></dl>
  1956. <dl class="py method">
  1957. <dt class="sig sig-object py" id="unittest.TestLoader.loadTestsFromName">
  1958. <span class="sig-name descname"><span class="pre">loadTestsFromName</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">module</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestLoader.loadTestsFromName" title="Link to this definition">¶</a></dt>
  1959. <dd><p>Return a suite of all test cases given a string specifier.</p>
  1960. <p>The specifier <em>name</em> is a “dotted name” that may resolve either to a
  1961. module, a test case class, a test method within a test case class, a
  1962. <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a> instance, or a callable object which returns a
  1963. <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> or <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a> instance. These checks are
  1964. applied in the order listed here; that is, a method on a possible test
  1965. case class will be picked up as “a test method within a test case class”,
  1966. rather than “a callable object”.</p>
  1967. <p>For example, if you have a module <code class="xref py py-mod docutils literal notranslate"><span class="pre">SampleTests</span></code> containing a
  1968. <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a>-derived class <code class="xref py py-class docutils literal notranslate"><span class="pre">SampleTestCase</span></code> with three test
  1969. methods (<code class="xref py py-meth docutils literal notranslate"><span class="pre">test_one()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">test_two()</span></code>, and <code class="xref py py-meth docutils literal notranslate"><span class="pre">test_three()</span></code>), the
  1970. specifier <code class="docutils literal notranslate"><span class="pre">'SampleTests.SampleTestCase'</span></code> would cause this method to
  1971. return a suite which will run all three test methods. Using the specifier
  1972. <code class="docutils literal notranslate"><span class="pre">'SampleTests.SampleTestCase.test_two'</span></code> would cause it to return a test
  1973. suite which will run only the <code class="xref py py-meth docutils literal notranslate"><span class="pre">test_two()</span></code> test method. The specifier
  1974. can refer to modules and packages which have not been imported; they will
  1975. be imported as a side-effect.</p>
  1976. <p>The method optionally resolves <em>name</em> relative to the given <em>module</em>.</p>
  1977. <div class="versionchanged">
  1978. <p><span class="versionmodified changed">Changed in version 3.5: </span>If an <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> or <a class="reference internal" href="exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a> occurs while traversing
  1979. <em>name</em> then a synthetic test that raises that error when run will be
  1980. returned. These errors are included in the errors accumulated by
  1981. self.errors.</p>
  1982. </div>
  1983. </dd></dl>
  1984. <dl class="py method">
  1985. <dt class="sig sig-object py" id="unittest.TestLoader.loadTestsFromNames">
  1986. <span class="sig-name descname"><span class="pre">loadTestsFromNames</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">names</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">module</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestLoader.loadTestsFromNames" title="Link to this definition">¶</a></dt>
  1987. <dd><p>Similar to <a class="reference internal" href="#unittest.TestLoader.loadTestsFromName" title="unittest.TestLoader.loadTestsFromName"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loadTestsFromName()</span></code></a>, but takes a sequence of names rather
  1988. than a single name. The return value is a test suite which supports all
  1989. the tests defined for each name.</p>
  1990. </dd></dl>
  1991. <dl class="py method">
  1992. <dt class="sig sig-object py" id="unittest.TestLoader.getTestCaseNames">
  1993. <span class="sig-name descname"><span class="pre">getTestCaseNames</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">testCaseClass</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestLoader.getTestCaseNames" title="Link to this definition">¶</a></dt>
  1994. <dd><p>Return a sorted sequence of method names found within <em>testCaseClass</em>;
  1995. this should be a subclass of <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a>.</p>
  1996. </dd></dl>
  1997. <dl class="py method">
  1998. <dt class="sig sig-object py" id="unittest.TestLoader.discover">
  1999. <span class="sig-name descname"><span class="pre">discover</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">start_dir</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pattern</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'test*.py'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">top_level_dir</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestLoader.discover" title="Link to this definition">¶</a></dt>
  2000. <dd><p>Find all the test modules by recursing into subdirectories from the
  2001. specified start directory, and return a TestSuite object containing them.
  2002. Only test files that match <em>pattern</em> will be loaded. (Using shell style
  2003. pattern matching.) Only module names that are importable (i.e. are valid
  2004. Python identifiers) will be loaded.</p>
  2005. <p>All test modules must be importable from the top level of the project. If
  2006. the start directory is not the top level directory then the top level
  2007. directory must be specified separately.</p>
  2008. <p>If importing a module fails, for example due to a syntax error, then
  2009. this will be recorded as a single error and discovery will continue. If
  2010. the import failure is due to <a class="reference internal" href="#unittest.SkipTest" title="unittest.SkipTest"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SkipTest</span></code></a> being raised, it will be
  2011. recorded as a skip instead of an error.</p>
  2012. <p>If a package (a directory containing a file named <code class="file docutils literal notranslate"><span class="pre">__init__.py</span></code>) is
  2013. found, the package will be checked for a <code class="docutils literal notranslate"><span class="pre">load_tests</span></code> function. If this
  2014. exists then it will be called
  2015. <code class="docutils literal notranslate"><span class="pre">package.load_tests(loader,</span> <span class="pre">tests,</span> <span class="pre">pattern)</span></code>. Test discovery takes care
  2016. to ensure that a package is only checked for tests once during an
  2017. invocation, even if the load_tests function itself calls
  2018. <code class="docutils literal notranslate"><span class="pre">loader.discover</span></code>.</p>
  2019. <p>If <code class="docutils literal notranslate"><span class="pre">load_tests</span></code> exists then discovery does <em>not</em> recurse into the
  2020. package, <code class="docutils literal notranslate"><span class="pre">load_tests</span></code> is responsible for loading all tests in the
  2021. package.</p>
  2022. <p>The pattern is deliberately not stored as a loader attribute so that
  2023. packages can continue discovery themselves. <em>top_level_dir</em> is stored so
  2024. <code class="docutils literal notranslate"><span class="pre">load_tests</span></code> does not need to pass this argument in to
  2025. <code class="docutils literal notranslate"><span class="pre">loader.discover()</span></code>.</p>
  2026. <p><em>start_dir</em> can be a dotted module name as well as a directory.</p>
  2027. <div class="versionadded">
  2028. <p><span class="versionmodified added">New in version 3.2.</span></p>
  2029. </div>
  2030. <div class="versionchanged">
  2031. <p><span class="versionmodified changed">Changed in version 3.4: </span>Modules that raise <a class="reference internal" href="#unittest.SkipTest" title="unittest.SkipTest"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SkipTest</span></code></a> on import are recorded as skips,
  2032. not errors.</p>
  2033. </div>
  2034. <div class="versionchanged">
  2035. <p><span class="versionmodified changed">Changed in version 3.4: </span><em>start_dir</em> can be a <a class="reference internal" href="../glossary.html#term-namespace-package"><span class="xref std std-term">namespace packages</span></a>.</p>
  2036. </div>
  2037. <div class="versionchanged">
  2038. <p><span class="versionmodified changed">Changed in version 3.4: </span>Paths are sorted before being imported so that execution order is the
  2039. same even if the underlying file system’s ordering is not dependent
  2040. on file name.</p>
  2041. </div>
  2042. <div class="versionchanged">
  2043. <p><span class="versionmodified changed">Changed in version 3.5: </span>Found packages are now checked for <code class="docutils literal notranslate"><span class="pre">load_tests</span></code> regardless of
  2044. whether their path matches <em>pattern</em>, because it is impossible for
  2045. a package name to match the default pattern.</p>
  2046. </div>
  2047. <div class="versionchanged">
  2048. <p><span class="versionmodified changed">Changed in version 3.11: </span><em>start_dir</em> can not be a <a class="reference internal" href="../glossary.html#term-namespace-package"><span class="xref std std-term">namespace packages</span></a>.
  2049. It has been broken since Python 3.7 and Python 3.11 officially remove it.</p>
  2050. </div>
  2051. </dd></dl>
  2052. <p>The following attributes of a <a class="reference internal" href="#unittest.TestLoader" title="unittest.TestLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestLoader</span></code></a> can be configured either by
  2053. subclassing or assignment on an instance:</p>
  2054. <dl class="py attribute">
  2055. <dt class="sig sig-object py" id="unittest.TestLoader.testMethodPrefix">
  2056. <span class="sig-name descname"><span class="pre">testMethodPrefix</span></span><a class="headerlink" href="#unittest.TestLoader.testMethodPrefix" title="Link to this definition">¶</a></dt>
  2057. <dd><p>String giving the prefix of method names which will be interpreted as test
  2058. methods. The default value is <code class="docutils literal notranslate"><span class="pre">'test'</span></code>.</p>
  2059. <p>This affects <a class="reference internal" href="#unittest.TestLoader.getTestCaseNames" title="unittest.TestLoader.getTestCaseNames"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getTestCaseNames()</span></code></a> and all the <code class="docutils literal notranslate"><span class="pre">loadTestsFrom*</span></code>
  2060. methods.</p>
  2061. </dd></dl>
  2062. <dl class="py attribute">
  2063. <dt class="sig sig-object py" id="unittest.TestLoader.sortTestMethodsUsing">
  2064. <span class="sig-name descname"><span class="pre">sortTestMethodsUsing</span></span><a class="headerlink" href="#unittest.TestLoader.sortTestMethodsUsing" title="Link to this definition">¶</a></dt>
  2065. <dd><p>Function to be used to compare method names when sorting them in
  2066. <a class="reference internal" href="#unittest.TestLoader.getTestCaseNames" title="unittest.TestLoader.getTestCaseNames"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getTestCaseNames()</span></code></a> and all the <code class="docutils literal notranslate"><span class="pre">loadTestsFrom*</span></code> methods.</p>
  2067. </dd></dl>
  2068. <dl class="py attribute">
  2069. <dt class="sig sig-object py" id="unittest.TestLoader.suiteClass">
  2070. <span class="sig-name descname"><span class="pre">suiteClass</span></span><a class="headerlink" href="#unittest.TestLoader.suiteClass" title="Link to this definition">¶</a></dt>
  2071. <dd><p>Callable object that constructs a test suite from a list of tests. No
  2072. methods on the resulting object are needed. The default value is the
  2073. <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a> class.</p>
  2074. <p>This affects all the <code class="docutils literal notranslate"><span class="pre">loadTestsFrom*</span></code> methods.</p>
  2075. </dd></dl>
  2076. <dl class="py attribute">
  2077. <dt class="sig sig-object py" id="unittest.TestLoader.testNamePatterns">
  2078. <span class="sig-name descname"><span class="pre">testNamePatterns</span></span><a class="headerlink" href="#unittest.TestLoader.testNamePatterns" title="Link to this definition">¶</a></dt>
  2079. <dd><p>List of Unix shell-style wildcard test name patterns that test methods
  2080. have to match to be included in test suites (see <code class="docutils literal notranslate"><span class="pre">-k</span></code> option).</p>
  2081. <p>If this attribute is not <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default), all test methods to be
  2082. included in test suites must match one of the patterns in this list.
  2083. Note that matches are always performed using <a class="reference internal" href="fnmatch.html#fnmatch.fnmatchcase" title="fnmatch.fnmatchcase"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fnmatch.fnmatchcase()</span></code></a>,
  2084. so unlike patterns passed to the <code class="docutils literal notranslate"><span class="pre">-k</span></code> option, simple substring patterns
  2085. will have to be converted using <code class="docutils literal notranslate"><span class="pre">*</span></code> wildcards.</p>
  2086. <p>This affects all the <code class="docutils literal notranslate"><span class="pre">loadTestsFrom*</span></code> methods.</p>
  2087. <div class="versionadded">
  2088. <p><span class="versionmodified added">New in version 3.7.</span></p>
  2089. </div>
  2090. </dd></dl>
  2091. </dd></dl>
  2092. <dl class="py class">
  2093. <dt class="sig sig-object py" id="unittest.TestResult">
  2094. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">TestResult</span></span><a class="headerlink" href="#unittest.TestResult" title="Link to this definition">¶</a></dt>
  2095. <dd><p>This class is used to compile information about which tests have succeeded
  2096. and which have failed.</p>
  2097. <p>A <a class="reference internal" href="#unittest.TestResult" title="unittest.TestResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResult</span></code></a> object stores the results of a set of tests. The
  2098. <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> and <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a> classes ensure that results are
  2099. properly recorded; test authors do not need to worry about recording the
  2100. outcome of tests.</p>
  2101. <p>Testing frameworks built on top of <a class="reference internal" href="#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> may want access to the
  2102. <a class="reference internal" href="#unittest.TestResult" title="unittest.TestResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResult</span></code></a> object generated by running a set of tests for reporting
  2103. purposes; a <a class="reference internal" href="#unittest.TestResult" title="unittest.TestResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResult</span></code></a> instance is returned by the
  2104. <code class="xref py py-meth docutils literal notranslate"><span class="pre">TestRunner.run()</span></code> method for this purpose.</p>
  2105. <p><a class="reference internal" href="#unittest.TestResult" title="unittest.TestResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResult</span></code></a> instances have the following attributes that will be of
  2106. interest when inspecting the results of running a set of tests:</p>
  2107. <dl class="py attribute">
  2108. <dt class="sig sig-object py" id="unittest.TestResult.errors">
  2109. <span class="sig-name descname"><span class="pre">errors</span></span><a class="headerlink" href="#unittest.TestResult.errors" title="Link to this definition">¶</a></dt>
  2110. <dd><p>A list containing 2-tuples of <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> instances and strings
  2111. holding formatted tracebacks. Each tuple represents a test which raised an
  2112. unexpected exception.</p>
  2113. </dd></dl>
  2114. <dl class="py attribute">
  2115. <dt class="sig sig-object py" id="unittest.TestResult.failures">
  2116. <span class="sig-name descname"><span class="pre">failures</span></span><a class="headerlink" href="#unittest.TestResult.failures" title="Link to this definition">¶</a></dt>
  2117. <dd><p>A list containing 2-tuples of <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> instances and strings
  2118. holding formatted tracebacks. Each tuple represents a test where a failure
  2119. was explicitly signalled using the <a class="reference internal" href="#assert-methods"><span class="std std-ref">assert* methods</span></a>.</p>
  2120. </dd></dl>
  2121. <dl class="py attribute">
  2122. <dt class="sig sig-object py" id="unittest.TestResult.skipped">
  2123. <span class="sig-name descname"><span class="pre">skipped</span></span><a class="headerlink" href="#unittest.TestResult.skipped" title="Link to this definition">¶</a></dt>
  2124. <dd><p>A list containing 2-tuples of <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> instances and strings
  2125. holding the reason for skipping the test.</p>
  2126. <div class="versionadded">
  2127. <p><span class="versionmodified added">New in version 3.1.</span></p>
  2128. </div>
  2129. </dd></dl>
  2130. <dl class="py attribute">
  2131. <dt class="sig sig-object py" id="unittest.TestResult.expectedFailures">
  2132. <span class="sig-name descname"><span class="pre">expectedFailures</span></span><a class="headerlink" href="#unittest.TestResult.expectedFailures" title="Link to this definition">¶</a></dt>
  2133. <dd><p>A list containing 2-tuples of <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> instances and strings
  2134. holding formatted tracebacks. Each tuple represents an expected failure
  2135. or error of the test case.</p>
  2136. </dd></dl>
  2137. <dl class="py attribute">
  2138. <dt class="sig sig-object py" id="unittest.TestResult.unexpectedSuccesses">
  2139. <span class="sig-name descname"><span class="pre">unexpectedSuccesses</span></span><a class="headerlink" href="#unittest.TestResult.unexpectedSuccesses" title="Link to this definition">¶</a></dt>
  2140. <dd><p>A list containing <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> instances that were marked as expected
  2141. failures, but succeeded.</p>
  2142. </dd></dl>
  2143. <dl class="py attribute">
  2144. <dt class="sig sig-object py" id="unittest.TestResult.collectedDurations">
  2145. <span class="sig-name descname"><span class="pre">collectedDurations</span></span><a class="headerlink" href="#unittest.TestResult.collectedDurations" title="Link to this definition">¶</a></dt>
  2146. <dd><p>A list containing 2-tuples of test case names and floats
  2147. representing the elapsed time of each test which was run.</p>
  2148. <div class="versionadded">
  2149. <p><span class="versionmodified added">New in version 3.12.</span></p>
  2150. </div>
  2151. </dd></dl>
  2152. <dl class="py attribute">
  2153. <dt class="sig sig-object py" id="unittest.TestResult.shouldStop">
  2154. <span class="sig-name descname"><span class="pre">shouldStop</span></span><a class="headerlink" href="#unittest.TestResult.shouldStop" title="Link to this definition">¶</a></dt>
  2155. <dd><p>Set to <code class="docutils literal notranslate"><span class="pre">True</span></code> when the execution of tests should stop by <a class="reference internal" href="#unittest.TestResult.stop" title="unittest.TestResult.stop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">stop()</span></code></a>.</p>
  2156. </dd></dl>
  2157. <dl class="py attribute">
  2158. <dt class="sig sig-object py" id="unittest.TestResult.testsRun">
  2159. <span class="sig-name descname"><span class="pre">testsRun</span></span><a class="headerlink" href="#unittest.TestResult.testsRun" title="Link to this definition">¶</a></dt>
  2160. <dd><p>The total number of tests run so far.</p>
  2161. </dd></dl>
  2162. <dl class="py attribute">
  2163. <dt class="sig sig-object py" id="unittest.TestResult.buffer">
  2164. <span class="sig-name descname"><span class="pre">buffer</span></span><a class="headerlink" href="#unittest.TestResult.buffer" title="Link to this definition">¶</a></dt>
  2165. <dd><p>If set to true, <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code> and <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code> will be buffered in between
  2166. <a class="reference internal" href="#unittest.TestResult.startTest" title="unittest.TestResult.startTest"><code class="xref py py-meth docutils literal notranslate"><span class="pre">startTest()</span></code></a> and <a class="reference internal" href="#unittest.TestResult.stopTest" title="unittest.TestResult.stopTest"><code class="xref py py-meth docutils literal notranslate"><span class="pre">stopTest()</span></code></a> being called. Collected output will
  2167. only be echoed onto the real <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code> and <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code> if the test
  2168. fails or errors. Any output is also attached to the failure / error message.</p>
  2169. <div class="versionadded">
  2170. <p><span class="versionmodified added">New in version 3.2.</span></p>
  2171. </div>
  2172. </dd></dl>
  2173. <dl class="py attribute">
  2174. <dt class="sig sig-object py" id="unittest.TestResult.failfast">
  2175. <span class="sig-name descname"><span class="pre">failfast</span></span><a class="headerlink" href="#unittest.TestResult.failfast" title="Link to this definition">¶</a></dt>
  2176. <dd><p>If set to true <a class="reference internal" href="#unittest.TestResult.stop" title="unittest.TestResult.stop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">stop()</span></code></a> will be called on the first failure or error,
  2177. halting the test run.</p>
  2178. <div class="versionadded">
  2179. <p><span class="versionmodified added">New in version 3.2.</span></p>
  2180. </div>
  2181. </dd></dl>
  2182. <dl class="py attribute">
  2183. <dt class="sig sig-object py" id="unittest.TestResult.tb_locals">
  2184. <span class="sig-name descname"><span class="pre">tb_locals</span></span><a class="headerlink" href="#unittest.TestResult.tb_locals" title="Link to this definition">¶</a></dt>
  2185. <dd><p>If set to true then local variables will be shown in tracebacks.</p>
  2186. <div class="versionadded">
  2187. <p><span class="versionmodified added">New in version 3.5.</span></p>
  2188. </div>
  2189. </dd></dl>
  2190. <dl class="py method">
  2191. <dt class="sig sig-object py" id="unittest.TestResult.wasSuccessful">
  2192. <span class="sig-name descname"><span class="pre">wasSuccessful</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestResult.wasSuccessful" title="Link to this definition">¶</a></dt>
  2193. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if all tests run so far have passed, otherwise returns
  2194. <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  2195. <div class="versionchanged">
  2196. <p><span class="versionmodified changed">Changed in version 3.4: </span>Returns <code class="docutils literal notranslate"><span class="pre">False</span></code> if there were any <a class="reference internal" href="#unittest.TestResult.unexpectedSuccesses" title="unittest.TestResult.unexpectedSuccesses"><code class="xref py py-attr docutils literal notranslate"><span class="pre">unexpectedSuccesses</span></code></a>
  2197. from tests marked with the <a class="reference internal" href="#unittest.expectedFailure" title="unittest.expectedFailure"><code class="xref py py-func docutils literal notranslate"><span class="pre">expectedFailure()</span></code></a> decorator.</p>
  2198. </div>
  2199. </dd></dl>
  2200. <dl class="py method">
  2201. <dt class="sig sig-object py" id="unittest.TestResult.stop">
  2202. <span class="sig-name descname"><span class="pre">stop</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestResult.stop" title="Link to this definition">¶</a></dt>
  2203. <dd><p>This method can be called to signal that the set of tests being run should
  2204. be aborted by setting the <a class="reference internal" href="#unittest.TestResult.shouldStop" title="unittest.TestResult.shouldStop"><code class="xref py py-attr docutils literal notranslate"><span class="pre">shouldStop</span></code></a> attribute to <code class="docutils literal notranslate"><span class="pre">True</span></code>.
  2205. <code class="xref py py-class docutils literal notranslate"><span class="pre">TestRunner</span></code> objects should respect this flag and return without
  2206. running any additional tests.</p>
  2207. <p>For example, this feature is used by the <a class="reference internal" href="#unittest.TextTestRunner" title="unittest.TextTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextTestRunner</span></code></a> class to
  2208. stop the test framework when the user signals an interrupt from the
  2209. keyboard. Interactive tools which provide <code class="xref py py-class docutils literal notranslate"><span class="pre">TestRunner</span></code>
  2210. implementations can use this in a similar manner.</p>
  2211. </dd></dl>
  2212. <p>The following methods of the <a class="reference internal" href="#unittest.TestResult" title="unittest.TestResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResult</span></code></a> class are used to maintain
  2213. the internal data structures, and may be extended in subclasses to support
  2214. additional reporting requirements. This is particularly useful in building
  2215. tools which support interactive reporting while tests are being run.</p>
  2216. <dl class="py method">
  2217. <dt class="sig sig-object py" id="unittest.TestResult.startTest">
  2218. <span class="sig-name descname"><span class="pre">startTest</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestResult.startTest" title="Link to this definition">¶</a></dt>
  2219. <dd><p>Called when the test case <em>test</em> is about to be run.</p>
  2220. </dd></dl>
  2221. <dl class="py method">
  2222. <dt class="sig sig-object py" id="unittest.TestResult.stopTest">
  2223. <span class="sig-name descname"><span class="pre">stopTest</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestResult.stopTest" title="Link to this definition">¶</a></dt>
  2224. <dd><p>Called after the test case <em>test</em> has been executed, regardless of the
  2225. outcome.</p>
  2226. </dd></dl>
  2227. <dl class="py method">
  2228. <dt class="sig sig-object py" id="unittest.TestResult.startTestRun">
  2229. <span class="sig-name descname"><span class="pre">startTestRun</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestResult.startTestRun" title="Link to this definition">¶</a></dt>
  2230. <dd><p>Called once before any tests are executed.</p>
  2231. <div class="versionadded">
  2232. <p><span class="versionmodified added">New in version 3.1.</span></p>
  2233. </div>
  2234. </dd></dl>
  2235. <dl class="py method">
  2236. <dt class="sig sig-object py" id="unittest.TestResult.stopTestRun">
  2237. <span class="sig-name descname"><span class="pre">stopTestRun</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestResult.stopTestRun" title="Link to this definition">¶</a></dt>
  2238. <dd><p>Called once after all tests are executed.</p>
  2239. <div class="versionadded">
  2240. <p><span class="versionmodified added">New in version 3.1.</span></p>
  2241. </div>
  2242. </dd></dl>
  2243. <dl class="py method">
  2244. <dt class="sig sig-object py" id="unittest.TestResult.addError">
  2245. <span class="sig-name descname"><span class="pre">addError</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">err</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestResult.addError" title="Link to this definition">¶</a></dt>
  2246. <dd><p>Called when the test case <em>test</em> raises an unexpected exception. <em>err</em> is a
  2247. tuple of the form returned by <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_info()</span></code></a>: <code class="docutils literal notranslate"><span class="pre">(type,</span> <span class="pre">value,</span>
  2248. <span class="pre">traceback)</span></code>.</p>
  2249. <p>The default implementation appends a tuple <code class="docutils literal notranslate"><span class="pre">(test,</span> <span class="pre">formatted_err)</span></code> to
  2250. the instance’s <a class="reference internal" href="#unittest.TestResult.errors" title="unittest.TestResult.errors"><code class="xref py py-attr docutils literal notranslate"><span class="pre">errors</span></code></a> attribute, where <em>formatted_err</em> is a
  2251. formatted traceback derived from <em>err</em>.</p>
  2252. </dd></dl>
  2253. <dl class="py method">
  2254. <dt class="sig sig-object py" id="unittest.TestResult.addFailure">
  2255. <span class="sig-name descname"><span class="pre">addFailure</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">err</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestResult.addFailure" title="Link to this definition">¶</a></dt>
  2256. <dd><p>Called when the test case <em>test</em> signals a failure. <em>err</em> is a tuple of
  2257. the form returned by <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_info()</span></code></a>: <code class="docutils literal notranslate"><span class="pre">(type,</span> <span class="pre">value,</span> <span class="pre">traceback)</span></code>.</p>
  2258. <p>The default implementation appends a tuple <code class="docutils literal notranslate"><span class="pre">(test,</span> <span class="pre">formatted_err)</span></code> to
  2259. the instance’s <a class="reference internal" href="#unittest.TestResult.failures" title="unittest.TestResult.failures"><code class="xref py py-attr docutils literal notranslate"><span class="pre">failures</span></code></a> attribute, where <em>formatted_err</em> is a
  2260. formatted traceback derived from <em>err</em>.</p>
  2261. </dd></dl>
  2262. <dl class="py method">
  2263. <dt class="sig sig-object py" id="unittest.TestResult.addSuccess">
  2264. <span class="sig-name descname"><span class="pre">addSuccess</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestResult.addSuccess" title="Link to this definition">¶</a></dt>
  2265. <dd><p>Called when the test case <em>test</em> succeeds.</p>
  2266. <p>The default implementation does nothing.</p>
  2267. </dd></dl>
  2268. <dl class="py method">
  2269. <dt class="sig sig-object py" id="unittest.TestResult.addSkip">
  2270. <span class="sig-name descname"><span class="pre">addSkip</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">reason</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestResult.addSkip" title="Link to this definition">¶</a></dt>
  2271. <dd><p>Called when the test case <em>test</em> is skipped. <em>reason</em> is the reason the
  2272. test gave for skipping.</p>
  2273. <p>The default implementation appends a tuple <code class="docutils literal notranslate"><span class="pre">(test,</span> <span class="pre">reason)</span></code> to the
  2274. instance’s <a class="reference internal" href="#unittest.TestResult.skipped" title="unittest.TestResult.skipped"><code class="xref py py-attr docutils literal notranslate"><span class="pre">skipped</span></code></a> attribute.</p>
  2275. </dd></dl>
  2276. <dl class="py method">
  2277. <dt class="sig sig-object py" id="unittest.TestResult.addExpectedFailure">
  2278. <span class="sig-name descname"><span class="pre">addExpectedFailure</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">err</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestResult.addExpectedFailure" title="Link to this definition">¶</a></dt>
  2279. <dd><p>Called when the test case <em>test</em> fails or errors, but was marked with
  2280. the <a class="reference internal" href="#unittest.expectedFailure" title="unittest.expectedFailure"><code class="xref py py-func docutils literal notranslate"><span class="pre">expectedFailure()</span></code></a> decorator.</p>
  2281. <p>The default implementation appends a tuple <code class="docutils literal notranslate"><span class="pre">(test,</span> <span class="pre">formatted_err)</span></code> to
  2282. the instance’s <a class="reference internal" href="#unittest.TestResult.expectedFailures" title="unittest.TestResult.expectedFailures"><code class="xref py py-attr docutils literal notranslate"><span class="pre">expectedFailures</span></code></a> attribute, where <em>formatted_err</em>
  2283. is a formatted traceback derived from <em>err</em>.</p>
  2284. </dd></dl>
  2285. <dl class="py method">
  2286. <dt class="sig sig-object py" id="unittest.TestResult.addUnexpectedSuccess">
  2287. <span class="sig-name descname"><span class="pre">addUnexpectedSuccess</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestResult.addUnexpectedSuccess" title="Link to this definition">¶</a></dt>
  2288. <dd><p>Called when the test case <em>test</em> was marked with the
  2289. <a class="reference internal" href="#unittest.expectedFailure" title="unittest.expectedFailure"><code class="xref py py-func docutils literal notranslate"><span class="pre">expectedFailure()</span></code></a> decorator, but succeeded.</p>
  2290. <p>The default implementation appends the test to the instance’s
  2291. <a class="reference internal" href="#unittest.TestResult.unexpectedSuccesses" title="unittest.TestResult.unexpectedSuccesses"><code class="xref py py-attr docutils literal notranslate"><span class="pre">unexpectedSuccesses</span></code></a> attribute.</p>
  2292. </dd></dl>
  2293. <dl class="py method">
  2294. <dt class="sig sig-object py" id="unittest.TestResult.addSubTest">
  2295. <span class="sig-name descname"><span class="pre">addSubTest</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">subtest</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">outcome</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestResult.addSubTest" title="Link to this definition">¶</a></dt>
  2296. <dd><p>Called when a subtest finishes. <em>test</em> is the test case
  2297. corresponding to the test method. <em>subtest</em> is a custom
  2298. <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> instance describing the subtest.</p>
  2299. <p>If <em>outcome</em> is <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>, the subtest succeeded. Otherwise,
  2300. it failed with an exception where <em>outcome</em> is a tuple of the form
  2301. returned by <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_info()</span></code></a>: <code class="docutils literal notranslate"><span class="pre">(type,</span> <span class="pre">value,</span> <span class="pre">traceback)</span></code>.</p>
  2302. <p>The default implementation does nothing when the outcome is a
  2303. success, and records subtest failures as normal failures.</p>
  2304. <div class="versionadded">
  2305. <p><span class="versionmodified added">New in version 3.4.</span></p>
  2306. </div>
  2307. </dd></dl>
  2308. <dl class="py method">
  2309. <dt class="sig sig-object py" id="unittest.TestResult.addDuration">
  2310. <span class="sig-name descname"><span class="pre">addDuration</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">elapsed</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TestResult.addDuration" title="Link to this definition">¶</a></dt>
  2311. <dd><p>Called when the test case finishes. <em>elapsed</em> is the time represented in
  2312. seconds, and it includes the execution of cleanup functions.</p>
  2313. <div class="versionadded">
  2314. <p><span class="versionmodified added">New in version 3.12.</span></p>
  2315. </div>
  2316. </dd></dl>
  2317. </dd></dl>
  2318. <dl class="py class">
  2319. <dt class="sig sig-object py" id="unittest.TextTestResult">
  2320. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">TextTestResult</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">stream</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">descriptions</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbosity</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">durations</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TextTestResult" title="Link to this definition">¶</a></dt>
  2321. <dd><p>A concrete implementation of <a class="reference internal" href="#unittest.TestResult" title="unittest.TestResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResult</span></code></a> used by the
  2322. <a class="reference internal" href="#unittest.TextTestRunner" title="unittest.TextTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextTestRunner</span></code></a>. Subclasses should accept <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code> to ensure
  2323. compatibility as the interface changes.</p>
  2324. <div class="versionadded">
  2325. <p><span class="versionmodified added">New in version 3.2.</span></p>
  2326. </div>
  2327. <div class="versionchanged">
  2328. <p><span class="versionmodified changed">Changed in version 3.12: </span>Added the <em>durations</em> keyword parameter.</p>
  2329. </div>
  2330. </dd></dl>
  2331. <dl class="py data">
  2332. <dt class="sig sig-object py" id="unittest.defaultTestLoader">
  2333. <span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">defaultTestLoader</span></span><a class="headerlink" href="#unittest.defaultTestLoader" title="Link to this definition">¶</a></dt>
  2334. <dd><p>Instance of the <a class="reference internal" href="#unittest.TestLoader" title="unittest.TestLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestLoader</span></code></a> class intended to be shared. If no
  2335. customization of the <a class="reference internal" href="#unittest.TestLoader" title="unittest.TestLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestLoader</span></code></a> is needed, this instance can be used
  2336. instead of repeatedly creating new instances.</p>
  2337. </dd></dl>
  2338. <dl class="py class">
  2339. <dt class="sig sig-object py" id="unittest.TextTestRunner">
  2340. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">TextTestRunner</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">stream</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">descriptions</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbosity</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">failfast</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffer</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">resultclass</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">warnings</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tb_locals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">durations</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TextTestRunner" title="Link to this definition">¶</a></dt>
  2341. <dd><p>A basic test runner implementation that outputs results to a stream. If <em>stream</em>
  2342. is <code class="docutils literal notranslate"><span class="pre">None</span></code>, the default, <a class="reference internal" href="sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> is used as the output stream. This class
  2343. has a few configurable parameters, but is essentially very simple. Graphical
  2344. applications which run test suites should provide alternate implementations. Such
  2345. implementations should accept <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code> as the interface to construct runners
  2346. changes when features are added to unittest.</p>
  2347. <p>By default this runner shows <a class="reference internal" href="exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a>,
  2348. <a class="reference internal" href="exceptions.html#PendingDeprecationWarning" title="PendingDeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PendingDeprecationWarning</span></code></a>, <a class="reference internal" href="exceptions.html#ResourceWarning" title="ResourceWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ResourceWarning</span></code></a> and
  2349. <a class="reference internal" href="exceptions.html#ImportWarning" title="ImportWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportWarning</span></code></a> even if they are <a class="reference internal" href="warnings.html#warning-ignored"><span class="std std-ref">ignored by default</span></a>. This behavior can
  2350. be overridden using Python’s <code class="xref std std-option docutils literal notranslate"><span class="pre">-Wd</span></code> or <code class="xref std std-option docutils literal notranslate"><span class="pre">-Wa</span></code> options
  2351. (see <a class="reference internal" href="../using/cmdline.html#using-on-warnings"><span class="std std-ref">Warning control</span></a>) and leaving
  2352. <em>warnings</em> to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  2353. <div class="versionchanged">
  2354. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added the <em>warnings</em> parameter.</p>
  2355. </div>
  2356. <div class="versionchanged">
  2357. <p><span class="versionmodified changed">Changed in version 3.2: </span>The default stream is set to <a class="reference internal" href="sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> at instantiation time rather
  2358. than import time.</p>
  2359. </div>
  2360. <div class="versionchanged">
  2361. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added the <em>tb_locals</em> parameter.</p>
  2362. </div>
  2363. <div class="versionchanged">
  2364. <p><span class="versionmodified changed">Changed in version 3.12: </span>Added the <em>durations</em> parameter.</p>
  2365. </div>
  2366. <dl class="py method">
  2367. <dt class="sig sig-object py" id="unittest.TextTestRunner._makeResult">
  2368. <span class="sig-name descname"><span class="pre">_makeResult</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TextTestRunner._makeResult" title="Link to this definition">¶</a></dt>
  2369. <dd><p>This method returns the instance of <code class="docutils literal notranslate"><span class="pre">TestResult</span></code> used by <a class="reference internal" href="#unittest.TextTestRunner.run" title="unittest.TextTestRunner.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a>.
  2370. It is not intended to be called directly, but can be overridden in
  2371. subclasses to provide a custom <code class="docutils literal notranslate"><span class="pre">TestResult</span></code>.</p>
  2372. <p><code class="docutils literal notranslate"><span class="pre">_makeResult()</span></code> instantiates the class or callable passed in the
  2373. <code class="docutils literal notranslate"><span class="pre">TextTestRunner</span></code> constructor as the <code class="docutils literal notranslate"><span class="pre">resultclass</span></code> argument. It
  2374. defaults to <a class="reference internal" href="#unittest.TextTestResult" title="unittest.TextTestResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextTestResult</span></code></a> if no <code class="docutils literal notranslate"><span class="pre">resultclass</span></code> is provided.
  2375. The result class is instantiated with the following arguments:</p>
  2376. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">stream</span><span class="p">,</span> <span class="n">descriptions</span><span class="p">,</span> <span class="n">verbosity</span>
  2377. </pre></div>
  2378. </div>
  2379. </dd></dl>
  2380. <dl class="py method">
  2381. <dt class="sig sig-object py" id="unittest.TextTestRunner.run">
  2382. <span class="sig-name descname"><span class="pre">run</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.TextTestRunner.run" title="Link to this definition">¶</a></dt>
  2383. <dd><p>This method is the main public interface to the <code class="docutils literal notranslate"><span class="pre">TextTestRunner</span></code>. This
  2384. method takes a <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a> or <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> instance. A
  2385. <a class="reference internal" href="#unittest.TestResult" title="unittest.TestResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResult</span></code></a> is created by calling
  2386. <a class="reference internal" href="#unittest.TextTestRunner._makeResult" title="unittest.TextTestRunner._makeResult"><code class="xref py py-func docutils literal notranslate"><span class="pre">_makeResult()</span></code></a> and the test(s) are run and the
  2387. results printed to stdout.</p>
  2388. </dd></dl>
  2389. </dd></dl>
  2390. <dl class="py function">
  2391. <dt class="sig sig-object py" id="unittest.main">
  2392. <span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">main</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'__main__'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">defaultTest</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">argv</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">testRunner</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">testLoader</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">unittest.defaultTestLoader</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exit</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbosity</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">failfast</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">catchbreak</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffer</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">warnings</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.main" title="Link to this definition">¶</a></dt>
  2393. <dd><p>A command-line program that loads a set of tests from <em>module</em> and runs them;
  2394. this is primarily for making test modules conveniently executable.
  2395. The simplest use for this function is to include the following line at the
  2396. end of a test script:</p>
  2397. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
  2398. <span class="n">unittest</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
  2399. </pre></div>
  2400. </div>
  2401. <p>You can run tests with more detailed information by passing in the verbosity
  2402. argument:</p>
  2403. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
  2404. <span class="n">unittest</span><span class="o">.</span><span class="n">main</span><span class="p">(</span><span class="n">verbosity</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
  2405. </pre></div>
  2406. </div>
  2407. <p>The <em>defaultTest</em> argument is either the name of a single test or an
  2408. iterable of test names to run if no test names are specified via <em>argv</em>. If
  2409. not specified or <code class="docutils literal notranslate"><span class="pre">None</span></code> and no test names are provided via <em>argv</em>, all
  2410. tests found in <em>module</em> are run.</p>
  2411. <p>The <em>argv</em> argument can be a list of options passed to the program, with the
  2412. first element being the program name. If not specified or <code class="docutils literal notranslate"><span class="pre">None</span></code>,
  2413. the values of <a class="reference internal" href="sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> are used.</p>
  2414. <p>The <em>testRunner</em> argument can either be a test runner class or an already
  2415. created instance of it. By default <code class="docutils literal notranslate"><span class="pre">main</span></code> calls <a class="reference internal" href="sys.html#sys.exit" title="sys.exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exit()</span></code></a> with
  2416. an exit code indicating success (0) or failure (1) of the tests run.
  2417. An exit code of 5 indicates that no tests were run or skipped.</p>
  2418. <p>The <em>testLoader</em> argument has to be a <a class="reference internal" href="#unittest.TestLoader" title="unittest.TestLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestLoader</span></code></a> instance,
  2419. and defaults to <a class="reference internal" href="#unittest.defaultTestLoader" title="unittest.defaultTestLoader"><code class="xref py py-data docutils literal notranslate"><span class="pre">defaultTestLoader</span></code></a>.</p>
  2420. <p><code class="docutils literal notranslate"><span class="pre">main</span></code> supports being used from the interactive interpreter by passing in the
  2421. argument <code class="docutils literal notranslate"><span class="pre">exit=False</span></code>. This displays the result on standard output without
  2422. calling <a class="reference internal" href="sys.html#sys.exit" title="sys.exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exit()</span></code></a>:</p>
  2423. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">unittest</span> <span class="kn">import</span> <span class="n">main</span>
  2424. <span class="gp">&gt;&gt;&gt; </span><span class="n">main</span><span class="p">(</span><span class="n">module</span><span class="o">=</span><span class="s1">&#39;test_module&#39;</span><span class="p">,</span> <span class="n">exit</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
  2425. </pre></div>
  2426. </div>
  2427. <p>The <em>failfast</em>, <em>catchbreak</em> and <em>buffer</em> parameters have the same
  2428. effect as the same-name <a class="reference internal" href="#command-line-options">command-line options</a>.</p>
  2429. <p>The <em>warnings</em> argument specifies the <a class="reference internal" href="warnings.html#warning-filter"><span class="std std-ref">warning filter</span></a>
  2430. that should be used while running the tests. If it’s not specified, it will
  2431. remain <code class="docutils literal notranslate"><span class="pre">None</span></code> if a <code class="xref std std-option docutils literal notranslate"><span class="pre">-W</span></code> option is passed to <strong class="program">python</strong>
  2432. (see <a class="reference internal" href="../using/cmdline.html#using-on-warnings"><span class="std std-ref">Warning control</span></a>),
  2433. otherwise it will be set to <code class="docutils literal notranslate"><span class="pre">'default'</span></code>.</p>
  2434. <p>Calling <code class="docutils literal notranslate"><span class="pre">main</span></code> actually returns an instance of the <code class="docutils literal notranslate"><span class="pre">TestProgram</span></code> class.
  2435. This stores the result of the tests run as the <code class="docutils literal notranslate"><span class="pre">result</span></code> attribute.</p>
  2436. <div class="versionchanged">
  2437. <p><span class="versionmodified changed">Changed in version 3.1: </span>The <em>exit</em> parameter was added.</p>
  2438. </div>
  2439. <div class="versionchanged">
  2440. <p><span class="versionmodified changed">Changed in version 3.2: </span>The <em>verbosity</em>, <em>failfast</em>, <em>catchbreak</em>, <em>buffer</em>
  2441. and <em>warnings</em> parameters were added.</p>
  2442. </div>
  2443. <div class="versionchanged">
  2444. <p><span class="versionmodified changed">Changed in version 3.4: </span>The <em>defaultTest</em> parameter was changed to also accept an iterable of
  2445. test names.</p>
  2446. </div>
  2447. </dd></dl>
  2448. <section id="load-tests-protocol">
  2449. <span id="id1"></span><h4>load_tests Protocol<a class="headerlink" href="#load-tests-protocol" title="Link to this heading">¶</a></h4>
  2450. <div class="versionadded">
  2451. <p><span class="versionmodified added">New in version 3.2.</span></p>
  2452. </div>
  2453. <p>Modules or packages can customize how tests are loaded from them during normal
  2454. test runs or test discovery by implementing a function called <code class="docutils literal notranslate"><span class="pre">load_tests</span></code>.</p>
  2455. <p>If a test module defines <code class="docutils literal notranslate"><span class="pre">load_tests</span></code> it will be called by
  2456. <a class="reference internal" href="#unittest.TestLoader.loadTestsFromModule" title="unittest.TestLoader.loadTestsFromModule"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TestLoader.loadTestsFromModule()</span></code></a> with the following arguments:</p>
  2457. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">load_tests</span><span class="p">(</span><span class="n">loader</span><span class="p">,</span> <span class="n">standard_tests</span><span class="p">,</span> <span class="n">pattern</span><span class="p">)</span>
  2458. </pre></div>
  2459. </div>
  2460. <p>where <em>pattern</em> is passed straight through from <code class="docutils literal notranslate"><span class="pre">loadTestsFromModule</span></code>. It
  2461. defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  2462. <p>It should return a <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a>.</p>
  2463. <p><em>loader</em> is the instance of <a class="reference internal" href="#unittest.TestLoader" title="unittest.TestLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestLoader</span></code></a> doing the loading.
  2464. <em>standard_tests</em> are the tests that would be loaded by default from the
  2465. module. It is common for test modules to only want to add or remove tests
  2466. from the standard set of tests.
  2467. The third argument is used when loading packages as part of test discovery.</p>
  2468. <p>A typical <code class="docutils literal notranslate"><span class="pre">load_tests</span></code> function that loads tests from a specific set of
  2469. <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> classes may look like:</p>
  2470. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">test_cases</span> <span class="o">=</span> <span class="p">(</span><span class="n">TestCase1</span><span class="p">,</span> <span class="n">TestCase2</span><span class="p">,</span> <span class="n">TestCase3</span><span class="p">)</span>
  2471. <span class="k">def</span> <span class="nf">load_tests</span><span class="p">(</span><span class="n">loader</span><span class="p">,</span> <span class="n">tests</span><span class="p">,</span> <span class="n">pattern</span><span class="p">):</span>
  2472. <span class="n">suite</span> <span class="o">=</span> <span class="n">TestSuite</span><span class="p">()</span>
  2473. <span class="k">for</span> <span class="n">test_class</span> <span class="ow">in</span> <span class="n">test_cases</span><span class="p">:</span>
  2474. <span class="n">tests</span> <span class="o">=</span> <span class="n">loader</span><span class="o">.</span><span class="n">loadTestsFromTestCase</span><span class="p">(</span><span class="n">test_class</span><span class="p">)</span>
  2475. <span class="n">suite</span><span class="o">.</span><span class="n">addTests</span><span class="p">(</span><span class="n">tests</span><span class="p">)</span>
  2476. <span class="k">return</span> <span class="n">suite</span>
  2477. </pre></div>
  2478. </div>
  2479. <p>If discovery is started in a directory containing a package, either from the
  2480. command line or by calling <a class="reference internal" href="#unittest.TestLoader.discover" title="unittest.TestLoader.discover"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TestLoader.discover()</span></code></a>, then the package
  2481. <code class="file docutils literal notranslate"><span class="pre">__init__.py</span></code> will be checked for <code class="docutils literal notranslate"><span class="pre">load_tests</span></code>. If that function does
  2482. not exist, discovery will recurse into the package as though it were just
  2483. another directory. Otherwise, discovery of the package’s tests will be left up
  2484. to <code class="docutils literal notranslate"><span class="pre">load_tests</span></code> which is called with the following arguments:</p>
  2485. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">load_tests</span><span class="p">(</span><span class="n">loader</span><span class="p">,</span> <span class="n">standard_tests</span><span class="p">,</span> <span class="n">pattern</span><span class="p">)</span>
  2486. </pre></div>
  2487. </div>
  2488. <p>This should return a <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a> representing all the tests
  2489. from the package. (<code class="docutils literal notranslate"><span class="pre">standard_tests</span></code> will only contain tests
  2490. collected from <code class="file docutils literal notranslate"><span class="pre">__init__.py</span></code>.)</p>
  2491. <p>Because the pattern is passed into <code class="docutils literal notranslate"><span class="pre">load_tests</span></code> the package is free to
  2492. continue (and potentially modify) test discovery. A ‘do nothing’
  2493. <code class="docutils literal notranslate"><span class="pre">load_tests</span></code> function for a test package would look like:</p>
  2494. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">load_tests</span><span class="p">(</span><span class="n">loader</span><span class="p">,</span> <span class="n">standard_tests</span><span class="p">,</span> <span class="n">pattern</span><span class="p">):</span>
  2495. <span class="c1"># top level directory cached on loader instance</span>
  2496. <span class="n">this_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span>
  2497. <span class="n">package_tests</span> <span class="o">=</span> <span class="n">loader</span><span class="o">.</span><span class="n">discover</span><span class="p">(</span><span class="n">start_dir</span><span class="o">=</span><span class="n">this_dir</span><span class="p">,</span> <span class="n">pattern</span><span class="o">=</span><span class="n">pattern</span><span class="p">)</span>
  2498. <span class="n">standard_tests</span><span class="o">.</span><span class="n">addTests</span><span class="p">(</span><span class="n">package_tests</span><span class="p">)</span>
  2499. <span class="k">return</span> <span class="n">standard_tests</span>
  2500. </pre></div>
  2501. </div>
  2502. <div class="versionchanged">
  2503. <p><span class="versionmodified changed">Changed in version 3.5: </span>Discovery no longer checks package names for matching <em>pattern</em> due to the
  2504. impossibility of package names matching the default pattern.</p>
  2505. </div>
  2506. </section>
  2507. </section>
  2508. </section>
  2509. <section id="class-and-module-fixtures">
  2510. <h2>Class and Module Fixtures<a class="headerlink" href="#class-and-module-fixtures" title="Link to this heading">¶</a></h2>
  2511. <p>Class and module level fixtures are implemented in <a class="reference internal" href="#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestSuite</span></code></a>. When
  2512. the test suite encounters a test from a new class then <code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDownClass()</span></code>
  2513. from the previous class (if there is one) is called, followed by
  2514. <code class="xref py py-meth docutils literal notranslate"><span class="pre">setUpClass()</span></code> from the new class.</p>
  2515. <p>Similarly if a test is from a different module from the previous test then
  2516. <code class="docutils literal notranslate"><span class="pre">tearDownModule</span></code> from the previous module is run, followed by
  2517. <code class="docutils literal notranslate"><span class="pre">setUpModule</span></code> from the new module.</p>
  2518. <p>After all the tests have run the final <code class="docutils literal notranslate"><span class="pre">tearDownClass</span></code> and
  2519. <code class="docutils literal notranslate"><span class="pre">tearDownModule</span></code> are run.</p>
  2520. <p>Note that shared fixtures do not play well with [potential] features like test
  2521. parallelization and they break test isolation. They should be used with care.</p>
  2522. <p>The default ordering of tests created by the unittest test loaders is to group
  2523. all tests from the same modules and classes together. This will lead to
  2524. <code class="docutils literal notranslate"><span class="pre">setUpClass</span></code> / <code class="docutils literal notranslate"><span class="pre">setUpModule</span></code> (etc) being called exactly once per class and
  2525. module. If you randomize the order, so that tests from different modules and
  2526. classes are adjacent to each other, then these shared fixture functions may be
  2527. called multiple times in a single test run.</p>
  2528. <p>Shared fixtures are not intended to work with suites with non-standard
  2529. ordering. A <code class="docutils literal notranslate"><span class="pre">BaseTestSuite</span></code> still exists for frameworks that don’t want to
  2530. support shared fixtures.</p>
  2531. <p>If there are any exceptions raised during one of the shared fixture functions
  2532. the test is reported as an error. Because there is no corresponding test
  2533. instance an <code class="docutils literal notranslate"><span class="pre">_ErrorHolder</span></code> object (that has the same interface as a
  2534. <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a>) is created to represent the error. If you are just using
  2535. the standard unittest test runner then this detail doesn’t matter, but if you
  2536. are a framework author it may be relevant.</p>
  2537. <section id="setupclass-and-teardownclass">
  2538. <h3>setUpClass and tearDownClass<a class="headerlink" href="#setupclass-and-teardownclass" title="Link to this heading">¶</a></h3>
  2539. <p>These must be implemented as class methods:</p>
  2540. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">unittest</span>
  2541. <span class="k">class</span> <span class="nc">Test</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
  2542. <span class="nd">@classmethod</span>
  2543. <span class="k">def</span> <span class="nf">setUpClass</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
  2544. <span class="bp">cls</span><span class="o">.</span><span class="n">_connection</span> <span class="o">=</span> <span class="n">createExpensiveConnectionObject</span><span class="p">()</span>
  2545. <span class="nd">@classmethod</span>
  2546. <span class="k">def</span> <span class="nf">tearDownClass</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
  2547. <span class="bp">cls</span><span class="o">.</span><span class="n">_connection</span><span class="o">.</span><span class="n">destroy</span><span class="p">()</span>
  2548. </pre></div>
  2549. </div>
  2550. <p>If you want the <code class="docutils literal notranslate"><span class="pre">setUpClass</span></code> and <code class="docutils literal notranslate"><span class="pre">tearDownClass</span></code> on base classes called
  2551. then you must call up to them yourself. The implementations in
  2552. <a class="reference internal" href="#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> are empty.</p>
  2553. <p>If an exception is raised during a <code class="docutils literal notranslate"><span class="pre">setUpClass</span></code> then the tests in the class
  2554. are not run and the <code class="docutils literal notranslate"><span class="pre">tearDownClass</span></code> is not run. Skipped classes will not
  2555. have <code class="docutils literal notranslate"><span class="pre">setUpClass</span></code> or <code class="docutils literal notranslate"><span class="pre">tearDownClass</span></code> run. If the exception is a
  2556. <a class="reference internal" href="#unittest.SkipTest" title="unittest.SkipTest"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SkipTest</span></code></a> exception then the class will be reported as having been skipped
  2557. instead of as an error.</p>
  2558. </section>
  2559. <section id="setupmodule-and-teardownmodule">
  2560. <h3>setUpModule and tearDownModule<a class="headerlink" href="#setupmodule-and-teardownmodule" title="Link to this heading">¶</a></h3>
  2561. <p>These should be implemented as functions:</p>
  2562. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">setUpModule</span><span class="p">():</span>
  2563. <span class="n">createConnection</span><span class="p">()</span>
  2564. <span class="k">def</span> <span class="nf">tearDownModule</span><span class="p">():</span>
  2565. <span class="n">closeConnection</span><span class="p">()</span>
  2566. </pre></div>
  2567. </div>
  2568. <p>If an exception is raised in a <code class="docutils literal notranslate"><span class="pre">setUpModule</span></code> then none of the tests in the
  2569. module will be run and the <code class="docutils literal notranslate"><span class="pre">tearDownModule</span></code> will not be run. If the exception is a
  2570. <a class="reference internal" href="#unittest.SkipTest" title="unittest.SkipTest"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SkipTest</span></code></a> exception then the module will be reported as having been skipped
  2571. instead of as an error.</p>
  2572. <p>To add cleanup code that must be run even in the case of an exception, use
  2573. <code class="docutils literal notranslate"><span class="pre">addModuleCleanup</span></code>:</p>
  2574. <dl class="py function">
  2575. <dt class="sig sig-object py" id="unittest.addModuleCleanup">
  2576. <span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">addModuleCleanup</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">function</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.addModuleCleanup" title="Link to this definition">¶</a></dt>
  2577. <dd><p>Add a function to be called after <code class="xref py py-func docutils literal notranslate"><span class="pre">tearDownModule()</span></code> to cleanup
  2578. resources used during the test class. Functions will be called in reverse
  2579. order to the order they are added (<abbr title="last-in, first-out">LIFO</abbr>).
  2580. They are called with any arguments and keyword arguments passed into
  2581. <a class="reference internal" href="#unittest.addModuleCleanup" title="unittest.addModuleCleanup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addModuleCleanup()</span></code></a> when they are added.</p>
  2582. <p>If <code class="xref py py-meth docutils literal notranslate"><span class="pre">setUpModule()</span></code> fails, meaning that <code class="xref py py-func docutils literal notranslate"><span class="pre">tearDownModule()</span></code> is not
  2583. called, then any cleanup functions added will still be called.</p>
  2584. <div class="versionadded">
  2585. <p><span class="versionmodified added">New in version 3.8.</span></p>
  2586. </div>
  2587. </dd></dl>
  2588. <dl class="py method">
  2589. <dt class="sig sig-object py" id="unittest.enterModuleContext">
  2590. <em class="property"><span class="pre">classmethod</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">enterModuleContext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cm</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.enterModuleContext" title="Link to this definition">¶</a></dt>
  2591. <dd><p>Enter the supplied <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a>. If successful, also
  2592. add its <a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__exit__()</span></code></a> method as a cleanup function by
  2593. <a class="reference internal" href="#unittest.addModuleCleanup" title="unittest.addModuleCleanup"><code class="xref py py-func docutils literal notranslate"><span class="pre">addModuleCleanup()</span></code></a> and return the result of the
  2594. <a class="reference internal" href="../reference/datamodel.html#object.__enter__" title="object.__enter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__enter__()</span></code></a> method.</p>
  2595. <div class="versionadded">
  2596. <p><span class="versionmodified added">New in version 3.11.</span></p>
  2597. </div>
  2598. </dd></dl>
  2599. <dl class="py function">
  2600. <dt class="sig sig-object py" id="unittest.doModuleCleanups">
  2601. <span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">doModuleCleanups</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.doModuleCleanups" title="Link to this definition">¶</a></dt>
  2602. <dd><p>This function is called unconditionally after <code class="xref py py-func docutils literal notranslate"><span class="pre">tearDownModule()</span></code>, or
  2603. after <code class="xref py py-func docutils literal notranslate"><span class="pre">setUpModule()</span></code> if <code class="xref py py-func docutils literal notranslate"><span class="pre">setUpModule()</span></code> raises an exception.</p>
  2604. <p>It is responsible for calling all the cleanup functions added by
  2605. <a class="reference internal" href="#unittest.addModuleCleanup" title="unittest.addModuleCleanup"><code class="xref py py-func docutils literal notranslate"><span class="pre">addModuleCleanup()</span></code></a>. If you need cleanup functions to be called
  2606. <em>prior</em> to <code class="xref py py-func docutils literal notranslate"><span class="pre">tearDownModule()</span></code> then you can call
  2607. <a class="reference internal" href="#unittest.doModuleCleanups" title="unittest.doModuleCleanups"><code class="xref py py-func docutils literal notranslate"><span class="pre">doModuleCleanups()</span></code></a> yourself.</p>
  2608. <p><a class="reference internal" href="#unittest.doModuleCleanups" title="unittest.doModuleCleanups"><code class="xref py py-func docutils literal notranslate"><span class="pre">doModuleCleanups()</span></code></a> pops methods off the stack of cleanup
  2609. functions one at a time, so it can be called at any time.</p>
  2610. <div class="versionadded">
  2611. <p><span class="versionmodified added">New in version 3.8.</span></p>
  2612. </div>
  2613. </dd></dl>
  2614. </section>
  2615. </section>
  2616. <section id="signal-handling">
  2617. <h2>Signal Handling<a class="headerlink" href="#signal-handling" title="Link to this heading">¶</a></h2>
  2618. <div class="versionadded">
  2619. <p><span class="versionmodified added">New in version 3.2.</span></p>
  2620. </div>
  2621. <p>The <a class="reference internal" href="#cmdoption-unittest-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">-c/--catch</span></code></a> command-line option to unittest,
  2622. along with the <code class="docutils literal notranslate"><span class="pre">catchbreak</span></code> parameter to <a class="reference internal" href="#unittest.main" title="unittest.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">unittest.main()</span></code></a>, provide
  2623. more friendly handling of control-C during a test run. With catch break
  2624. behavior enabled control-C will allow the currently running test to complete,
  2625. and the test run will then end and report all the results so far. A second
  2626. control-c will raise a <a class="reference internal" href="exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a> in the usual way.</p>
  2627. <p>The control-c handling signal handler attempts to remain compatible with code or
  2628. tests that install their own <a class="reference internal" href="signal.html#signal.SIGINT" title="signal.SIGINT"><code class="xref py py-const docutils literal notranslate"><span class="pre">signal.SIGINT</span></code></a> handler. If the <code class="docutils literal notranslate"><span class="pre">unittest</span></code>
  2629. handler is called but <em>isn’t</em> the installed <a class="reference internal" href="signal.html#signal.SIGINT" title="signal.SIGINT"><code class="xref py py-const docutils literal notranslate"><span class="pre">signal.SIGINT</span></code></a> handler,
  2630. i.e. it has been replaced by the system under test and delegated to, then it
  2631. calls the default handler. This will normally be the expected behavior by code
  2632. that replaces an installed handler and delegates to it. For individual tests
  2633. that need <code class="docutils literal notranslate"><span class="pre">unittest</span></code> control-c handling disabled the <a class="reference internal" href="#unittest.removeHandler" title="unittest.removeHandler"><code class="xref py py-func docutils literal notranslate"><span class="pre">removeHandler()</span></code></a>
  2634. decorator can be used.</p>
  2635. <p>There are a few utility functions for framework authors to enable control-c
  2636. handling functionality within test frameworks.</p>
  2637. <dl class="py function">
  2638. <dt class="sig sig-object py" id="unittest.installHandler">
  2639. <span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">installHandler</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#unittest.installHandler" title="Link to this definition">¶</a></dt>
  2640. <dd><p>Install the control-c handler. When a <a class="reference internal" href="signal.html#signal.SIGINT" title="signal.SIGINT"><code class="xref py py-const docutils literal notranslate"><span class="pre">signal.SIGINT</span></code></a> is received
  2641. (usually in response to the user pressing control-c) all registered results
  2642. have <a class="reference internal" href="#unittest.TestResult.stop" title="unittest.TestResult.stop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">stop()</span></code></a> called.</p>
  2643. </dd></dl>
  2644. <dl class="py function">
  2645. <dt class="sig sig-object py" id="unittest.registerResult">
  2646. <span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">registerResult</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">result</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.registerResult" title="Link to this definition">¶</a></dt>
  2647. <dd><p>Register a <a class="reference internal" href="#unittest.TestResult" title="unittest.TestResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResult</span></code></a> object for control-c handling. Registering a
  2648. result stores a weak reference to it, so it doesn’t prevent the result from
  2649. being garbage collected.</p>
  2650. <p>Registering a <a class="reference internal" href="#unittest.TestResult" title="unittest.TestResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResult</span></code></a> object has no side-effects if control-c
  2651. handling is not enabled, so test frameworks can unconditionally register
  2652. all results they create independently of whether or not handling is enabled.</p>
  2653. </dd></dl>
  2654. <dl class="py function">
  2655. <dt class="sig sig-object py" id="unittest.removeResult">
  2656. <span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">removeResult</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">result</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.removeResult" title="Link to this definition">¶</a></dt>
  2657. <dd><p>Remove a registered result. Once a result has been removed then
  2658. <a class="reference internal" href="#unittest.TestResult.stop" title="unittest.TestResult.stop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">stop()</span></code></a> will no longer be called on that result object in
  2659. response to a control-c.</p>
  2660. </dd></dl>
  2661. <dl class="py function">
  2662. <dt class="sig sig-object py" id="unittest.removeHandler">
  2663. <span class="sig-prename descclassname"><span class="pre">unittest.</span></span><span class="sig-name descname"><span class="pre">removeHandler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">function</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#unittest.removeHandler" title="Link to this definition">¶</a></dt>
  2664. <dd><p>When called without arguments this function removes the control-c handler
  2665. if it has been installed. This function can also be used as a test decorator
  2666. to temporarily remove the handler while the test is being executed:</p>
  2667. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@unittest</span><span class="o">.</span><span class="n">removeHandler</span>
  2668. <span class="k">def</span> <span class="nf">test_signal_handling</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  2669. <span class="o">...</span>
  2670. </pre></div>
  2671. </div>
  2672. </dd></dl>
  2673. </section>
  2674. </section>
  2675. <div class="clearer"></div>
  2676. </div>
  2677. </div>
  2678. </div>
  2679. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  2680. <div class="sphinxsidebarwrapper">
  2681. <div>
  2682. <h3><a href="../contents.html">Table of Contents</a></h3>
  2683. <ul>
  2684. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code> — Unit testing framework</a><ul>
  2685. <li><a class="reference internal" href="#basic-example">Basic example</a></li>
  2686. <li><a class="reference internal" href="#command-line-interface">Command-Line Interface</a><ul>
  2687. <li><a class="reference internal" href="#command-line-options">Command-line options</a></li>
  2688. </ul>
  2689. </li>
  2690. <li><a class="reference internal" href="#test-discovery">Test Discovery</a></li>
  2691. <li><a class="reference internal" href="#organizing-test-code">Organizing test code</a></li>
  2692. <li><a class="reference internal" href="#re-using-old-test-code">Re-using old test code</a></li>
  2693. <li><a class="reference internal" href="#skipping-tests-and-expected-failures">Skipping tests and expected failures</a></li>
  2694. <li><a class="reference internal" href="#distinguishing-test-iterations-using-subtests">Distinguishing test iterations using subtests</a></li>
  2695. <li><a class="reference internal" href="#classes-and-functions">Classes and functions</a><ul>
  2696. <li><a class="reference internal" href="#test-cases">Test cases</a></li>
  2697. <li><a class="reference internal" href="#grouping-tests">Grouping tests</a></li>
  2698. <li><a class="reference internal" href="#loading-and-running-tests">Loading and running tests</a><ul>
  2699. <li><a class="reference internal" href="#load-tests-protocol">load_tests Protocol</a></li>
  2700. </ul>
  2701. </li>
  2702. </ul>
  2703. </li>
  2704. <li><a class="reference internal" href="#class-and-module-fixtures">Class and Module Fixtures</a><ul>
  2705. <li><a class="reference internal" href="#setupclass-and-teardownclass">setUpClass and tearDownClass</a></li>
  2706. <li><a class="reference internal" href="#setupmodule-and-teardownmodule">setUpModule and tearDownModule</a></li>
  2707. </ul>
  2708. </li>
  2709. <li><a class="reference internal" href="#signal-handling">Signal Handling</a></li>
  2710. </ul>
  2711. </li>
  2712. </ul>
  2713. </div>
  2714. <div>
  2715. <h4>Previous topic</h4>
  2716. <p class="topless"><a href="doctest.html"
  2717. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> — Test interactive Python examples</a></p>
  2718. </div>
  2719. <div>
  2720. <h4>Next topic</h4>
  2721. <p class="topless"><a href="unittest.mock.html"
  2722. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest.mock</span></code> — mock object library</a></p>
  2723. </div>
  2724. <div role="note" aria-label="source link">
  2725. <h3>This Page</h3>
  2726. <ul class="this-page-menu">
  2727. <li><a href="../bugs.html">Report a Bug</a></li>
  2728. <li>
  2729. <a href="https://github.com/python/cpython/blob/main/Doc/library/unittest.rst"
  2730. rel="nofollow">Show Source
  2731. </a>
  2732. </li>
  2733. </ul>
  2734. </div>
  2735. </div>
  2736. <div id="sidebarbutton" title="Collapse sidebar">
  2737. <span>«</span>
  2738. </div>
  2739. </div>
  2740. <div class="clearer"></div>
  2741. </div>
  2742. <div class="related" role="navigation" aria-label="related navigation">
  2743. <h3>Navigation</h3>
  2744. <ul>
  2745. <li class="right" style="margin-right: 10px">
  2746. <a href="../genindex.html" title="General Index"
  2747. >index</a></li>
  2748. <li class="right" >
  2749. <a href="../py-modindex.html" title="Python Module Index"
  2750. >modules</a> |</li>
  2751. <li class="right" >
  2752. <a href="unittest.mock.html" title="unittest.mock — mock object library"
  2753. >next</a> |</li>
  2754. <li class="right" >
  2755. <a href="doctest.html" title="doctest — Test interactive Python examples"
  2756. >previous</a> |</li>
  2757. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  2758. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  2759. <li class="switchers">
  2760. <div class="language_switcher_placeholder"></div>
  2761. <div class="version_switcher_placeholder"></div>
  2762. </li>
  2763. <li>
  2764. </li>
  2765. <li id="cpython-language-and-version">
  2766. <a href="../index.html">3.12.3 Documentation</a> &#187;
  2767. </li>
  2768. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  2769. <li class="nav-item nav-item-2"><a href="development.html" >Development Tools</a> &#187;</li>
  2770. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code> — Unit testing framework</a></li>
  2771. <li class="right">
  2772. <div class="inline-search" role="search">
  2773. <form class="inline-search" action="../search.html" method="get">
  2774. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  2775. <input type="submit" value="Go" />
  2776. </form>
  2777. </div>
  2778. |
  2779. </li>
  2780. <li class="right">
  2781. <label class="theme-selector-label">
  2782. Theme
  2783. <select class="theme-selector" oninput="activateTheme(this.value)">
  2784. <option value="auto" selected>Auto</option>
  2785. <option value="light">Light</option>
  2786. <option value="dark">Dark</option>
  2787. </select>
  2788. </label> |</li>
  2789. </ul>
  2790. </div>
  2791. <div class="footer">
  2792. &copy;
  2793. <a href="../copyright.html">
  2794. Copyright
  2795. </a>
  2796. 2001-2024, Python Software Foundation.
  2797. <br />
  2798. This page is licensed under the Python Software Foundation License Version 2.
  2799. <br />
  2800. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  2801. <br />
  2802. See <a href="/license.html">History and License</a> for more information.<br />
  2803. <br />
  2804. The Python Software Foundation is a non-profit corporation.
  2805. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  2806. <br />
  2807. <br />
  2808. Last updated on Apr 09, 2024 (13:47 UTC).
  2809. <a href="/bugs.html">Found a bug</a>?
  2810. <br />
  2811. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  2812. </div>
  2813. </body>
  2814. </html>
上海开阖软件有限公司 沪ICP备12045867号-1