Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > e870e6598e1c7e3918555a3d0ba5f3d4 > files > 786

python3-docs-3.1.1-2mdv2010.0.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>25.3. unittest — Unit testing framework &mdash; Python v3.1.1 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.1.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v3.1.1 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v3.1.1 documentation" href="../index.html" />
    <link rel="up" title="25. Development Tools" href="development.html" />
    <link rel="next" title="25.4. 2to3 - Automated Python 2 to 3 code translation" href="2to3.html" />
    <link rel="prev" title="25.2. doctest — Test interactive Python examples" href="doctest.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="2to3.html" title="25.4. 2to3 - Automated Python 2 to 3 code translation"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="doctest.html" title="25.2. doctest — Test interactive Python examples"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.1.1 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="development.html" accesskey="U">25. Development Tools</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-unittest">
<h1>25.3. <tt class="xref docutils literal"><span class="pre">unittest</span></tt> &#8212; Unit testing framework<a class="headerlink" href="#module-unittest" title="Permalink to this headline">¶</a></h1>
<p>
<span class="versionmodified">Changed in version 3.1: </span>Added test <a class="reference internal" href="#unittest-skipping"><em>skipping and expected failures</em></a>.</p>
<p>The Python unit testing framework, sometimes referred to as &#8220;PyUnit,&#8221; is a
Python language version of JUnit, by Kent Beck and Erich Gamma. JUnit is, in
turn, a Java version of Kent&#8217;s Smalltalk testing framework.  Each is the de
facto standard unit testing framework for its respective language.</p>
<p><tt class="xref docutils literal"><span class="pre">unittest</span></tt> supports test automation, sharing of setup and shutdown code for
tests, aggregation of tests into collections, and independence of the tests from
the reporting framework.  The <tt class="xref docutils literal"><span class="pre">unittest</span></tt> module provides classes that make
it easy to support these qualities for a set of tests.</p>
<p>To achieve this, <tt class="xref docutils literal"><span class="pre">unittest</span></tt> supports some important concepts:</p>
<dl class="docutils">
<dt>test fixture</dt>
<dd>A <em>test fixture</em> represents the preparation needed to perform one or more
tests, and any associate cleanup actions.  This may involve, for example,
creating temporary or proxy databases, directories, or starting a server
process.</dd>
<dt>test case</dt>
<dd>A <em>test case</em> is the smallest unit of testing.  It checks for a specific
response to a particular set of inputs.  <tt class="xref docutils literal"><span class="pre">unittest</span></tt> provides a base class,
<a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>, which may be used to create new test cases.</dd>
<dt>test suite</dt>
<dd>A <em>test suite</em> is a collection of test cases, test suites, or both.  It is
used to aggregate tests that should be executed together.</dd>
<dt>test runner</dt>
<dd>A <em>test runner</em> is a component which orchestrates the execution of tests
and provides the outcome to the user.  The runner may use a graphical interface,
a textual interface, or return a special value to indicate the results of
executing the tests.</dd>
</dl>
<p>The test case and test fixture concepts are supported through the
<a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> and <a title="unittest.FunctionTestCase" class="reference internal" href="#unittest.FunctionTestCase"><tt class="xref docutils literal"><span class="pre">FunctionTestCase</span></tt></a> classes; the former should be
used when creating new tests, and the latter can be used when integrating
existing test code with a <tt class="xref docutils literal"><span class="pre">unittest</span></tt>-driven framework. When building test
fixtures using <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>, the <a title="unittest.TestCase.setUp" class="reference internal" href="#unittest.TestCase.setUp"><tt class="xref docutils literal"><span class="pre">setUp()</span></tt></a> and
<a title="unittest.TestCase.tearDown" class="reference internal" href="#unittest.TestCase.tearDown"><tt class="xref docutils literal"><span class="pre">tearDown()</span></tt></a> methods can be overridden to provide initialization
and cleanup for the fixture.  With <a title="unittest.FunctionTestCase" class="reference internal" href="#unittest.FunctionTestCase"><tt class="xref docutils literal"><span class="pre">FunctionTestCase</span></tt></a>, existing functions
can be passed to the constructor for these purposes.  When the test is run, the
fixture initialization is run first; if it succeeds, the cleanup method is run
after the test has been executed, regardless of the outcome of the test.  Each
instance of the <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> will only be used to run a single test method,
so a new fixture is created for each test.</p>
<p>Test suites are implemented by the <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> class.  This class allows
individual tests and test suites to be aggregated; when the suite is executed,
all tests added directly to the suite and in &#8220;child&#8221; test suites are run.</p>
<p>A test runner is an object that provides a single method,
<tt class="xref docutils literal"><span class="pre">run()</span></tt>, which accepts a <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> or <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a>
object as a parameter, and returns a result object.  The class
<a title="unittest.TestResult" class="reference internal" href="#unittest.TestResult"><tt class="xref docutils literal"><span class="pre">TestResult</span></tt></a> is provided for use as the result object. <tt class="xref docutils literal"><span class="pre">unittest</span></tt>
provides the <a title="unittest.TextTestRunner" class="reference internal" href="#unittest.TextTestRunner"><tt class="xref docutils literal"><span class="pre">TextTestRunner</span></tt></a> as an example test runner which reports
test results on the standard error stream by default.  Alternate runners can be
implemented for other environments (such as graphical environments) without any
need to derive from a specific class.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt>Module <a title="Test pieces of code within docstrings." class="reference external" href="doctest.html#module-doctest"><tt class="xref docutils literal"><span class="pre">doctest</span></tt></a></dt>
<dd>Another test-support module with a very different flavor.</dd>
<dt><a class="reference external" href="http://www.XProgramming.com/testfram.htm">Simple Smalltalk Testing: With Patterns</a></dt>
<dd>Kent Beck&#8217;s original paper on testing frameworks using the pattern shared by
<tt class="xref docutils literal"><span class="pre">unittest</span></tt>.</dd>
<dt><a class="reference external" href="http://code.google.com/p/python-nose/">Nose</a> and <a class="reference external" href="http://pytest.org">py.test</a></dt>
<dd>Third-party unittest frameworks with a lighter-weight syntax
for writing tests.  For example, <tt class="docutils literal"><span class="pre">assert</span> <span class="pre">func(10)</span> <span class="pre">==</span> <span class="pre">42</span></tt>.</dd>
<dt><a class="reference external" href="http://python-mock.sourceforge.net/">python-mock</a> and <a class="reference external" href="http://blog.ianbicking.org/minimock.html">minimock</a></dt>
<dd>Tools for creating mock test objects (objects simulating external resources).</dd>
</dl>
</div>
<div class="section" id="basic-example">
<span id="unittest-minimal-example"></span><h2>25.3.1. Basic example<a class="headerlink" href="#basic-example" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="xref docutils literal"><span class="pre">unittest</span></tt> module provides a rich set of tools for constructing and
running tests.  This section demonstrates that a small subset of the tools
suffice to meet the needs of most users.</p>
<p>Here is a short script to test three functions from the <a title="Generate pseudo-random numbers with various common distributions." class="reference external" href="random.html#module-random"><tt class="xref docutils literal"><span class="pre">random</span></tt></a> module:</p>
<div class="highlight-python3"><pre>import random
import unittest

class TestSequenceFunctions(unittest.TestCase):

    def setUp(self):
        self.seq = list(range(10))

    def test_shuffle(self):
        # make sure the shuffled sequence does not lose any elements
        random.shuffle(self.seq)
        self.seq.sort()
        self.assertEqual(self.seq, list(range(10)))

    def test_choice(self):
        element = random.choice(self.seq)
        self.assert_(element in self.seq)

    def test_sample(self):
        self.assertRaises(ValueError, random.sample, self.seq, 20)
        for element in random.sample(self.seq, 5):
            self.assert_(element in self.seq)

if __name__ == '__main__':
    unittest.main()</pre>
</div>
<p>A testcase is created by subclassing <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">unittest.TestCase</span></tt></a>.  The three
individual tests are defined with methods whose names start with the letters
<tt class="docutils literal"><span class="pre">test</span></tt>.  This naming convention informs the test runner about which methods
represent tests.</p>
<p>The crux of each test is a call to <a title="unittest.TestCase.assertEqual" class="reference internal" href="#unittest.TestCase.assertEqual"><tt class="xref docutils literal"><span class="pre">assertEqual()</span></tt></a> to check for an
expected result; <a title="unittest.TestCase.assert_" class="reference internal" href="#unittest.TestCase.assert_"><tt class="xref docutils literal"><span class="pre">assert_()</span></tt></a> to verify a condition; or
<a title="unittest.TestCase.assertRaises" class="reference internal" href="#unittest.TestCase.assertRaises"><tt class="xref docutils literal"><span class="pre">assertRaises()</span></tt></a> to verify that an expected exception gets raised.
These methods are used instead of the <a class="reference external" href="../reference/simple_stmts.html#assert"><tt class="xref docutils literal"><span class="pre">assert</span></tt></a> statement so the test
runner can accumulate all test results and produce a report.</p>
<p>When a <a title="unittest.TestCase.setUp" class="reference internal" href="#unittest.TestCase.setUp"><tt class="xref docutils literal"><span class="pre">setUp()</span></tt></a> method is defined, the test runner will run that
method prior to each test.  Likewise, if a <a title="unittest.TestCase.tearDown" class="reference internal" href="#unittest.TestCase.tearDown"><tt class="xref docutils literal"><span class="pre">tearDown()</span></tt></a> method is
defined, the test runner will invoke that method after each test.  In the
example, <a title="unittest.TestCase.setUp" class="reference internal" href="#unittest.TestCase.setUp"><tt class="xref docutils literal"><span class="pre">setUp()</span></tt></a> was used to create a fresh sequence for each
test.</p>
<p>The final block shows a simple way to run the tests. <a title="unittest.main" class="reference internal" href="#unittest.main"><tt class="xref docutils literal"><span class="pre">unittest.main()</span></tt></a>
provides a command line interface to the test script.  When run from the command
line, the above script produces an output that looks like this:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="o">...</span>
<span class="o">----------------------------------------------------------------------</span>
<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>

<span class="n">OK</span>
</pre></div>
</div>
<p>Instead of <a title="unittest.main" class="reference internal" href="#unittest.main"><tt class="xref docutils literal"><span class="pre">unittest.main()</span></tt></a>, there are other ways to run the tests with a
finer level of control, less terse output, and no requirement to be run from the
command line.  For example, the last two lines may be replaced with:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">suite</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestLoader</span><span class="p">()</span><span class="o">.</span><span class="n">loadTestsFromTestCase</span><span class="p">(</span><span class="n">TestSequenceFunctions</span><span class="p">)</span>
<span class="n">unittest</span><span class="o">.</span><span class="n">TextTestRunner</span><span class="p">(</span><span class="n">verbosity</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">suite</span><span class="p">)</span>
</pre></div>
</div>
<p>Running the revised script from the interpreter or another script produces the
following output:</p>
<div class="highlight-python3"><pre>testchoice (__main__.TestSequenceFunctions) ... ok
testsample (__main__.TestSequenceFunctions) ... ok
testshuffle (__main__.TestSequenceFunctions) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.110s

OK</pre>
</div>
<p>The above examples show the most commonly used <tt class="xref docutils literal"><span class="pre">unittest</span></tt> features which
are sufficient to meet many everyday testing needs.  The remainder of the
documentation explores the full feature set from first principles.</p>
</div>
<div class="section" id="organizing-test-code">
<span id="organizing-tests"></span><h2>25.3.2. Organizing test code<a class="headerlink" href="#organizing-test-code" title="Permalink to this headline">¶</a></h2>
<p>The basic building blocks of unit testing are <em>test cases</em> &#8212; single
scenarios that must be set up and checked for correctness.  In <tt class="xref docutils literal"><span class="pre">unittest</span></tt>,
test cases are represented by instances of <tt class="xref docutils literal"><span class="pre">unittest</span></tt>&#8216;s <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>
class. To make your own test cases you must write subclasses of
<a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>, or use <a title="unittest.FunctionTestCase" class="reference internal" href="#unittest.FunctionTestCase"><tt class="xref docutils literal"><span class="pre">FunctionTestCase</span></tt></a>.</p>
<p>An instance of a <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>-derived class is an object that can
completely run a single test method, together with optional set-up and tidy-up
code.</p>
<p>The testing code of a <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instance should be entirely self
contained, such that it can be run either in isolation or in arbitrary
combination with any number of other test cases.</p>
<p>The simplest <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> subclass will simply override the
<tt class="xref docutils literal"><span class="pre">runTest()</span></tt> method in order to perform specific testing code:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">unittest</span>

<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>
    <span class="k">def</span> <span class="nf">runTest</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">widget</span> <span class="o">=</span> <span class="n">Widget</span><span class="p">(</span><span class="s">&#39;The widget&#39;</span><span class="p">)</span>
        <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> <span class="s">&#39;incorrect default size&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that in order to test something, we use the one of the <tt class="xref docutils literal"><span class="pre">assert*()</span></tt>
methods provided by the <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> base class.  If the
test fails, an exception will be raised, and <tt class="xref docutils literal"><span class="pre">unittest</span></tt> will identify the
test case as a <em>failure</em>.  Any other exceptions will be treated as
<em>errors</em>. This helps you identify where the problem is: <em>failures</em> are
caused by incorrect results - a 5 where you expected a 6. <em>Errors</em> are
caused by incorrect code - e.g., a <a title="exceptions.TypeError" class="reference external" href="exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></a> caused by an incorrect
function call.</p>
<p>The way to run a test case will be described later.  For now, note that to
construct an instance of such a test case, we call its constructor without
arguments:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">testCase</span> <span class="o">=</span> <span class="n">DefaultWidgetSizeTestCase</span><span class="p">()</span>
</pre></div>
</div>
<p>Now, such test cases can be numerous, and their set-up can be repetitive.  In
the above case, constructing a <tt class="xref docutils literal"><span class="pre">Widget</span></tt> in each of 100 Widget test case
subclasses would mean unsightly duplication.</p>
<p>Luckily, we can factor out such set-up code by implementing a method called
<a title="unittest.TestCase.setUp" class="reference internal" href="#unittest.TestCase.setUp"><tt class="xref docutils literal"><span class="pre">setUp()</span></tt></a>, which the testing framework will automatically call for
us when we run the test:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">unittest</span>

<span class="k">class</span> <span class="nc">SimpleWidgetTestCase</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</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">Widget</span><span class="p">(</span><span class="s">&#39;The widget&#39;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">DefaultWidgetSizeTestCase</span><span class="p">(</span><span class="n">SimpleWidgetTestCase</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">runTest</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</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="o">==</span> <span class="p">(</span><span class="mi">50</span><span class="p">,</span><span class="mi">50</span><span class="p">),</span>
                        <span class="s">&#39;incorrect default size&#39;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">WidgetResizeTestCase</span><span class="p">(</span><span class="n">SimpleWidgetTestCase</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">runTest</span><span class="p">(</span><span class="bp">self</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">resize</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span><span class="mi">150</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</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="o">==</span> <span class="p">(</span><span class="mi">100</span><span class="p">,</span><span class="mi">150</span><span class="p">),</span>
                        <span class="s">&#39;wrong size after resize&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>If the <a title="unittest.TestCase.setUp" class="reference internal" href="#unittest.TestCase.setUp"><tt class="xref docutils literal"><span class="pre">setUp()</span></tt></a> method raises an exception while the test is
running, the framework will consider the test to have suffered an error, and the
<tt class="xref docutils literal"><span class="pre">runTest()</span></tt> method will not be executed.</p>
<p>Similarly, we can provide a <a title="unittest.TestCase.tearDown" class="reference internal" href="#unittest.TestCase.tearDown"><tt class="xref docutils literal"><span class="pre">tearDown()</span></tt></a> method that tidies up
after the <tt class="xref docutils literal"><span class="pre">runTest()</span></tt> method has been run:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">unittest</span>

<span class="k">class</span> <span class="nc">SimpleWidgetTestCase</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</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">Widget</span><span class="p">(</span><span class="s">&#39;The widget&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tearDown</span><span class="p">(</span><span class="bp">self</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">dispose</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="k">None</span>
</pre></div>
</div>
<p>If <a title="unittest.TestCase.setUp" class="reference internal" href="#unittest.TestCase.setUp"><tt class="xref docutils literal"><span class="pre">setUp()</span></tt></a> succeeded, the <a title="unittest.TestCase.tearDown" class="reference internal" href="#unittest.TestCase.tearDown"><tt class="xref docutils literal"><span class="pre">tearDown()</span></tt></a> method will
be run whether <tt class="xref docutils literal"><span class="pre">runTest()</span></tt> succeeded or not.</p>
<p>Such a working environment for the testing code is called a <em>fixture</em>.</p>
<p>Often, many small test cases will use the same fixture.  In this case, we would
end up subclassing <tt class="xref docutils literal"><span class="pre">SimpleWidgetTestCase</span></tt> into many small one-method
classes such as <tt class="xref docutils literal"><span class="pre">DefaultWidgetSizeTestCase</span></tt>.  This is time-consuming and
discouraging, so in the same vein as JUnit, <tt class="xref docutils literal"><span class="pre">unittest</span></tt> provides a simpler
mechanism:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">unittest</span>

<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>
    <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</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">Widget</span><span class="p">(</span><span class="s">&#39;The widget&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tearDown</span><span class="p">(</span><span class="bp">self</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">dispose</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="k">None</span>

    <span class="k">def</span> <span class="nf">testDefaultSize</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</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="o">==</span> <span class="p">(</span><span class="mi">50</span><span class="p">,</span><span class="mi">50</span><span class="p">),</span>
                        <span class="s">&#39;incorrect default size&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">testResize</span><span class="p">(</span><span class="bp">self</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">resize</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span><span class="mi">150</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</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="o">==</span> <span class="p">(</span><span class="mi">100</span><span class="p">,</span><span class="mi">150</span><span class="p">),</span>
                        <span class="s">&#39;wrong size after resize&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Here we have not provided a <tt class="xref docutils literal"><span class="pre">runTest()</span></tt> method, but have instead
provided two different test methods.  Class instances will now each run one of
the <tt class="xref docutils literal"><span class="pre">test*()</span></tt> methods, with <tt class="docutils literal"><span class="pre">self.widget</span></tt> created and destroyed
separately for each instance.  When creating an instance we must specify the
test method it is to run.  We do this by passing the method name in the
constructor:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">defaultSizeTestCase</span> <span class="o">=</span> <span class="n">WidgetTestCase</span><span class="p">(</span><span class="s">&#39;testDefaultSize&#39;</span><span class="p">)</span>
<span class="n">resizeTestCase</span> <span class="o">=</span> <span class="n">WidgetTestCase</span><span class="p">(</span><span class="s">&#39;testResize&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Test case instances are grouped together according to the features they test.
<tt class="xref docutils literal"><span class="pre">unittest</span></tt> provides a mechanism for this: the <em>test suite</em>,
represented by <tt class="xref docutils literal"><span class="pre">unittest</span></tt>&#8216;s <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> class:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">widgetTestSuite</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestSuite</span><span class="p">()</span>
<span class="n">widgetTestSuite</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="s">&#39;testDefaultSize&#39;</span><span class="p">))</span>
<span class="n">widgetTestSuite</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="s">&#39;testResize&#39;</span><span class="p">))</span>
</pre></div>
</div>
<p>For the ease of running tests, as we will see later, it is a good idea to
provide in each test module a callable object that returns a pre-built test
suite:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">def</span> <span class="nf">suite</span><span class="p">():</span>
    <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>
    <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="s">&#39;testDefaultSize&#39;</span><span class="p">))</span>
    <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="s">&#39;testResize&#39;</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">suite</span>
</pre></div>
</div>
<p>or even:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">def</span> <span class="nf">suite</span><span class="p">():</span>
    <span class="n">tests</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;testDefaultSize&#39;</span><span class="p">,</span> <span class="s">&#39;testResize&#39;</span><span class="p">]</span>

    <span class="k">return</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestSuite</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">WidgetTestCase</span><span class="p">,</span> <span class="n">tests</span><span class="p">))</span>
</pre></div>
</div>
<p>Since it is a common pattern to create a <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> subclass with many
similarly named test functions, <tt class="xref docutils literal"><span class="pre">unittest</span></tt> provides a <a title="unittest.TestLoader" class="reference internal" href="#unittest.TestLoader"><tt class="xref docutils literal"><span class="pre">TestLoader</span></tt></a>
class that can be used to automate the process of creating a test suite and
populating it with individual tests. For example,</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">suite</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestLoader</span><span class="p">()</span><span class="o">.</span><span class="n">loadTestsFromTestCase</span><span class="p">(</span><span class="n">WidgetTestCase</span><span class="p">)</span>
</pre></div>
</div>
<p>will create a test suite that will run <tt class="docutils literal"><span class="pre">WidgetTestCase.testDefaultSize()</span></tt> and
<tt class="docutils literal"><span class="pre">WidgetTestCase.testResize</span></tt>. <a title="unittest.TestLoader" class="reference internal" href="#unittest.TestLoader"><tt class="xref docutils literal"><span class="pre">TestLoader</span></tt></a> uses the <tt class="docutils literal"><span class="pre">'test'</span></tt> method
name prefix to identify test methods automatically.</p>
<p>Note that the order in which the various test cases will be run is
determined by sorting the test function names with respect to the
built-in ordering for strings.</p>
<p>Often it is desirable to group suites of test cases together, so as to run tests
for the whole system at once.  This is easy, since <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> instances
can be added to a <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> just as <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instances can be
added to a <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">suite1</span> <span class="o">=</span> <span class="n">module1</span><span class="o">.</span><span class="n">TheTestSuite</span><span class="p">()</span>
<span class="n">suite2</span> <span class="o">=</span> <span class="n">module2</span><span class="o">.</span><span class="n">TheTestSuite</span><span class="p">()</span>
<span class="n">alltests</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestSuite</span><span class="p">([</span><span class="n">suite1</span><span class="p">,</span> <span class="n">suite2</span><span class="p">])</span>
</pre></div>
</div>
<p>You can place the definitions of test cases and test suites in the same modules
as the code they are to test (such as <tt class="docutils literal"><span class="pre">widget.py</span></tt>), but there are several
advantages to placing the test code in a separate module, such as
<tt class="docutils literal"><span class="pre">test_widget.py</span></tt>:</p>
<ul class="simple">
<li>The test module can be run standalone from the command line.</li>
<li>The test code can more easily be separated from shipped code.</li>
<li>There is less temptation to change test code to fit the code it tests without
a good reason.</li>
<li>Test code should be modified much less frequently than the code it tests.</li>
<li>Tested code can be refactored more easily.</li>
<li>Tests for modules written in C must be in separate modules anyway, so why not
be consistent?</li>
<li>If the testing strategy changes, there is no need to change the source code.</li>
</ul>
</div>
<div class="section" id="re-using-old-test-code">
<span id="legacy-unit-tests"></span><h2>25.3.3. Re-using old test code<a class="headerlink" href="#re-using-old-test-code" title="Permalink to this headline">¶</a></h2>
<p>Some users will find that they have existing test code that they would like to
run from <tt class="xref docutils literal"><span class="pre">unittest</span></tt>, without converting every old test function to a
<a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> subclass.</p>
<p>For this reason, <tt class="xref docutils literal"><span class="pre">unittest</span></tt> provides a <a title="unittest.FunctionTestCase" class="reference internal" href="#unittest.FunctionTestCase"><tt class="xref docutils literal"><span class="pre">FunctionTestCase</span></tt></a> class.
This subclass of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> can be used to wrap an existing test
function.  Set-up and tear-down functions can also be provided.</p>
<p>Given the following test function:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">def</span> <span class="nf">testSomething</span><span class="p">():</span>
    <span class="n">something</span> <span class="o">=</span> <span class="n">makeSomething</span><span class="p">()</span>
    <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="k">None</span>
    <span class="c"># ...</span>
</pre></div>
</div>
<p>one can create an equivalent test case instance as follows:</p>
<div class="highlight-python3"><div class="highlight"><pre><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>
</pre></div>
</div>
<p>If there are additional set-up and tear-down methods that should be called as
part of the test case&#8217;s operation, they can also be provided like so:</p>
<div class="highlight-python3"><div class="highlight"><pre><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>
                                     <span class="n">setUp</span><span class="o">=</span><span class="n">makeSomethingDB</span><span class="p">,</span>
                                     <span class="n">tearDown</span><span class="o">=</span><span class="n">deleteSomethingDB</span><span class="p">)</span>
</pre></div>
</div>
<p>To make migrating existing test suites easier, <tt class="xref docutils literal"><span class="pre">unittest</span></tt> supports tests
raising <a title="exceptions.AssertionError" class="reference external" href="exceptions.html#exceptions.AssertionError"><tt class="xref docutils literal"><span class="pre">AssertionError</span></tt></a> to indicate test failure. However, it is
recommended that you use the explicit <tt class="xref docutils literal"><span class="pre">TestCase.fail*()</span></tt> and
<tt class="xref docutils literal"><span class="pre">TestCase.assert*()</span></tt> methods instead, as future versions of <tt class="xref docutils literal"><span class="pre">unittest</span></tt>
may treat <a title="exceptions.AssertionError" class="reference external" href="exceptions.html#exceptions.AssertionError"><tt class="xref docutils literal"><span class="pre">AssertionError</span></tt></a> differently.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Even though <a title="unittest.FunctionTestCase" class="reference internal" href="#unittest.FunctionTestCase"><tt class="xref docutils literal"><span class="pre">FunctionTestCase</span></tt></a> can be used to quickly convert an existing
test base over to a <tt class="xref docutils literal"><span class="pre">unittest</span></tt>-based system, this approach is not
recommended.  Taking the time to set up proper <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> subclasses will
make future test refactorings infinitely easier.</p>
</div>
<p>In some cases, the existing tests may have been written using the <a title="Test pieces of code within docstrings." class="reference external" href="doctest.html#module-doctest"><tt class="xref docutils literal"><span class="pre">doctest</span></tt></a>
module.  If so, <a title="Test pieces of code within docstrings." class="reference external" href="doctest.html#module-doctest"><tt class="xref docutils literal"><span class="pre">doctest</span></tt></a> provides a <tt class="xref docutils literal"><span class="pre">DocTestSuite</span></tt> class that can
automatically build <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">unittest.TestSuite</span></tt></a> instances from the existing
<a title="Test pieces of code within docstrings." class="reference external" href="doctest.html#module-doctest"><tt class="xref docutils literal"><span class="pre">doctest</span></tt></a>-based tests.</p>
</div>
<div class="section" id="skipping-tests-and-expected-failures">
<span id="unittest-skipping"></span><h2>25.3.4. Skipping tests and expected failures<a class="headerlink" href="#skipping-tests-and-expected-failures" title="Permalink to this headline">¶</a></h2>
<p>Unittest supports skipping individual test methods and even whole classes of
tests.  In addition, it supports marking a test as a &#8220;expected failure,&#8221; a test
that is broken and will fail, but shouldn&#8217;t be counted as a failure on a
<a title="unittest.TestResult" class="reference internal" href="#unittest.TestResult"><tt class="xref docutils literal"><span class="pre">TestResult</span></tt></a>.</p>
<p>Skipping a test is simply a matter of using the <a title="unittest.skip" class="reference internal" href="#unittest.skip"><tt class="xref docutils literal"><span class="pre">skip()</span></tt></a> <a class="reference external" href="../glossary.html#term-decorator"><em class="xref">decorator</em></a>
or one of its conditional variants.</p>
<p>Basic skipping looks like this:</p>
<div class="highlight-python3"><pre>class MyTestCase(unittest.TestCase):

    @unittest.skip("demonstrating skipping")
    def test_nothing(self):
        self.fail("shouldn't happen")

    @unittest.skipIf(mylib.__version__ &lt; (1, 3), "not supported in this library version")
    def test_format(self):
        # Tests that work for only a certain version of the library.
        pass

    @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
    def test_windows_support(self):
        # windows specific testing code
        pass</pre>
</div>
<p>This is the output of running the example above in verbose mode:</p>
<div class="highlight-python3"><pre>test_format (__main__.MyTestCase) ... skipped 'not supported in this library version'
test_nothing (__main__.MyTestCase) ... skipped 'demonstrating skipping'
test_windows_support (__main__.MyTestCase) ... skipped 'requires Windows'

----------------------------------------------------------------------
Ran 3 tests in 0.005s

OK (skipped=3)</pre>
</div>
<p>Classes can be skipped just like methods:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="nd">@skip</span><span class="p">(</span><span class="s">&quot;showing class skipping&quot;</span><span class="p">)</span>
<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>
    <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>
        <span class="k">pass</span>
</pre></div>
</div>
<p><a title="unittest.TestCase.setUp" class="reference internal" href="#unittest.TestCase.setUp"><tt class="xref docutils literal"><span class="pre">TestCase.setUp()</span></tt></a> can also skip the test.  This is useful when a resource
that needs to be set up is not available.</p>
<p>Expected failures use the <a title="unittest.expectedFailure" class="reference internal" href="#unittest.expectedFailure"><tt class="xref docutils literal"><span class="pre">expectedFailure()</span></tt></a> decorator.</p>
<div class="highlight-python3"><div class="highlight"><pre><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>
    <span class="nd">@unittest</span><span class="o">.</span><span class="n">expectedFailure</span>
    <span class="k">def</span> <span class="nf">test_fail</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <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="s">&quot;broken&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>It&#8217;s easy to roll your own skipping decorators by making a decorator that calls
<a title="unittest.skip" class="reference internal" href="#unittest.skip"><tt class="xref docutils literal"><span class="pre">skip()</span></tt></a> on the test when it wants it to be skipped.  This decorator skips
the test unless the passed object has a certain attribute:</p>
<div class="highlight-python3"><div class="highlight"><pre><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>
    <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>
        <span class="k">return</span> <span class="k">lambda</span> <span class="n">func</span><span class="p">:</span> <span class="n">func</span>
    <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="s">&quot;{0!r} doesn&#39;t have {1!r}&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>
</pre></div>
</div>
<p>The following decorators implement test skipping and expected failures:</p>
<dl class="function">
<dt id="unittest.skip">
<tt class="descclassname">unittest.</tt><tt class="descname">skip</tt><big>(</big><em>reason</em><big>)</big><a class="headerlink" href="#unittest.skip" title="Permalink to this definition">¶</a></dt>
<dd>Unconditionally skip the decorated test.  <em>reason</em> should describe why the
test is being skipped.</dd></dl>

<dl class="function">
<dt id="unittest.skipIf">
<tt class="descclassname">unittest.</tt><tt class="descname">skipIf</tt><big>(</big><em>condition</em>, <em>reason</em><big>)</big><a class="headerlink" href="#unittest.skipIf" title="Permalink to this definition">¶</a></dt>
<dd>Skip the decorated test if <em>condition</em> is true.</dd></dl>

<dl class="function">
<dt id="unittest.skipUnless">
<tt class="descclassname">unittest.</tt><tt class="descname">skipUnless</tt><big>(</big><em>condition</em>, <em>reason</em><big>)</big><a class="headerlink" href="#unittest.skipUnless" title="Permalink to this definition">¶</a></dt>
<dd>Skip the decoratored test unless <em>condition</em> is true.</dd></dl>

<dl class="function">
<dt id="unittest.expectedFailure">
<tt class="descclassname">unittest.</tt><tt class="descname">expectedFailure</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.expectedFailure" title="Permalink to this definition">¶</a></dt>
<dd>Mark the test as an expected failure.  If the test fails when run, the test
is not counted as a failure.</dd></dl>

</div>
<div class="section" id="classes-and-functions">
<span id="unittest-contents"></span><h2>25.3.5. Classes and functions<a class="headerlink" href="#classes-and-functions" title="Permalink to this headline">¶</a></h2>
<p>This section describes in depth the API of <tt class="xref docutils literal"><span class="pre">unittest</span></tt>.</p>
<div class="section" id="test-cases">
<span id="testcase-objects"></span><h3>25.3.5.1. Test cases<a class="headerlink" href="#test-cases" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="unittest.TestCase">
<em class="property">
class </em><tt class="descclassname">unittest.</tt><tt class="descname">TestCase</tt><big>(</big><span class="optional">[</span><em>methodName</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase" title="Permalink to this definition">¶</a></dt>
<dd><p>Instances of the <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> class represent the smallest testable units
in the <tt class="xref docutils literal"><span class="pre">unittest</span></tt> universe.  This class is intended to be used as a base
class, with specific tests being implemented by concrete subclasses.  This class
implements the interface needed by the test runner to allow it to drive the
test, and methods that the test code can use to check for and report various
kinds of failure.</p>
<p>Each instance of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> will run a single test method: the method
named <em>methodName</em>.  If you remember, we had an earlier example that went
something like this:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">def</span> <span class="nf">suite</span><span class="p">():</span>
    <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>
    <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="s">&#39;testDefaultSize&#39;</span><span class="p">))</span>
    <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="s">&#39;testResize&#39;</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">suite</span>
</pre></div>
</div>
<p>Here, we create two instances of <tt class="xref docutils literal"><span class="pre">WidgetTestCase</span></tt>, each of which runs a
single test.</p>
<p><em>methodName</em> defaults to <tt class="xref docutils literal"><span class="pre">runTest()</span></tt>.</p>
<p><a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instances provide three groups of methods: one group used
to run the test, another used by the test implementation to check conditions
and report failures, and some inquiry methods allowing information about the
test itself to be gathered.</p>
<p>Methods in the first group (running the test) are:</p>
<dl class="method">
<dt id="unittest.TestCase.setUp">
<tt class="descname">setUp</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestCase.setUp" title="Permalink to this definition">¶</a></dt>
<dd>Method called to prepare the test fixture.  This is called immediately
before calling the test method; any exception raised by this method will
be considered an error rather than a test failure. The default
implementation does nothing.</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.tearDown">
<tt class="descname">tearDown</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestCase.tearDown" title="Permalink to this definition">¶</a></dt>
<dd>Method called immediately after the test method has been called and the
result recorded.  This is called even if the test method raised an
exception, so the implementation in subclasses may need to be particularly
careful about checking internal state.  Any exception raised by this
method will be considered an error rather than a test failure.  This
method will only be called if the <a title="unittest.TestCase.setUp" class="reference internal" href="#unittest.TestCase.setUp"><tt class="xref docutils literal"><span class="pre">setUp()</span></tt></a> succeeds, regardless of
the outcome of the test method. The default implementation does nothing.</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.run">
<tt class="descname">run</tt><big>(</big><span class="optional">[</span><em>result</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.run" title="Permalink to this definition">¶</a></dt>
<dd><p>Run the test, collecting the result into the test result object passed as
<em>result</em>.  If <em>result</em> is omitted or <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>, a temporary result
object is created (by calling the <a title="unittest.TestCase.defaultTestResult" class="reference internal" href="#unittest.TestCase.defaultTestResult"><tt class="xref docutils literal"><span class="pre">defaultTestResult()</span></tt></a> method) and
used. The result object is not returned to <a title="unittest.TestCase.run" class="reference internal" href="#unittest.TestCase.run"><tt class="xref docutils literal"><span class="pre">run()</span></tt></a>&#8216;s caller.</p>
<p>The same effect may be had by simply calling the <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>
instance.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.skipTest">
<tt class="descname">skipTest</tt><big>(</big><em>reason</em><big>)</big><a class="headerlink" href="#unittest.TestCase.skipTest" title="Permalink to this definition">¶</a></dt>
<dd>Calling this during the a test method or <a title="unittest.TestCase.setUp" class="reference internal" href="#unittest.TestCase.setUp"><tt class="xref docutils literal"><span class="pre">setUp()</span></tt></a> skips the current
test.  See <a class="reference internal" href="#unittest-skipping"><em>Skipping tests and expected failures</em></a> for more information.</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.debug">
<tt class="descname">debug</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestCase.debug" title="Permalink to this definition">¶</a></dt>
<dd>Run the test without collecting the result.  This allows exceptions raised
by the test to be propagated to the caller, and can be used to support
running tests under a debugger.</dd></dl>

<p>The test code can use any of the following methods to check for and report
failures.</p>
<dl class="method">
<dt id="unittest.TestCase.assertTrue">
<tt class="descname">assertTrue</tt><big>(</big><em>expr</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertTrue" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.assert_">
<tt class="descname">assert_</tt><big>(</big><em>expr</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assert_" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.failUnless">
<tt class="descname">failUnless</tt><big>(</big><em>expr</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.failUnless" title="Permalink to this definition">¶</a></dt>
<dd><p>Signal a test failure if <em>expr</em> is false; the explanation for the failure
will be <em>msg</em> if given, otherwise it will be <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>.</p>
<p>
<span class="versionmodified">Deprecated since version 3.1: </span><a title="unittest.TestCase.failUnless" class="reference internal" href="#unittest.TestCase.failUnless"><tt class="xref docutils literal"><span class="pre">failUnless()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertEqual">
<tt class="descname">assertEqual</tt><big>(</big><em>first</em>, <em>second</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertEqual" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.failUnlessEqual">
<tt class="descname">failUnlessEqual</tt><big>(</big><em>first</em>, <em>second</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.failUnlessEqual" title="Permalink to this definition">¶</a></dt>
<dd><p>Test that <em>first</em> and <em>second</em> are equal.  If the values do not compare
equal, the test will fail with the explanation given by <em>msg</em>, or
<a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>.  Note that using <a title="unittest.TestCase.assertEqual" class="reference internal" href="#unittest.TestCase.assertEqual"><tt class="xref docutils literal"><span class="pre">assertEqual()</span></tt></a> improves upon
doing the comparison as the first parameter to <a title="unittest.TestCase.assertTrue" class="reference internal" href="#unittest.TestCase.assertTrue"><tt class="xref docutils literal"><span class="pre">assertTrue()</span></tt></a>: the
default value for <em>msg</em> include representations of both <em>first</em> and
<em>second</em>.</p>
<p>In addition, if <em>first</em> and <em>second</em> are the exact same type and one of
list, tuple, dict, set, or frozenset or any type that a subclass
registers <a title="unittest.TestCase.addTypeEqualityFunc" class="reference internal" href="#unittest.TestCase.addTypeEqualityFunc"><tt class="xref docutils literal"><span class="pre">addTypeEqualityFunc()</span></tt></a> the type specific equality function
will be called in order to generate a more useful default error message.</p>
<p>
<span class="versionmodified">Changed in version 3.1: </span>Added the automatic calling of type specific equality function.</p>
<p>
<span class="versionmodified">Deprecated since version 3.1: </span><a title="unittest.TestCase.failUnlessEqual" class="reference internal" href="#unittest.TestCase.failUnlessEqual"><tt class="xref docutils literal"><span class="pre">failUnlessEqual()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertNotEqual">
<tt class="descname">assertNotEqual</tt><big>(</big><em>first</em>, <em>second</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertNotEqual" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.failIfEqual">
<tt class="descname">failIfEqual</tt><big>(</big><em>first</em>, <em>second</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.failIfEqual" title="Permalink to this definition">¶</a></dt>
<dd><p>Test that <em>first</em> and <em>second</em> are not equal.  If the values do compare
equal, the test will fail with the explanation given by <em>msg</em>, or
<a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>.  Note that using <a title="unittest.TestCase.assertNotEqual" class="reference internal" href="#unittest.TestCase.assertNotEqual"><tt class="xref docutils literal"><span class="pre">assertNotEqual()</span></tt></a> improves upon doing
the comparison as the first parameter to <a title="unittest.TestCase.assertTrue" class="reference internal" href="#unittest.TestCase.assertTrue"><tt class="xref docutils literal"><span class="pre">assertTrue()</span></tt></a> is that the
default value for <em>msg</em> can be computed to include representations of both
<em>first</em> and <em>second</em>.</p>
<p>
<span class="versionmodified">Deprecated since version 3.1: </span><a title="unittest.TestCase.failIfEqual" class="reference internal" href="#unittest.TestCase.failIfEqual"><tt class="xref docutils literal"><span class="pre">failIfEqual()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertAlmostEqual">
<tt class="descname">assertAlmostEqual</tt><big>(</big><em>first</em>, <em>second</em><span class="optional">[</span>, <em>places</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertAlmostEqual" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.failUnlessAlmostEqual">
<tt class="descname">failUnlessAlmostEqual</tt><big>(</big><em>first</em>, <em>second</em><span class="optional">[</span>, <em>places</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.failUnlessAlmostEqual" title="Permalink to this definition">¶</a></dt>
<dd><p>Test that <em>first</em> and <em>second</em> are approximately equal by computing the
difference, rounding to the given number of decimal <em>places</em> (default 7),
and comparing to zero.</p>
<p>Note that comparing a given number of decimal places is not the same as
comparing a given number of significant digits. If the values do not
compare equal, the test will fail with the explanation given by <em>msg</em>, or
<a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>.</p>
<p>
<span class="versionmodified">Deprecated since version 3.1: </span><a title="unittest.TestCase.failUnlessAlmostEqual" class="reference internal" href="#unittest.TestCase.failUnlessAlmostEqual"><tt class="xref docutils literal"><span class="pre">failUnlessAlmostEqual()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertNotAlmostEqual">
<tt class="descname">assertNotAlmostEqual</tt><big>(</big><em>first</em>, <em>second</em><span class="optional">[</span>, <em>places</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertNotAlmostEqual" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.failIfAlmostEqual">
<tt class="descname">failIfAlmostEqual</tt><big>(</big><em>first</em>, <em>second</em><span class="optional">[</span>, <em>places</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.failIfAlmostEqual" title="Permalink to this definition">¶</a></dt>
<dd><p>Test that <em>first</em> and <em>second</em> are not approximately equal by computing
the difference, rounding to the given number of decimal <em>places</em> (default
7), and comparing to zero.</p>
<p>Note that comparing a given number of decimal places is not the same as
comparing a given number of significant digits. If the values do not
compare equal, the test will fail with the explanation given by <em>msg</em>, or
<a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>.</p>
<p>
<span class="versionmodified">Deprecated since version 3.1: </span><a title="unittest.TestCase.failIfAlmostEqual" class="reference internal" href="#unittest.TestCase.failIfAlmostEqual"><tt class="xref docutils literal"><span class="pre">failIfAlmostEqual()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertGreater">
<tt class="descname">assertGreater</tt><big>(</big><em>first</em>, <em>second</em>, <em>msg=None</em><big>)</big><a class="headerlink" href="#unittest.TestCase.assertGreater" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.assertGreaterEqual">
<tt class="descname">assertGreaterEqual</tt><big>(</big><em>first</em>, <em>second</em>, <em>msg=None</em><big>)</big><a class="headerlink" href="#unittest.TestCase.assertGreaterEqual" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.assertLess">
<tt class="descname">assertLess</tt><big>(</big><em>first</em>, <em>second</em>, <em>msg=None</em><big>)</big><a class="headerlink" href="#unittest.TestCase.assertLess" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.assertLessEqual">
<tt class="descname">assertLessEqual</tt><big>(</big><em>first</em>, <em>second</em>, <em>msg=None</em><big>)</big><a class="headerlink" href="#unittest.TestCase.assertLessEqual" title="Permalink to this definition">¶</a></dt>
<dd><p>Test that <em>first</em> is respectively &gt;, &gt;=, &lt; or &lt;= than <em>second</em> depending
on the method name.  If not, the test will fail with an explanation
or with the explanation given by <em>msg</em>:</p>
<div class="highlight-python3"><div class="highlight"><pre><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="mf">3</span><span class="p">,</span> <span class="mf">4</span><span class="p">)</span>
<span class="go">AssertionError: &quot;3&quot; unexpectedly not greater than or equal to &quot;4&quot;</span>
</pre></div>
</div>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertMultiLineEqual">
<tt class="descname">assertMultiLineEqual</tt><big>(</big><em>self</em>, <em>first</em>, <em>second</em>, <em>msg=None</em><big>)</big><a class="headerlink" href="#unittest.TestCase.assertMultiLineEqual" title="Permalink to this definition">¶</a></dt>
<dd><p>Test that the multiline string <em>first</em> is equal to the string <em>second</em>.
When not equal a diff of the two strings highlighting the differences
will be included in the error message.</p>
<p>If specified <em>msg</em> will be used as the error message on failure.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt>
<tt class="descname">assertRegexpMatches(text, regexp[, msg=None]):</tt></dt>
<dd><p>Verifies that a <em>regexp</em> search matches <em>text</em>.  Fails with an error
message including the pattern and the <em>text</em>.  <em>regexp</em> may be
a regular expression object or a string containing a regular expression
suitable for use by <a title="re.search" class="reference external" href="re.html#re.search"><tt class="xref docutils literal"><span class="pre">re.search()</span></tt></a>.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertIn">
<tt class="descname">assertIn</tt><big>(</big><em>first</em>, <em>second</em>, <em>msg=None</em><big>)</big><a class="headerlink" href="#unittest.TestCase.assertIn" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.assertNotIn">
<tt class="descname">assertNotIn</tt><big>(</big><em>first</em>, <em>second</em>, <em>msg=None</em><big>)</big><a class="headerlink" href="#unittest.TestCase.assertNotIn" title="Permalink to this definition">¶</a></dt>
<dd><p>Tests that <em>first</em> is or is not in <em>second</em> with an explanatory error
message as appropriate.</p>
<p>If specified <em>msg</em> will be used as the error message on failure.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertSameElements">
<tt class="descname">assertSameElements</tt><big>(</big><em>expected</em>, <em>actual</em>, <em>msg=None</em><big>)</big><a class="headerlink" href="#unittest.TestCase.assertSameElements" title="Permalink to this definition">¶</a></dt>
<dd><p>Test that sequence <em>expected</em> contains the same elements as <em>actual</em>.
When they don&#8217;t an error message listing the differences between the
sequences will be generated.</p>
<p>If specified <em>msg</em> will be used as the error message on failure.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertSetEqual">
<tt class="descname">assertSetEqual</tt><big>(</big><em>set1</em>, <em>set2</em>, <em>msg=None</em><big>)</big><a class="headerlink" href="#unittest.TestCase.assertSetEqual" title="Permalink to this definition">¶</a></dt>
<dd><p>Tests that two sets are equal.  If not, an error message is constructed
that lists the differences between the sets.</p>
<p>Fails if either of <em>set1</em> or <em>set2</em> does not have a <a title="set.difference" class="reference external" href="stdtypes.html#set.difference"><tt class="xref docutils literal"><span class="pre">set.difference()</span></tt></a>
method.</p>
<p>If specified <em>msg</em> will be used as the error message on failure.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertDictEqual">
<tt class="descname">assertDictEqual</tt><big>(</big><em>expected</em>, <em>actual</em>, <em>msg=None</em><big>)</big><a class="headerlink" href="#unittest.TestCase.assertDictEqual" title="Permalink to this definition">¶</a></dt>
<dd><p>Test that two dictionaries are equal.  If not, an error message is
constructed that shows the differences in the dictionaries.</p>
<p>If specified <em>msg</em> will be used as the error message on failure.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertDictContainsSubset">
<tt class="descname">assertDictContainsSubset</tt><big>(</big><em>expected</em>, <em>actual</em>, <em>msg=None</em><big>)</big><a class="headerlink" href="#unittest.TestCase.assertDictContainsSubset" title="Permalink to this definition">¶</a></dt>
<dd><p>Tests whether the key/value pairs in dictionary <em>actual</em> are a
superset of those in <em>expected</em>.  If not, an error message listing
the missing keys and mismatched values is generated.</p>
<p>If specified <em>msg</em> will be used as the error message on failure.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertListEqual">
<tt class="descname">assertListEqual</tt><big>(</big><em>list1</em>, <em>list2</em>, <em>msg=None</em><big>)</big><a class="headerlink" href="#unittest.TestCase.assertListEqual" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.assertTupleEqual">
<tt class="descname">assertTupleEqual</tt><big>(</big><em>tuple1</em>, <em>tuple2</em>, <em>msg=None</em><big>)</big><a class="headerlink" href="#unittest.TestCase.assertTupleEqual" title="Permalink to this definition">¶</a></dt>
<dd><p>Tests that two lists or tuples are equal.  If not an error message is
constructed that shows only the differences between the two.  An error
is also raised if either of the parameters are of the wrong type.</p>
<p>If specified <em>msg</em> will be used as the error message on failure.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertSequenceEqual">
<tt class="descname">assertSequenceEqual</tt><big>(</big><em>seq1</em>, <em>seq2</em>, <em>msg=None</em>, <em>seq_type=None</em><big>)</big><a class="headerlink" href="#unittest.TestCase.assertSequenceEqual" title="Permalink to this definition">¶</a></dt>
<dd><p>Tests that two sequences are equal.  If a <em>seq_type</em> is supplied, both
<em>seq1</em> and <em>seq2</em> must be instances of <em>seq_type</em> or a failure will
be raised.  If the sequences are different an error message is
constructed that shows the difference between the two.</p>
<p>If specified <em>msg</em> will be used as the error message on failure.</p>
<p>This method is used to implement <a title="unittest.TestCase.assertListEqual" class="reference internal" href="#unittest.TestCase.assertListEqual"><tt class="xref docutils literal"><span class="pre">assertListEqual()</span></tt></a> and
<a title="unittest.TestCase.assertTupleEqual" class="reference internal" href="#unittest.TestCase.assertTupleEqual"><tt class="xref docutils literal"><span class="pre">assertTupleEqual()</span></tt></a>.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertRaises">
<tt class="descname">assertRaises</tt><big>(</big><em>exception</em><span class="optional">[</span>, <em>callable</em>, <em>...</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertRaises" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.failUnlessRaises">
<tt class="descname">failUnlessRaises</tt><big>(</big><em>exception</em><span class="optional">[</span>, <em>callable</em>, <em>...</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.failUnlessRaises" title="Permalink to this definition">¶</a></dt>
<dd><p>Test that an exception is raised when <em>callable</em> is called with any
positional or keyword arguments that are also passed to
<a title="unittest.TestCase.assertRaises" class="reference internal" href="#unittest.TestCase.assertRaises"><tt class="xref docutils literal"><span class="pre">assertRaises()</span></tt></a>.  The test passes if <em>exception</em> is raised, is an
error if another exception is raised, or fails if no exception is raised.
To catch any of a group of exceptions, a tuple containing the exception
classes may be passed as <em>exception</em>.</p>
<p>If <em>callable</em> is omitted or None, returns a context manager so that the
code under test can be written inline rather than as a function:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">failUnlessRaises</span><span class="p">(</span><span class="n">some_error_class</span><span class="p">):</span>
    <span class="n">do_something</span><span class="p">()</span>
</pre></div>
</div>
<p>
<span class="versionmodified">Changed in version 3.1: </span>Added the ability to use <a title="unittest.TestCase.assertRaises" class="reference internal" href="#unittest.TestCase.assertRaises"><tt class="xref docutils literal"><span class="pre">assertRaises()</span></tt></a> as a context manager.</p>
<p>
<span class="versionmodified">Deprecated since version 3.1: </span><a title="unittest.TestCase.failUnlessRaises" class="reference internal" href="#unittest.TestCase.failUnlessRaises"><tt class="xref docutils literal"><span class="pre">failUnlessRaises()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertRaisesRegexp">
<tt class="descname">assertRaisesRegexp</tt><big>(</big><em>exception</em>, <em>regexp</em><span class="optional">[</span>, <em>callable</em>, <em>...</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertRaisesRegexp" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a title="unittest.TestCase.assertRaises" class="reference internal" href="#unittest.TestCase.assertRaises"><tt class="xref docutils literal"><span class="pre">assertRaises()</span></tt></a> but also tests that <em>regexp</em> matches
on the string representation of the raised exception.  <em>regexp</em> may be
a regular expression object or a string containing a regular expression
suitable for use by <a title="re.search" class="reference external" href="re.html#re.search"><tt class="xref docutils literal"><span class="pre">re.search()</span></tt></a>.  Examples:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="bp">self</span><span class="o">.</span><span class="n">assertRaisesRegexp</span><span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="s">&#39;invalid literal for.*XYZ$&#39;</span><span class="p">,</span>
                        <span class="nb">int</span><span class="p">,</span> <span class="s">&#39;XYZ&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>or:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaisesRegexp</span><span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="s">&#39;literal&#39;</span><span class="p">):</span>
   <span class="nb">int</span><span class="p">(</span><span class="s">&#39;XYZ&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertIsNone">
<tt class="descname">assertIsNone</tt><big>(</big><em>expr</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertIsNone" title="Permalink to this definition">¶</a></dt>
<dd><p>This signals a test failure if <em>expr</em> is not None.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertIsNotNone">
<tt class="descname">assertIsNotNone</tt><big>(</big><em>expr</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertIsNotNone" title="Permalink to this definition">¶</a></dt>
<dd><p>The inverse of the <a title="unittest.TestCase.assertIsNone" class="reference internal" href="#unittest.TestCase.assertIsNone"><tt class="xref docutils literal"><span class="pre">assertIsNone()</span></tt></a> method.
This signals a test failure if <em>expr</em> is None.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertIs">
<tt class="descname">assertIs</tt><big>(</big><em>expr1</em>, <em>expr2</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertIs" title="Permalink to this definition">¶</a></dt>
<dd><p>This signals a test failure if <em>expr1</em> and <em>expr2</em> don&#8217;t evaluate to the same
object.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertIsNot">
<tt class="descname">assertIsNot</tt><big>(</big><em>expr1</em>, <em>expr2</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertIsNot" title="Permalink to this definition">¶</a></dt>
<dd><p>The inverse of the <a title="unittest.TestCase.assertIs" class="reference internal" href="#unittest.TestCase.assertIs"><tt class="xref docutils literal"><span class="pre">assertIs()</span></tt></a> method.
This signals a test failure if <em>expr1</em> and <em>expr2</em> evaluate to the same
object.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertFalse">
<tt class="descname">assertFalse</tt><big>(</big><em>expr</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertFalse" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.failIf">
<tt class="descname">failIf</tt><big>(</big><em>expr</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.failIf" title="Permalink to this definition">¶</a></dt>
<dd><p>The inverse of the <a title="unittest.TestCase.assertTrue" class="reference internal" href="#unittest.TestCase.assertTrue"><tt class="xref docutils literal"><span class="pre">assertTrue()</span></tt></a> method is the <a title="unittest.TestCase.assertFalse" class="reference internal" href="#unittest.TestCase.assertFalse"><tt class="xref docutils literal"><span class="pre">assertFalse()</span></tt></a> method.
This signals a test failure if <em>expr</em> is true, with <em>msg</em> or <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>
for the error message.</p>
<p>
<span class="versionmodified">Deprecated since version 3.1: </span><a title="unittest.TestCase.failIf" class="reference internal" href="#unittest.TestCase.failIf"><tt class="xref docutils literal"><span class="pre">failIf()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.fail">
<tt class="descname">fail</tt><big>(</big><span class="optional">[</span><em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.fail" title="Permalink to this definition">¶</a></dt>
<dd>Signals a test failure unconditionally, with <em>msg</em> or <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a> for
the error message.</dd></dl>

<dl class="attribute">
<dt id="unittest.TestCase.failureException">
<tt class="descname">failureException</tt><a class="headerlink" href="#unittest.TestCase.failureException" title="Permalink to this definition">¶</a></dt>
<dd>This class attribute gives the exception raised by the test method.  If a
test framework needs to use a specialized exception, possibly to carry
additional information, it must subclass this exception in order to &#8220;play
fair&#8221; with the framework.  The initial value of this attribute is
<a title="exceptions.AssertionError" class="reference external" href="exceptions.html#exceptions.AssertionError"><tt class="xref docutils literal"><span class="pre">AssertionError</span></tt></a>.</dd></dl>

<dl class="attribute">
<dt id="unittest.TestCase.longMessage">
<tt class="descname">longMessage</tt><a class="headerlink" href="#unittest.TestCase.longMessage" title="Permalink to this definition">¶</a></dt>
<dd><p>If set to True then any explicit failure message you pass in to the
assert methods will be appended to the end of the normal failure message.
The normal messages contain useful information about the objects involved,
for example the message from assertEqual shows you the repr of the two
unequal objects. Setting this attribute to True allows you to have a
custom error message in addition to the normal one.</p>
<p>This attribute defaults to False, meaning that a custom message passed
to an assert method will silence the normal message.</p>
<p>The class setting can be overridden in individual tests by assigning an
instance attribute to True or False before calling the assert methods.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<p>Testing frameworks can use the following methods to collect information on
the test:</p>
<dl class="method">
<dt id="unittest.TestCase.countTestCases">
<tt class="descname">countTestCases</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestCase.countTestCases" title="Permalink to this definition">¶</a></dt>
<dd>Return the number of tests represented by this test object.  For
<a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instances, this will always be <tt class="docutils literal"><span class="pre">1</span></tt>.</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.defaultTestResult">
<tt class="descname">defaultTestResult</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestCase.defaultTestResult" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an instance of the test result class that should be used for this
test case class (if no other result instance is provided to the
<a title="unittest.TestCase.run" class="reference internal" href="#unittest.TestCase.run"><tt class="xref docutils literal"><span class="pre">run()</span></tt></a> method).</p>
<p>For <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instances, this will always be an instance of
<a title="unittest.TestResult" class="reference internal" href="#unittest.TestResult"><tt class="xref docutils literal"><span class="pre">TestResult</span></tt></a>; subclasses of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> should override this
as necessary.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.id">
<tt class="descname">id</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestCase.id" title="Permalink to this definition">¶</a></dt>
<dd>Return a string identifying the specific test case.  This is usually the
full name of the test method, including the module and class name.</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.shortDescription">
<tt class="descname">shortDescription</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestCase.shortDescription" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a description of the test, or <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a> if no description
has been provided.  The default implementation of this method
returns the first line of the test method&#8217;s docstring, if available,
along with the method name.</p>
<p>
<span class="versionmodified">Changed in version 3.1: </span>In earlier versions this only returned the first line of the test
method&#8217;s docstring, if available or the <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>.  That led to
undesirable behavior of not printing the test name when someone was
thoughtful enough to write a docstring.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.addTypeEqualityFunc">
<tt class="descname">addTypeEqualityFunc</tt><big>(</big><em>typeobj</em>, <em>function</em><big>)</big><a class="headerlink" href="#unittest.TestCase.addTypeEqualityFunc" title="Permalink to this definition">¶</a></dt>
<dd><p>Registers a type specific <a title="unittest.TestCase.assertEqual" class="reference internal" href="#unittest.TestCase.assertEqual"><tt class="xref docutils literal"><span class="pre">assertEqual()</span></tt></a> equality checking
function to be called by <a title="unittest.TestCase.assertEqual" class="reference internal" href="#unittest.TestCase.assertEqual"><tt class="xref docutils literal"><span class="pre">assertEqual()</span></tt></a> when both objects it has
been asked to compare are exactly <em>typeobj</em> (not subclasses).
<em>function</em> must take two positional arguments and a third msg=None
keyword argument just as <a title="unittest.TestCase.assertEqual" class="reference internal" href="#unittest.TestCase.assertEqual"><tt class="xref docutils literal"><span class="pre">assertEqual()</span></tt></a> does.  It must raise
<tt class="docutils literal"><span class="pre">self.failureException</span></tt> when inequality between the first two
parameters is detected.</p>
<p>One good use of custom equality checking functions for a type
is to raise <tt class="docutils literal"><span class="pre">self.failureException</span></tt> with an error message useful
for debugging the problem by explaining the inequalities in detail.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.addCleanup">
<tt class="descname">addCleanup</tt><big>(</big><em>function</em><span class="optional">[</span>, <em>*args</em><span class="optional">[</span>, <em>**kwargs</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.addCleanup" title="Permalink to this definition">¶</a></dt>
<dd><p>Add a function to be called after <a title="unittest.TestCase.tearDown" class="reference internal" href="#unittest.TestCase.tearDown"><tt class="xref docutils literal"><span class="pre">tearDown()</span></tt></a> to cleanup resources
used during the test. Functions will be called in reverse order to the
order they are added (LIFO). They are called with any arguments and
keyword arguments passed into <a title="unittest.TestCase.addCleanup" class="reference internal" href="#unittest.TestCase.addCleanup"><tt class="xref docutils literal"><span class="pre">addCleanup()</span></tt></a> when they are
added.</p>
<p>If <a title="unittest.TestCase.setUp" class="reference internal" href="#unittest.TestCase.setUp"><tt class="xref docutils literal"><span class="pre">setUp()</span></tt></a> fails, meaning that <a title="unittest.TestCase.tearDown" class="reference internal" href="#unittest.TestCase.tearDown"><tt class="xref docutils literal"><span class="pre">tearDown()</span></tt></a> is not called,
then any cleanup functions added will still be called.</p>
<p>
<span class="versionmodified">New in version 2.7.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.doCleanups">
<tt class="descname">doCleanups</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestCase.doCleanups" title="Permalink to this definition">¶</a></dt>
<dd><p>This method is called uncoditionally after <a title="unittest.TestCase.tearDown" class="reference internal" href="#unittest.TestCase.tearDown"><tt class="xref docutils literal"><span class="pre">tearDown()</span></tt></a>, or
after <a title="unittest.TestCase.setUp" class="reference internal" href="#unittest.TestCase.setUp"><tt class="xref docutils literal"><span class="pre">setUp()</span></tt></a> if <a title="unittest.TestCase.setUp" class="reference internal" href="#unittest.TestCase.setUp"><tt class="xref docutils literal"><span class="pre">setUp()</span></tt></a> raises an exception.</p>
<p>It is responsible for calling all the cleanup functions added by
<a title="unittest.TestCase.addCleanup" class="reference internal" href="#unittest.TestCase.addCleanup"><tt class="xref docutils literal"><span class="pre">addCleanup()</span></tt></a>. If you need cleanup functions to be called
<em>prior</em> to <a title="unittest.TestCase.tearDown" class="reference internal" href="#unittest.TestCase.tearDown"><tt class="xref docutils literal"><span class="pre">tearDown()</span></tt></a> then you can call <a title="unittest.TestCase.doCleanups" class="reference internal" href="#unittest.TestCase.doCleanups"><tt class="xref docutils literal"><span class="pre">doCleanups()</span></tt></a>
yourself.</p>
<p><a title="unittest.TestCase.doCleanups" class="reference internal" href="#unittest.TestCase.doCleanups"><tt class="xref docutils literal"><span class="pre">doCleanups()</span></tt></a> pops methods off the stack of cleanup
functions one at a time, so it can be called at any time.</p>
<p>
<span class="versionmodified">New in version 2.7.</span></p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="unittest.FunctionTestCase">
<em class="property">
class </em><tt class="descclassname">unittest.</tt><tt class="descname">FunctionTestCase</tt><big>(</big><em>testFunc</em><span class="optional">[</span>, <em>setUp</em><span class="optional">[</span>, <em>tearDown</em><span class="optional">[</span>, <em>description</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.FunctionTestCase" title="Permalink to this definition">¶</a></dt>
<dd>This class implements the portion of the <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> interface which
allows the test runner to drive the test, but does not provide the methods which
test code can use to check and report errors. This is used to create test cases
using legacy test code, allowing it to be integrated into a <tt class="xref docutils literal"><span class="pre">unittest</span></tt>-based test framework.</dd></dl>

</div>
<div class="section" id="grouping-tests">
<span id="testsuite-objects"></span><h3>25.3.5.2. Grouping tests<a class="headerlink" href="#grouping-tests" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="unittest.TestSuite">
<em class="property">
class </em><tt class="descclassname">unittest.</tt><tt class="descname">TestSuite</tt><big>(</big><span class="optional">[</span><em>tests</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestSuite" title="Permalink to this definition">¶</a></dt>
<dd><p>This class represents an aggregation of individual tests cases and test suites.
The class presents the interface needed by the test runner to allow it to be run
as any other test case.  Running a <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> instance is the same as
iterating over the suite, running each test individually.</p>
<p>If <em>tests</em> is given, it must be an iterable of individual test cases or other
test suites that will be used to build the suite initially. Additional methods
are provided to add test cases and suites to the collection later on.</p>
<p><a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> objects behave much like <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> objects, except
they do not actually implement a test.  Instead, they are used to aggregate
tests into groups of tests that should be run together. Some additional
methods are available to add tests to <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> instances:</p>
<dl class="method">
<dt id="unittest.TestSuite.addTest">
<tt class="descname">addTest</tt><big>(</big><em>test</em><big>)</big><a class="headerlink" href="#unittest.TestSuite.addTest" title="Permalink to this definition">¶</a></dt>
<dd>Add a <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> or <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> to the suite.</dd></dl>

<dl class="method">
<dt id="unittest.TestSuite.addTests">
<tt class="descname">addTests</tt><big>(</big><em>tests</em><big>)</big><a class="headerlink" href="#unittest.TestSuite.addTests" title="Permalink to this definition">¶</a></dt>
<dd><p>Add all the tests from an iterable of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> and <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a>
instances to this test suite.</p>
<p>This is equivalent to iterating over <em>tests</em>, calling <a title="unittest.TestSuite.addTest" class="reference internal" href="#unittest.TestSuite.addTest"><tt class="xref docutils literal"><span class="pre">addTest()</span></tt></a> for each
element.</p>
</dd></dl>

<p><a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> shares the following methods with <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>:</p>
<dl class="method">
<dt id="unittest.TestSuite.run">
<tt class="descname">run</tt><big>(</big><em>result</em><big>)</big><a class="headerlink" href="#unittest.TestSuite.run" title="Permalink to this definition">¶</a></dt>
<dd>Run the tests associated with this suite, collecting the result into the
test result object passed as <em>result</em>.  Note that unlike
<a title="unittest.TestCase.run" class="reference internal" href="#unittest.TestCase.run"><tt class="xref docutils literal"><span class="pre">TestCase.run()</span></tt></a>, <a title="unittest.TestSuite.run" class="reference internal" href="#unittest.TestSuite.run"><tt class="xref docutils literal"><span class="pre">TestSuite.run()</span></tt></a> requires the result object to
be passed in.</dd></dl>

<dl class="method">
<dt id="unittest.TestSuite.debug">
<tt class="descname">debug</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestSuite.debug" title="Permalink to this definition">¶</a></dt>
<dd>Run the tests associated with this suite without collecting the
result. This allows exceptions raised by the test to be propagated to the
caller and can be used to support running tests under a debugger.</dd></dl>

<dl class="method">
<dt id="unittest.TestSuite.countTestCases">
<tt class="descname">countTestCases</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestSuite.countTestCases" title="Permalink to this definition">¶</a></dt>
<dd>Return the number of tests represented by this test object, including all
individual tests and sub-suites.</dd></dl>

<dl class="method">
<dt id="unittest.TestSuite.__iter__">
<tt class="descname">__iter__</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestSuite.__iter__" title="Permalink to this definition">¶</a></dt>
<dd><p>Tests grouped by a <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> are always accessed by iteration.
Subclasses can lazily provide tests by overriding <a title="unittest.TestSuite.__iter__" class="reference internal" href="#unittest.TestSuite.__iter__"><tt class="xref docutils literal"><span class="pre">__iter__()</span></tt></a>. Note
that this method maybe called several times on a single suite
(for example when counting tests or comparing for equality)
so the tests returned must be the same for repeated iterations.</p>
<p>
<span class="versionmodified">Changed in version 2.7: </span>In earlier versions the <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> accessed tests directly rather
than through iteration, so overriding <a title="unittest.TestSuite.__iter__" class="reference internal" href="#unittest.TestSuite.__iter__"><tt class="xref docutils literal"><span class="pre">__iter__()</span></tt></a> wasn&#8217;t sufficient
for providing tests.</p>
</dd></dl>

<p>In the typical usage of a <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> object, the <a title="unittest.TestSuite.run" class="reference internal" href="#unittest.TestSuite.run"><tt class="xref docutils literal"><span class="pre">run()</span></tt></a> method
is invoked by a <tt class="xref docutils literal"><span class="pre">TestRunner</span></tt> rather than by the end-user test harness.</p>
</dd></dl>

</div>
<div class="section" id="loading-and-running-tests">
<h3>25.3.5.3. Loading and running tests<a class="headerlink" href="#loading-and-running-tests" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="unittest.TestLoader">
<em class="property">
class </em><tt class="descclassname">unittest.</tt><tt class="descname">TestLoader</tt><a class="headerlink" href="#unittest.TestLoader" title="Permalink to this definition">¶</a></dt>
<dd><p>The <a title="unittest.TestLoader" class="reference internal" href="#unittest.TestLoader"><tt class="xref docutils literal"><span class="pre">TestLoader</span></tt></a> class is used to create test suites from classes and
modules.  Normally, there is no need to create an instance of this class; the
<tt class="xref docutils literal"><span class="pre">unittest</span></tt> module provides an instance that can be shared as
<tt class="docutils literal"><span class="pre">unittest.defaultTestLoader</span></tt>. Using a subclass or instance, however, allows
customization of some configurable properties.</p>
<p><a title="unittest.TestLoader" class="reference internal" href="#unittest.TestLoader"><tt class="xref docutils literal"><span class="pre">TestLoader</span></tt></a> objects have the following methods:</p>
<dl class="method">
<dt id="unittest.TestLoader.loadTestsFromTestCase">
<tt class="descname">loadTestsFromTestCase</tt><big>(</big><em>testCaseClass</em><big>)</big><a class="headerlink" href="#unittest.TestLoader.loadTestsFromTestCase" title="Permalink to this definition">¶</a></dt>
<dd>Return a suite of all tests cases contained in the <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>-derived
<tt class="xref docutils literal"><span class="pre">testCaseClass</span></tt>.</dd></dl>

<dl class="method">
<dt id="unittest.TestLoader.loadTestsFromModule">
<tt class="descname">loadTestsFromModule</tt><big>(</big><em>module</em><big>)</big><a class="headerlink" href="#unittest.TestLoader.loadTestsFromModule" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a suite of all tests cases contained in the given module. This
method searches <em>module</em> for classes derived from <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> and
creates an instance of the class for each test method defined for the
class.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">While using a hierarchy of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>-derived classes can be
convenient in sharing fixtures and helper functions, defining test
methods on base classes that are not intended to be instantiated
directly does not play well with this method.  Doing so, however, can
be useful when the fixtures are different and defined in subclasses.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="unittest.TestLoader.loadTestsFromName">
<tt class="descname">loadTestsFromName</tt><big>(</big><em>name</em><span class="optional">[</span>, <em>module</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestLoader.loadTestsFromName" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a suite of all tests cases given a string specifier.</p>
<p>The specifier <em>name</em> is a &#8220;dotted name&#8221; that may resolve either to a
module, a test case class, a test method within a test case class, a
<a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> instance, or a callable object which returns a
<a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> or <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> instance.  These checks are
applied in the order listed here; that is, a method on a possible test
case class will be picked up as &#8220;a test method within a test case class&#8221;,
rather than &#8220;a callable object&#8221;.</p>
<p>For example, if you have a module <tt class="xref docutils literal"><span class="pre">SampleTests</span></tt> containing a
<a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>-derived class <tt class="xref docutils literal"><span class="pre">SampleTestCase</span></tt> with three test
methods (<tt class="xref docutils literal"><span class="pre">test_one()</span></tt>, <tt class="xref docutils literal"><span class="pre">test_two()</span></tt>, and <tt class="xref docutils literal"><span class="pre">test_three()</span></tt>), the
specifier <tt class="docutils literal"><span class="pre">'SampleTests.SampleTestCase'</span></tt> would cause this method to return a
suite which will run all three test methods.  Using the specifier
<tt class="docutils literal"><span class="pre">'SampleTests.SampleTestCase.test_two'</span></tt> would cause it to return a test suite
which will run only the <tt class="xref docutils literal"><span class="pre">test_two()</span></tt> test method.  The specifier can refer
to modules and packages which have not been imported; they will be imported as a
side-effect.</p>
<p>The method optionally resolves <em>name</em> relative to the given <em>module</em>.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestLoader.loadTestsFromNames">
<tt class="descname">loadTestsFromNames</tt><big>(</big><em>names</em><span class="optional">[</span>, <em>module</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestLoader.loadTestsFromNames" title="Permalink to this definition">¶</a></dt>
<dd>Similar to <a title="unittest.TestLoader.loadTestsFromName" class="reference internal" href="#unittest.TestLoader.loadTestsFromName"><tt class="xref docutils literal"><span class="pre">loadTestsFromName()</span></tt></a>, but takes a sequence of names rather
than a single name.  The return value is a test suite which supports all
the tests defined for each name.</dd></dl>

<dl class="method">
<dt id="unittest.TestLoader.getTestCaseNames">
<tt class="descname">getTestCaseNames</tt><big>(</big><em>testCaseClass</em><big>)</big><a class="headerlink" href="#unittest.TestLoader.getTestCaseNames" title="Permalink to this definition">¶</a></dt>
<dd>Return a sorted sequence of method names found within <em>testCaseClass</em>;
this should be a subclass of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>.</dd></dl>

<p>The following attributes of a <a title="unittest.TestLoader" class="reference internal" href="#unittest.TestLoader"><tt class="xref docutils literal"><span class="pre">TestLoader</span></tt></a> can be configured either by
subclassing or assignment on an instance:</p>
<dl class="attribute">
<dt id="unittest.TestLoader.testMethodPrefix">
<tt class="descname">testMethodPrefix</tt><a class="headerlink" href="#unittest.TestLoader.testMethodPrefix" title="Permalink to this definition">¶</a></dt>
<dd><p>String giving the prefix of method names which will be interpreted as test
methods.  The default value is <tt class="docutils literal"><span class="pre">'test'</span></tt>.</p>
<p>This affects <a title="unittest.TestLoader.getTestCaseNames" class="reference internal" href="#unittest.TestLoader.getTestCaseNames"><tt class="xref docutils literal"><span class="pre">getTestCaseNames()</span></tt></a> and all the <tt class="xref docutils literal"><span class="pre">loadTestsFrom*()</span></tt>
methods.</p>
</dd></dl>

<dl class="attribute">
<dt id="unittest.TestLoader.sortTestMethodsUsing">
<tt class="descname">sortTestMethodsUsing</tt><a class="headerlink" href="#unittest.TestLoader.sortTestMethodsUsing" title="Permalink to this definition">¶</a></dt>
<dd>Function to be used to compare method names when sorting them in
<a title="unittest.TestLoader.getTestCaseNames" class="reference internal" href="#unittest.TestLoader.getTestCaseNames"><tt class="xref docutils literal"><span class="pre">getTestCaseNames()</span></tt></a> and all the <tt class="xref docutils literal"><span class="pre">loadTestsFrom*()</span></tt> methods.</dd></dl>

<dl class="attribute">
<dt id="unittest.TestLoader.suiteClass">
<tt class="descname">suiteClass</tt><a class="headerlink" href="#unittest.TestLoader.suiteClass" title="Permalink to this definition">¶</a></dt>
<dd><p>Callable object that constructs a test suite from a list of tests. No
methods on the resulting object are needed.  The default value is the
<a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> class.</p>
<p>This affects all the <tt class="xref docutils literal"><span class="pre">loadTestsFrom*()</span></tt> methods.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="unittest.TestResult">
<em class="property">
class </em><tt class="descclassname">unittest.</tt><tt class="descname">TestResult</tt><a class="headerlink" href="#unittest.TestResult" title="Permalink to this definition">¶</a></dt>
<dd><p>This class is used to compile information about which tests have succeeded
and which have failed.</p>
<p>A <a title="unittest.TestResult" class="reference internal" href="#unittest.TestResult"><tt class="xref docutils literal"><span class="pre">TestResult</span></tt></a> object stores the results of a set of tests.  The
<a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> and <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> classes ensure that results are
properly recorded; test authors do not need to worry about recording the
outcome of tests.</p>
<p>Testing frameworks built on top of <tt class="xref docutils literal"><span class="pre">unittest</span></tt> may want access to the
<a title="unittest.TestResult" class="reference internal" href="#unittest.TestResult"><tt class="xref docutils literal"><span class="pre">TestResult</span></tt></a> object generated by running a set of tests for reporting
purposes; a <a title="unittest.TestResult" class="reference internal" href="#unittest.TestResult"><tt class="xref docutils literal"><span class="pre">TestResult</span></tt></a> instance is returned by the
<tt class="xref docutils literal"><span class="pre">TestRunner.run()</span></tt> method for this purpose.</p>
<p><a title="unittest.TestResult" class="reference internal" href="#unittest.TestResult"><tt class="xref docutils literal"><span class="pre">TestResult</span></tt></a> instances have the following attributes that will be of
interest when inspecting the results of running a set of tests:</p>
<dl class="attribute">
<dt id="unittest.TestResult.errors">
<tt class="descname">errors</tt><a class="headerlink" href="#unittest.TestResult.errors" title="Permalink to this definition">¶</a></dt>
<dd>A list containing 2-tuples of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instances and strings
holding formatted tracebacks. Each tuple represents a test which raised an
unexpected exception.</dd></dl>

<dl class="attribute">
<dt id="unittest.TestResult.failures">
<tt class="descname">failures</tt><a class="headerlink" href="#unittest.TestResult.failures" title="Permalink to this definition">¶</a></dt>
<dd>A list containing 2-tuples of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instances and strings
holding formatted tracebacks. Each tuple represents a test where a failure
was explicitly signalled using the <tt class="xref docutils literal"><span class="pre">TestCase.fail*()</span></tt> or
<tt class="xref docutils literal"><span class="pre">TestCase.assert*()</span></tt> methods.</dd></dl>

<dl class="attribute">
<dt id="unittest.TestResult.skipped">
<tt class="descname">skipped</tt><a class="headerlink" href="#unittest.TestResult.skipped" title="Permalink to this definition">¶</a></dt>
<dd><p>A list containing 2-tuples of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instances and strings
holding the reason for skipping the test.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="attribute">
<dt id="unittest.TestResult.expectedFailures">
<tt class="descname">expectedFailures</tt><a class="headerlink" href="#unittest.TestResult.expectedFailures" title="Permalink to this definition">¶</a></dt>
<dd>A list contaning 2-tuples of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instances and strings
holding formatted tracebacks.  Each tuple represents a expected failures
of the test case.</dd></dl>

<dl class="attribute">
<dt id="unittest.TestResult.unexpectedSuccesses">
<tt class="descname">unexpectedSuccesses</tt><a class="headerlink" href="#unittest.TestResult.unexpectedSuccesses" title="Permalink to this definition">¶</a></dt>
<dd>A list containing <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instances that were marked as expected
failures, but succeeded.</dd></dl>

<dl class="attribute">
<dt id="unittest.TestResult.shouldStop">
<tt class="descname">shouldStop</tt><a class="headerlink" href="#unittest.TestResult.shouldStop" title="Permalink to this definition">¶</a></dt>
<dd>Set to <tt class="xref docutils literal"><span class="pre">True</span></tt> when the execution of tests should stop by <a title="unittest.TestResult.stop" class="reference internal" href="#unittest.TestResult.stop"><tt class="xref docutils literal"><span class="pre">stop()</span></tt></a>.</dd></dl>

<dl class="attribute">
<dt id="unittest.TestResult.testsRun">
<tt class="descname">testsRun</tt><a class="headerlink" href="#unittest.TestResult.testsRun" title="Permalink to this definition">¶</a></dt>
<dd>The total number of tests run so far.</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.wasSuccessful">
<tt class="descname">wasSuccessful</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestResult.wasSuccessful" title="Permalink to this definition">¶</a></dt>
<dd>Return <a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a> if all tests run so far have passed, otherwise returns
<a title="False" class="reference external" href="constants.html#False"><tt class="xref xref docutils literal"><span class="pre">False</span></tt></a>.</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.stop">
<tt class="descname">stop</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestResult.stop" title="Permalink to this definition">¶</a></dt>
<dd><p>This method can be called to signal that the set of tests being run should
be aborted by setting the <a title="unittest.TestResult.shouldStop" class="reference internal" href="#unittest.TestResult.shouldStop"><tt class="xref docutils literal"><span class="pre">shouldStop</span></tt></a> attribute to <a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a>.
<tt class="xref docutils literal"><span class="pre">TestRunner</span></tt> objects should respect this flag and return without
running any additional tests.</p>
<p>For example, this feature is used by the <a title="unittest.TextTestRunner" class="reference internal" href="#unittest.TextTestRunner"><tt class="xref docutils literal"><span class="pre">TextTestRunner</span></tt></a> class to
stop the test framework when the user signals an interrupt from the
keyboard.  Interactive tools which provide <tt class="xref docutils literal"><span class="pre">TestRunner</span></tt>
implementations can use this in a similar manner.</p>
</dd></dl>

<p>The following methods of the <a title="unittest.TestResult" class="reference internal" href="#unittest.TestResult"><tt class="xref docutils literal"><span class="pre">TestResult</span></tt></a> class are used to maintain
the internal data structures, and may be extended in subclasses to support
additional reporting requirements.  This is particularly useful in building
tools which support interactive reporting while tests are being run.</p>
<dl class="method">
<dt id="unittest.TestResult.startTest">
<tt class="descname">startTest</tt><big>(</big><em>test</em><big>)</big><a class="headerlink" href="#unittest.TestResult.startTest" title="Permalink to this definition">¶</a></dt>
<dd><p>Called when the test case <em>test</em> is about to be run.</p>
<p>The default implementation simply increments the instance&#8217;s <a title="unittest.TestResult.testsRun" class="reference internal" href="#unittest.TestResult.testsRun"><tt class="xref docutils literal"><span class="pre">testsRun</span></tt></a>
counter.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.stopTest">
<tt class="descname">stopTest</tt><big>(</big><em>test</em><big>)</big><a class="headerlink" href="#unittest.TestResult.stopTest" title="Permalink to this definition">¶</a></dt>
<dd><p>Called after the test case <em>test</em> has been executed, regardless of the
outcome.</p>
<p>The default implementation does nothing.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.startTestRun">
<tt class="descname">startTestRun</tt><big>(</big><em>test</em><big>)</big><a class="headerlink" href="#unittest.TestResult.startTestRun" title="Permalink to this definition">¶</a></dt>
<dd><p>Called once before any tests are executed.</p>
<p>
<span class="versionmodified">New in version 2.7.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.stopTestRun">
<tt class="descname">stopTestRun</tt><big>(</big><em>test</em><big>)</big><a class="headerlink" href="#unittest.TestResult.stopTestRun" title="Permalink to this definition">¶</a></dt>
<dd><p>Called once before any tests are executed.</p>
<p>
<span class="versionmodified">New in version 2.7.</span></p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.addError">
<tt class="descname">addError</tt><big>(</big><em>test</em>, <em>err</em><big>)</big><a class="headerlink" href="#unittest.TestResult.addError" title="Permalink to this definition">¶</a></dt>
<dd><p>Called when the test case <em>test</em> raises an unexpected exception <em>err</em> is a
tuple of the form returned by <a title="sys.exc_info" class="reference external" href="sys.html#sys.exc_info"><tt class="xref docutils literal"><span class="pre">sys.exc_info()</span></tt></a>: <tt class="docutils literal"><span class="pre">(type,</span> <span class="pre">value,</span>
<span class="pre">traceback)</span></tt>.</p>
<p>The default implementation appends a tuple <tt class="docutils literal"><span class="pre">(test,</span> <span class="pre">formatted_err)</span></tt> to
the instance&#8217;s <a title="unittest.TestResult.errors" class="reference internal" href="#unittest.TestResult.errors"><tt class="xref docutils literal"><span class="pre">errors</span></tt></a> attribute, where <em>formatted_err</em> is a
formatted traceback derived from <em>err</em>.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.addFailure">
<tt class="descname">addFailure</tt><big>(</big><em>test</em>, <em>err</em><big>)</big><a class="headerlink" href="#unittest.TestResult.addFailure" title="Permalink to this definition">¶</a></dt>
<dd><p>Called when the test case <em>test</em> signals a failure. <em>err</em> is a tuple of the form
returned by <a title="sys.exc_info" class="reference external" href="sys.html#sys.exc_info"><tt class="xref docutils literal"><span class="pre">sys.exc_info()</span></tt></a>:  <tt class="docutils literal"><span class="pre">(type,</span> <span class="pre">value,</span> <span class="pre">traceback)</span></tt>.</p>
<p>The default implementation appends a tuple <tt class="docutils literal"><span class="pre">(test,</span> <span class="pre">formatted_err)</span></tt> to
the instance&#8217;s <a title="unittest.TestResult.failures" class="reference internal" href="#unittest.TestResult.failures"><tt class="xref docutils literal"><span class="pre">failures</span></tt></a> attribute, where <em>formatted_err</em> is a
formatted traceback derived from <em>err</em>.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.addSuccess">
<tt class="descname">addSuccess</tt><big>(</big><em>test</em><big>)</big><a class="headerlink" href="#unittest.TestResult.addSuccess" title="Permalink to this definition">¶</a></dt>
<dd><p>Called when the test case <em>test</em> succeeds.</p>
<p>The default implementation does nothing.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.addSkip">
<tt class="descname">addSkip</tt><big>(</big><em>test</em>, <em>reason</em><big>)</big><a class="headerlink" href="#unittest.TestResult.addSkip" title="Permalink to this definition">¶</a></dt>
<dd><p>Called when the test case <em>test</em> is skipped.  <em>reason</em> is the reason the
test gave for skipping.</p>
<p>The default implementation appends a tuple <tt class="docutils literal"><span class="pre">(test,</span> <span class="pre">reason)</span></tt> to the
instance&#8217;s <a title="unittest.TestResult.skipped" class="reference internal" href="#unittest.TestResult.skipped"><tt class="xref docutils literal"><span class="pre">skipped</span></tt></a> attribute.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.addExpectedFailure">
<tt class="descname">addExpectedFailure</tt><big>(</big><em>test</em>, <em>err</em><big>)</big><a class="headerlink" href="#unittest.TestResult.addExpectedFailure" title="Permalink to this definition">¶</a></dt>
<dd><p>Called when the test case <em>test</em> fails, but was marked with the
<a title="unittest.expectedFailure" class="reference internal" href="#unittest.expectedFailure"><tt class="xref docutils literal"><span class="pre">expectedFailure()</span></tt></a> decorator.</p>
<p>The default implementation appends a tuple <tt class="docutils literal"><span class="pre">(test,</span> <span class="pre">formatted_err)</span></tt> to
the instance&#8217;s <a title="unittest.TestResult.expectedFailures" class="reference internal" href="#unittest.TestResult.expectedFailures"><tt class="xref docutils literal"><span class="pre">expectedFailures</span></tt></a> attribute, where <em>formatted_err</em>
is a formatted traceback derived from <em>err</em>.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.addUnexpectedSuccess">
<tt class="descname">addUnexpectedSuccess</tt><big>(</big><em>test</em><big>)</big><a class="headerlink" href="#unittest.TestResult.addUnexpectedSuccess" title="Permalink to this definition">¶</a></dt>
<dd><p>Called when the test case <em>test</em> was marked with the
<a title="unittest.expectedFailure" class="reference internal" href="#unittest.expectedFailure"><tt class="xref docutils literal"><span class="pre">expectedFailure()</span></tt></a> decorator, but succeeded.</p>
<p>The default implementation appends the test to the instance&#8217;s
<a title="unittest.TestResult.unexpectedSuccesses" class="reference internal" href="#unittest.TestResult.unexpectedSuccesses"><tt class="xref docutils literal"><span class="pre">unexpectedSuccesses</span></tt></a> attribute.</p>
</dd></dl>

</dd></dl>

<dl class="data">
<dt id="unittest.defaultTestLoader">
<tt class="descclassname">unittest.</tt><tt class="descname">defaultTestLoader</tt><a class="headerlink" href="#unittest.defaultTestLoader" title="Permalink to this definition">¶</a></dt>
<dd>Instance of the <a title="unittest.TestLoader" class="reference internal" href="#unittest.TestLoader"><tt class="xref docutils literal"><span class="pre">TestLoader</span></tt></a> class intended to be shared.  If no
customization of the <a title="unittest.TestLoader" class="reference internal" href="#unittest.TestLoader"><tt class="xref docutils literal"><span class="pre">TestLoader</span></tt></a> is needed, this instance can be used
instead of repeatedly creating new instances.</dd></dl>

<dl class="class">
<dt id="unittest.TextTestRunner">
<em class="property">
class </em><tt class="descclassname">unittest.</tt><tt class="descname">TextTestRunner</tt><big>(</big><span class="optional">[</span><em>stream</em><span class="optional">[</span>, <em>descriptions</em><span class="optional">[</span>, <em>verbosity</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TextTestRunner" title="Permalink to this definition">¶</a></dt>
<dd><p>A basic test runner implementation which prints results on standard error.  It
has a few configurable parameters, but is essentially very simple.  Graphical
applications which run test suites should provide alternate implementations.</p>
<dl class="method">
<dt id="unittest.TextTestRunner._makeResult">
<tt class="descname">_makeResult</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TextTestRunner._makeResult" title="Permalink to this definition">¶</a></dt>
<dd>This method returns the instance of <tt class="docutils literal"><span class="pre">TestResult</span></tt> used by <tt class="xref docutils literal"><span class="pre">run()</span></tt>.
It is not intended to be called directly, but can be overridden in
subclasses to provide a custom <tt class="docutils literal"><span class="pre">TestResult</span></tt>.</dd></dl>

</dd></dl>

<dl class="function">
<dt id="unittest.main">
<tt class="descclassname">unittest.</tt><tt class="descname">main</tt><big>(</big><span class="optional">[</span><em>module</em><span class="optional">[</span>, <em>defaultTest</em><span class="optional">[</span>, <em>argv</em><span class="optional">[</span>, <em>testRunner</em><span class="optional">[</span>, <em>testLoader</em><span class="optional">[</span>, <em>exit</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.main" title="Permalink to this definition">¶</a></dt>
<dd><p>A command-line program that runs a set of tests; this is primarily for making
test modules conveniently executable.  The simplest use for this function is to
include the following line at the end of a test script:</p>
<div class="highlight-python3"><pre>if __name__ == '__main__':
    unittest.main()</pre>
</div>
<p>The <em>testRunner</em> argument can either be a test runner class or an already
created instance of it. By default <tt class="docutils literal"><span class="pre">main</span></tt> calls <a title="sys.exit" class="reference external" href="sys.html#sys.exit"><tt class="xref docutils literal"><span class="pre">sys.exit()</span></tt></a> with
an exit code indicating success or failure of the tests run.</p>
<p><tt class="docutils literal"><span class="pre">main</span></tt> supports being used from the interactive interpreter by passing in the
argument <tt class="docutils literal"><span class="pre">exit=False</span></tt>. This displays the result on standard output without
calling <a title="sys.exit" class="reference external" href="sys.html#sys.exit"><tt class="xref docutils literal"><span class="pre">sys.exit()</span></tt></a>:</p>
<div class="highlight-python3"><div class="highlight"><pre><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>
<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="s">&#39;test_module&#39;</span><span class="p">,</span> <span class="nb">exit</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
</pre></div>
</div>
<p>Calling <tt class="docutils literal"><span class="pre">main</span></tt> actually returns an instance of the <tt class="docutils literal"><span class="pre">TestProgram</span></tt> class.
This stores the result of the tests run as the <tt class="docutils literal"><span class="pre">result</span></tt> attribute.</p>
<p>
<span class="versionmodified">Changed in version 2.7: </span>The <tt class="docutils literal"><span class="pre">exit</span></tt> parameter was added.</p>
</dd></dl>

</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">25.3. <tt class="docutils literal"><span class="pre">unittest</span></tt> &#8212; Unit testing framework</a><ul>
<li><a class="reference external" href="#basic-example">25.3.1. Basic example</a></li>
<li><a class="reference external" href="#organizing-test-code">25.3.2. Organizing test code</a></li>
<li><a class="reference external" href="#re-using-old-test-code">25.3.3. Re-using old test code</a></li>
<li><a class="reference external" href="#skipping-tests-and-expected-failures">25.3.4. Skipping tests and expected failures</a></li>
<li><a class="reference external" href="#classes-and-functions">25.3.5. Classes and functions</a><ul>
<li><a class="reference external" href="#test-cases">25.3.5.1. Test cases</a></li>
<li><a class="reference external" href="#grouping-tests">25.3.5.2. Grouping tests</a></li>
<li><a class="reference external" href="#loading-and-running-tests">25.3.5.3. Loading and running tests</a></li>
</ul>
</li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="doctest.html"
                                  title="previous chapter">25.2. <tt class="docutils literal docutils literal docutils literal"><span class="pre">doctest</span></tt> &#8212; Test interactive Python examples</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="2to3.html"
                                  title="next chapter">25.4. 2to3 - Automated Python 2 to 3 code translation</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/unittest.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="2to3.html" title="25.4. 2to3 - Automated Python 2 to 3 code translation"
             >next</a> |</li>
        <li class="right" >
          <a href="doctest.html" title="25.2. doctest — Test interactive Python examples"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.1.1 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="development.html" >25. Development Tools</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Aug 16, 2009.
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.2.
    </div>

  </body>
</html>