<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>pytest documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="_static/flask.css" />
<link rel="stylesheet" type="text/css" href="_static/pygments_pytest.css" />
<link rel="stylesheet" type="text/css" href="_static/_static/css/badge_only.css" />
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/_sphinx_javascript_frameworks_compat.js"></script>
<script src="_static/doctools.js"></script>
<script src="_static/sphinx_highlight.js"></script>
<link rel="canonical" href="https://docs.pytest.org/en/7.2.x/contents.html" />
<link rel="shortcut icon" href="_static/favicon.png"/>
<link rel="search" title="Search" href="search.html" />
<script>DOCUMENTATION_OPTIONS.URL_ROOT = '';</script>
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="nav-item nav-item-0"><a href="#">pytest-7.2</a> »</li>
<li class="nav-item nav-item-this"><a href="">pytest documentation</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="full-pytest-documentation">
<span id="toc"></span><h1>Full pytest documentation<a class="headerlink" href="#full-pytest-documentation" title="Permalink to this heading">¶</a></h1>
<p><a class="reference external" href="https://media.readthedocs.org/pdf/pytest/latest/pytest.pdf">Download latest version as PDF</a></p>
<section id="start-here">
<h2>Start here<a class="headerlink" href="#start-here" title="Permalink to this heading">¶</a></h2>
<div class="toctree-wrapper compound">
<span id="document-getting-started"></span><section id="get-started">
<span id="id1"></span><h3>Get Started<a class="headerlink" href="#get-started" title="Permalink to this heading">¶</a></h3>
<section id="install-pytest">
<span id="installation"></span><span id="getstarted"></span><h4>Install <code class="docutils literal notranslate"><span class="pre">pytest</span></code><a class="headerlink" href="#install-pytest" title="Permalink to this heading">¶</a></h4>
<p><code class="docutils literal notranslate"><span class="pre">pytest</span></code> requires: Python 3.7+ or PyPy3.</p>
<ol class="arabic simple">
<li><p>Run the following command in your command line:</p></li>
</ol>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>pip<span class="w"> </span>install<span class="w"> </span>-U<span class="w"> </span>pytest
</pre></div>
</div>
<ol class="arabic simple" start="2">
<li><p>Check that you installed the correct version:</p></li>
</ol>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>pytest<span class="w"> </span>--version
pytest<span class="w"> </span><span class="m">7</span>.2.2
</pre></div>
</div>
</section>
<section id="create-your-first-test">
<span id="simpletest"></span><h4>Create your first test<a class="headerlink" href="#create-your-first-test" title="Permalink to this heading">¶</a></h4>
<p>Create a new file called <code class="docutils literal notranslate"><span class="pre">test_sample.py</span></code>, containing a function, and a test:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># content of test_sample.py</span>
<span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="k">def</span> <span class="nf">test_answer</span><span class="p">():</span>
<span class="k">assert</span> <span class="n">func</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="o">==</span> <span class="mi">5</span>
</pre></div>
</div>
<p>The test</p>
<div class="highlight-pytest notranslate"><div class="highlight"><pre><span></span>$ pytest
<span class=" -Color -Color-Bold">=========================== test session starts ============================</span>
platform linux -- Python 3.x.y, pytest-7.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
collected 1 item
test_sample.py <span class=" -Color -Color-Red">F</span> <span class=" -Color -Color-Red">[100%]</span>
================================= FAILURES =================================
<span class=" -Color -Color-Bold -Color-Bold-Red">_______________________________ test_answer ________________________________</span>
def test_answer():
> assert func(3) == 5
<span class=" -Color -Color-Bold -Color-Bold-Red">E assert 4 == 5</span>
<span class=" -Color -Color-Bold -Color-Bold-Red">E + where 4 = func(3)</span>
<span class=" -Color -Color-Bold -Color-Bold-Red">test_sample.py</span>:6: AssertionError
<span class=" -Color -Color-Bold -Color-Bold-Cyan">========================= short test summary info ==========================</span>
<span class=" -Color -Color-Red">FAILED</span> test_sample.py::<span class=" -Color -Color-Bold">test_answer</span> - assert 4 == 5
<span class=" -Color -Color-Red">============================ </span><span class=" -Color -Color-Bold -Color-Bold-Red">1 failed</span><span class=" -Color -Color-Red"> in 0.12s =============================</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">[100%]</span></code> refers to the overall progress of running all test cases. After it finishes, pytest then shows a failure report because <code class="docutils literal notranslate"><span class="pre">func(3)</span></code> does not return <code class="docutils literal notranslate"><span class="pre">5</span></code>.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>You can use the <code class="docutils literal notranslate"><span class="pre">assert</span></code> statement to verify test expectations. pytest’s <a class="reference external" href="https://docs.python.org/3/reference/simple_stmts.html#assert" title="(in Python v3.11)"><span class="xref std std-ref">Advanced assertion introspection</span></a> will intelligently report intermediate values of the assert expression so you can avoid the many names <a class="reference external" href="https://docs.python.org/3/library/unittest.html#testcase-objects" title="(in Python v3.11)"><span class="xref std std-ref">of JUnit legacy methods</span></a>.</p>
</div>
</section>
<section id="run-multiple-tests">
<h4>Run multiple tests<a class="headerlink" href="#run-multiple-tests" title="Permalink to this heading">¶</a></h4>
<p><code class="docutils literal notranslate"><span class="pre">pytest</span></code> will run all files of the form test_*.py or *_test.py in the current directory and its subdirectories. More generally, it follows <a class="reference internal" href="contents.html#test-discovery"><span class="std std-ref">standard test discovery rules</span></a>.</p>
</section>
<section id="assert-that-a-certain-exception-is-raised">
<h4>Assert that a certain exception is raised<a class="headerlink" href="#assert-that-a-certain-exception-is-raised" title="Permalink to this heading">¶</a></h4>
<p>Use the <a class="reference internal" href="contents.html#assertraises"><span class="std std-ref">raises</span></a> helper to assert that some code raises an exception:</p>
<div class="high