{"id":47643,"date":"2026-11-23T22:06:11","date_gmt":"2026-11-23T22:06:11","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=47643"},"modified":"2026-11-23T22:06:11","modified_gmt":"2026-11-23T22:06:11","slug":"complete-guide-to-pytest-from-basics-to-advanced","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/complete-guide-to-pytest-from-basics-to-advanced\/","title":{"rendered":"Complete Guide to Pytest: From Basics to Advanced"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Pytest is the most widely used Python testing framework. You write tests as plain functions that use the <code>assert<\/code> keyword, and pytest adds fixtures, parametrization, markers and a large plugin ecosystem on top. This guide runs from installation to parallel runs, coverage and CI. Below the original walkthrough you will find a project layout that works when you copy it, corrected mocking and fixture examples, and fixes for the errors people hit most often.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/c0b260e7b845dff98556511071d0bf7c.js\"><\/script>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">A project layout that imports correctly<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The example above imports with <code>from src.my_code import add<\/code>. If you have no extra configuration and run plain <code>pytest<\/code>, that import usually fails with <code>ModuleNotFoundError: No module named &#039;src&#039;<\/code>. In its default import mode, pytest puts the test file&#039;s directory (<code>tests\/<\/code>) on <code>sys.path<\/code>, not the project root. The fix is to tell pytest where the source code lives. You can do this with the <code>pythonpath<\/code> ini option, which pytest has supported since version 7.0.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">my_project\/\n\u251c\u2500\u2500 pytest.ini\n\u251c\u2500\u2500 src\/\n\u2502   \u2514\u2500\u2500 my_code.py\n\u2514\u2500\u2500 tests\/\n    \u251c\u2500\u2500 conftest.py\n    \u2514\u2500\u2500 test_my_code.py<\/code><\/span><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">&#91;pytest]\ntestpaths = tests\npythonpath = src\naddopts = -ra --strict-markers\nmarkers =\n    slow: marks tests <span class=\"hljs-keyword\">as<\/span> slow (deselect <span class=\"hljs-keyword\">with<\/span> -m <span class=\"hljs-string\">\"not slow\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">With <code>pythonpath = src<\/code>, tests import modules directly: <code>from my_code import add<\/code>. <code>testpaths<\/code> limits discovery to <code>tests\/<\/code>. <code>-ra<\/code> prints a short summary of every test that did not pass. <code>--strict-markers<\/code> turns a misspelled marker into an error, so it cannot silently select nothing. If you would rather use <code>pyproject.toml<\/code>, the same settings go under <code>[tool.pytest.ini_options]<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">&#91;tool.pytest.ini_options]\ntestpaths = &#91;<span class=\"hljs-string\">\"tests\"<\/span>]\npythonpath = &#91;<span class=\"hljs-string\">\"src\"<\/span>]\naddopts = <span class=\"hljs-string\">\"-ra --strict-markers\"<\/span>\nmarkers = &#91;<span class=\"hljs-string\">\"slow: marks tests as slow\"<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Install pytest and its plugins inside a virtual environment (<code>python -m venv .venv<\/code> and then activate it), so that the project&#039;s versions do not clash with system packages. Running <code>python -m pytest<\/code> instead of <code>pytest<\/code> also puts the current directory on <code>sys.path<\/code> and makes sure you use the interpreter from your venv.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A successful first run looks like this (the header lines are trimmed):<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ pytest\n============================= test session starts ==============================\ncollected 1 item\n\ntests\/test_my_code.py .                                                  &#91;100%]\n\n============================== 1 passed in 0.01s ===============================<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Assertions beyond equality<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Pytest rewrites <code>assert<\/code> statements, so a failure shows the actual values on both sides. Two helpers cover cases that a plain <code>assert<\/code> handles badly: checking that an exception is raised, and comparing floating-point numbers.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">import pytest\n\n\ndef divide(a, b):\n    <span class=\"hljs-keyword\">return<\/span> a \/ b\n\n\ndef test_divide_by_zero_raises():\n    <span class=\"hljs-comment\"># match is a regex searched against str(exception)<\/span>\n    with pytest.raises(ZeroDivisionError, match=<span class=\"hljs-string\">\"division by zero\"<\/span>):\n        divide(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">0<\/span>)\n\n\ndef test_float_sum():\n    <span class=\"hljs-comment\"># 0.1 + 0.2 == 0.3 is False in Python; approx allows a small tolerance<\/span>\n    assert <span class=\"hljs-number\">0.1<\/span> + <span class=\"hljs-number\">0.2<\/span> == pytest.approx(<span class=\"hljs-number\">0.3<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Fixtures in depth: conftest.py, scope and teardown<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Put fixtures that several test files share in <code>tests\/conftest.py<\/code>. Pytest loads that file automatically, and you never import it. The code before <code>yield<\/code> is setup and the code after it is teardown. The teardown runs even when the test fails. Here is a complete, runnable version of the database fixture from the example above, using SQLite from the standard library:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># tests\/conftest.py<\/span>\nimport sqlite3\n\nimport pytest\n\n\n@pytest.fixture(scope=<span class=\"hljs-string\">\"module\"<\/span>)\ndef db_connection():\n    conn = sqlite3.connect(<span class=\"hljs-string\">\":memory:\"<\/span>)\n    conn.execute(<span class=\"hljs-string\">\"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)\"<\/span>)\n    <span class=\"hljs-keyword\">yield<\/span> conn          <span class=\"hljs-comment\"># tests in one module share this connection<\/span>\n    conn.close()        <span class=\"hljs-comment\"># teardown runs after the last test in the module<\/span>\n\n\n<span class=\"hljs-comment\"># tests\/test_db.py<\/span>\ndef test_insert_user(db_connection):\n    db_connection.execute(<span class=\"hljs-string\">\"INSERT INTO users (name) VALUES (?)\"<\/span>, (<span class=\"hljs-string\">\"asha\"<\/span>,))\n    row = db_connection.execute(<span class=\"hljs-string\">\"SELECT name FROM users\"<\/span>).fetchone()\n    assert row == (<span class=\"hljs-string\">\"asha\"<\/span>,)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Scopes:<\/strong> <code>function<\/code> (the default) creates a new fixture for every test. <code>class<\/code>, <code>module<\/code>, <code>package<\/code> and <code>session<\/code> reuse it for longer. Use a wider scope only for expensive resources, because shared state can make tests depend on each other.<\/li>\n\n\n<li><strong><code>tmp_path<\/code><\/strong> gives each test its own temporary directory as a <code>pathlib.Path<\/code>.<\/li>\n\n\n<li><strong><code>monkeypatch<\/code><\/strong> sets environment variables or attributes and undoes the change automatically after the test.<\/li>\n\n\n<li><strong><code>capsys<\/code><\/strong> captures stdout and stderr, so you can assert on printed output.<\/li>\n\n\n<li>Run <code>pytest --fixtures<\/code> to list every fixture available in the project, including the ones plugins provide.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Mocking that actually tests your code<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The mocking example above patches <code>src.my_code.external_function<\/code>, which does not exist in <code>my_code.py<\/code>, so <code>mocker.patch<\/code> raises <code>AttributeError<\/code>. Even if the patch worked, the test only calls the mock itself and never runs any real code. The <code>mocker<\/code> fixture also needs <code>pip install pytest-mock<\/code>. Below is a corrected example that patches a dependency where it is used and then checks how your function called it.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># src\/my_code.py<\/span>\nimport os\n\nimport requests\n\n\ndef get_user_name(user_id):\n    resp = requests.get(f<span class=\"hljs-string\">\"https:\/\/api.example.com\/users\/{user_id}\"<\/span>, timeout=<span class=\"hljs-number\">5<\/span>)\n    resp.raise_for_status()\n    <span class=\"hljs-keyword\">return<\/span> resp.json()&#91;<span class=\"hljs-string\">\"name\"<\/span>]\n\n\ndef get_env():\n    <span class=\"hljs-keyword\">return<\/span> os.environ.get(<span class=\"hljs-string\">\"APP_ENV\"<\/span>, <span class=\"hljs-string\">\"dev\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># tests\/test_mocking.py<\/span>\nfrom my_code import get_env, get_user_name\n\n\ndef test_get_user_name(mocker):\n    fake_resp = mocker.Mock()\n    fake_resp.json.return_value = {<span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"Asha\"<\/span>}\n    <span class=\"hljs-comment\"># Patch requests.get as seen from my_code, not where it is defined<\/span>\n    mock_get = mocker.patch(<span class=\"hljs-string\">\"my_code.requests.get\"<\/span>, return_value=fake_resp)\n\n    assert get_user_name(<span class=\"hljs-number\">7<\/span>) == <span class=\"hljs-string\">\"Asha\"<\/span>\n    mock_get.assert_called_once_with(<span class=\"hljs-string\">\"https:\/\/api.example.com\/users\/7\"<\/span>, timeout=<span class=\"hljs-number\">5<\/span>)\n\n\ndef test_get_env(monkeypatch):\n    <span class=\"hljs-comment\"># Built-in fixture, no plugin needed<\/span>\n    monkeypatch.setenv(<span class=\"hljs-string\">\"APP_ENV\"<\/span>, <span class=\"hljs-string\">\"prod\"<\/span>)\n    assert get_env() == <span class=\"hljs-string\">\"prod\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Patch the name in the namespace where your code looks it up. If <code>my_code.py<\/code> used <code>from requests import get<\/code>, the patch target would be <code>my_code.get<\/code>, not <code>requests.get<\/code>. For more on making HTTP calls, see <a href=\"https:\/\/www.devopsschool.com\/blog\/how-to-send-a-post-request-with-python\/\">How to Send a POST Request with Python<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parametrize IDs, skip and xfail<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can wrap parametrized cases in <code>pytest.param<\/code> to give them readable IDs and to attach markers to a single case. Use <code>skipif<\/code> for tests that cannot run in a given environment. Use <code>xfail<\/code> for known failures that should not break the build.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> sys\n\n<span class=\"hljs-keyword\">import<\/span> pytest\n<span class=\"hljs-keyword\">from<\/span> my_code <span class=\"hljs-keyword\">import<\/span> add\n\n\n@pytest.mark.parametrize(\n    (<span class=\"hljs-string\">\"a\"<\/span>, <span class=\"hljs-string\">\"b\"<\/span>, <span class=\"hljs-string\">\"expected\"<\/span>),\n    &#91;\n        pytest.param(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, id=<span class=\"hljs-string\">\"positive\"<\/span>),\n        pytest.param(<span class=\"hljs-number\">-1<\/span>, <span class=\"hljs-number\">-1<\/span>, <span class=\"hljs-number\">-2<\/span>, id=<span class=\"hljs-string\">\"negative\"<\/span>),\n        pytest.param(<span class=\"hljs-number\">0.1<\/span>, <span class=\"hljs-number\">0.2<\/span>, <span class=\"hljs-number\">0.3<\/span>, id=<span class=\"hljs-string\">\"float-exact\"<\/span>,\n                     marks=pytest.mark.xfail(reason=<span class=\"hljs-string\">\"float rounding\"<\/span>)),\n    ],\n)\ndef test_add(a, b, expected):\n    assert add(a, b) == expected\n\n\n@pytest.mark.skipif(sys.platform == <span class=\"hljs-string\">\"win32\"<\/span>, reason=<span class=\"hljs-string\">\"POSIX-only behaviour\"<\/span>)\ndef test_posix_paths():\n    assert <span class=\"hljs-string\">\"\/\"<\/span>.join(&#91;<span class=\"hljs-string\">\"a\"<\/span>, <span class=\"hljs-string\">\"b\"<\/span>]) == <span class=\"hljs-string\">\"a\/b\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Run <code>pytest -v -k positive<\/code> to run only the case with the <code>positive<\/code> ID. <code>-k<\/code> matches substrings of test names and IDs, and it accepts <code>and<\/code>, <code>or<\/code> and <code>not<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Command-line options worth knowing<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>What it does<\/th><\/tr><\/thead><tbody><tr><td><code>pytest -k &quot;add and not float&quot;<\/code><\/td><td>Selects tests by name expression<\/td><\/tr><tr><td><code>pytest -m &quot;not slow&quot;<\/code><\/td><td>Deselects tests that have the <code>slow<\/code> marker<\/td><\/tr><tr><td><code>pytest --lf<\/code> \/ <code>pytest --ff<\/code><\/td><td>Reruns only the last failures, or runs them first<\/td><\/tr><tr><td><code>pytest -x<\/code> \/ <code>pytest --maxfail=3<\/code><\/td><td>Stops after the first failure, or after N failures<\/td><\/tr><tr><td><code>pytest -s<\/code><\/td><td>Turns off output capture so <code>print()<\/code> output shows<\/td><\/tr><tr><td><code>pytest --durations=10<\/code><\/td><td>Lists the 10 slowest tests<\/td><\/tr><tr><td><code>pytest --collect-only -q<\/code><\/td><td>Lists the tests it collected without running them<\/td><\/tr><tr><td><code>pytest --junitxml=report.xml<\/code><\/td><td>Writes a JUnit XML report for CI systems (built in)<\/td><\/tr><tr><td><code>pytest -n auto<\/code><\/td><td>Runs tests in parallel, one worker per CPU (needs <code>pytest-xdist<\/code>)<\/td><\/tr><tr><td><code>pytest --cov=src --cov-report=term-missing<\/code><\/td><td>Shows coverage, including line numbers that were not run (needs <code>pytest-cov<\/code>)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Pytest&#039;s exit codes matter in CI: <code>0<\/code> means all tests passed, <code>1<\/code> means some tests failed, <code>2<\/code> means the run was interrupted, <code>3<\/code> means an internal error, <code>4<\/code> means a command-line usage error, and <code>5<\/code> means no tests were collected.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Running pytest in CI<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The example above recommends CI but does not show it. This minimal GitHub Actions workflow runs the suite in parallel, writes a JUnit report and fails the build if coverage drops below 80%:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">name: tests\n<span class=\"hljs-attr\">on<\/span>: &#91;push, pull_request]\n<span class=\"hljs-attr\">jobs<\/span>:\n  test:\n    runs-on: ubuntu-latest\n    <span class=\"hljs-attr\">steps<\/span>:\n      - uses: actions\/checkout@v4\n      - uses: actions\/setup-python@v5\n        <span class=\"hljs-attr\">with<\/span>:\n          python-version: <span class=\"hljs-string\">\"3.12\"<\/span>\n      - run: pip install pytest pytest-cov pytest-xdist pytest-mock requests\n      - run: pytest -n auto --cov=src --cov-report=xml --cov-fail-under=<span class=\"hljs-number\">80<\/span> --junitxml=report.xml<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Pin your test dependencies in a <code>requirements-dev.txt<\/code> file or in the dev extras of <code>pyproject.toml<\/code>. That way CI and local runs use the same plugin versions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td><code>ModuleNotFoundError: No module named &#039;src&#039;<\/code><\/td><td>The project root is not on <code>sys.path<\/code><\/td><td>Set <code>pythonpath = src<\/code> and import <code>my_code<\/code>, or install the package with <code>pip install -e .<\/code><\/td><\/tr><tr><td><code>error: unrecognized arguments: --html=report.html<\/code><\/td><td><code>pytest-html<\/code> is not installed<\/td><td><code>pip install pytest-html<\/code><\/td><\/tr><tr><td><code>fixture &#039;mocker&#039; not found<\/code><\/td><td><code>pytest-mock<\/code> is not installed<\/td><td><code>pip install pytest-mock<\/code>, or use the built-in <code>monkeypatch<\/code><\/td><\/tr><tr><td><code>PytestUnknownMarkWarning<\/code> or <code>&#039;slow&#039; not found in markers<\/code><\/td><td>The marker is not registered<\/td><td>Add it under <code>markers<\/code> in <code>pytest.ini<\/code><\/td><\/tr><tr><td><code>collected 0 items<\/code> (exit code 5)<\/td><td>File or function names do not match <code>test_*.py<\/code>, <code>*_test.py<\/code> and <code>test*<\/code><\/td><td>Rename the files and functions, or check <code>testpaths<\/code><\/td><\/tr><tr><td><code>import file mismatch<\/code><\/td><td>Two test files share a basename in different folders that have no <code>__init__.py<\/code><\/td><td>Add <code>__init__.py<\/code> files, or use <code>--import-mode=importlib<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Corrections to the original guide<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>from src.my_code import add<\/code> fails with plain <code>pytest<\/code> and no configuration. Configure <code>pythonpath = src<\/code> and import <code>from my_code import add<\/code>.<\/li>\n\n\n<li><code>pytest --html=report.html<\/code> is shown under built-in options, but it only works after <code>pip install pytest-html<\/code>.<\/li>\n\n\n<li>The mocking example patched a function that does not exist, imported <code>MagicMock<\/code> without using it, and tested only the mock. A corrected version is shown above.<\/li>\n\n\n<li>The <code>db_connection<\/code> fixture had no <code>import pytest<\/code> and called an undefined <code>setup_database_connection()<\/code>. A runnable SQLite version is shown above.<\/li>\n\n\n<li>Mocking is not built into pytest itself. The built-in option is <code>monkeypatch<\/code>, and <code>mocker<\/code> comes from the <code>pytest-mock<\/code> plugin.<\/li>\n\n\n<li>The <code>slow<\/code> marker should be registered before you use <code>pytest -m slow<\/code>. Otherwise pytest warns, or errors when <code>--strict-markers<\/code> is on.<\/li>\n\n\n<li>The closing line &quot;Let me know if you&#039;d like detailed examples&#8230;&quot; is leftover chat text and is not part of the guide.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between pytest and unittest?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>unittest<\/code> is in the standard library and uses classes and methods such as <code>self.assertEqual<\/code>. Pytest uses plain functions with <code>assert<\/code>, and adds fixtures, parametrization and plugins. Pytest can also run existing <code>unittest<\/code> tests without changes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Where should I put shared fixtures in pytest?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Put them in a <code>conftest.py<\/code> file. Pytest loads it automatically, and its fixtures are available to every test in that directory and its subdirectories, with no import needed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I run a single test in pytest?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Pass its node ID, for example <code>pytest tests\/test_my_code.py::test_add<\/code>. For a single parametrized case, add the ID in brackets, such as <code>pytest &quot;tests\/test_my_code.py::test_add[positive]&quot;<\/code>. You can also select tests by name with <code>-k<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I see print output in pytest?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Pytest captures output by default and shows it only for failing tests. Run with <code>-s<\/code> to turn off capture, or use the <code>capsys<\/code> fixture to assert on the output instead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Should I use pytest.ini or pyproject.toml?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Either one works. <code>pyproject.toml<\/code> with <code>[tool.pytest.ini_options]<\/code> keeps all project configuration in one file. If a <code>pytest.ini<\/code> file exists, it takes precedence, even when it is empty.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/programming\/python-programming-scripting-languages\/\">More Python tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/how-to-send-a-post-request-with-python\/\">How to Send a POST Request with Python<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/top-50-python-interview-questions-and-answers\/\">Top 50 Python Interview Questions and Answers<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/what-is-flask-and-use-cases-of-flask\/\">What is Flask and use cases of Flask?<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Pytest is the most widely used Python testing framework. You write tests as plain functions that use the assert keyword, and pytest adds fixtures, parametrization, markers and&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[499],"tags":[4803,7504],"class_list":["post-47643","post","type-post","status-publish","format-standard","hentry","category-python-programming-scripting-languages","tag-test-automation","tag-unit-testing"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=47643"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47643\/revisions"}],"predecessor-version":[{"id":80174,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47643\/revisions\/80174"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=47643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=47643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=47643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}