{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# はじめてのpytest"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "サンプルコードには {ref}`test_sample` が含まれています。\n",
    "\n",
    "```{literalinclude} ./tests/test_sample.py\n",
    ":name: test_sample\n",
    ":caption: テストのサンプルコード\n",
    ":language: python\n",
    "```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`pytest` コマンドでテストを実行します。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m============================= test session starts ==============================\u001b[0m\n",
      "platform linux -- Python 3.10.8, pytest-7.2.1, pluggy-1.0.0\n",
      "rootdir: /home/driller/repo/pytest-hands-on/docs\n",
      "plugins: anyio-3.6.2\n",
      "collected 1 item\n",
      "\n",
      "tests/test_sample.py \u001b[32m.\u001b[0m\u001b[32m                                                   [100%]\u001b[0m\n",
      "\n",
      "\u001b[32m============================== \u001b[32m\u001b[1m1 passed\u001b[0m\u001b[32m in 0.19s\u001b[0m\u001b[32m ===============================\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "\n",
    "pytest"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`-vv` フラグを使うとテストに失敗した場合に、どこで不一致が起きているかを確認できます。敢えて失敗するテストのコードに変更し、失敗箇所を確認してみましょう。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m============================= test session starts ==============================\u001b[0m\n",
      "platform linux -- Python 3.10.8, pytest-7.2.1, pluggy-1.0.0 -- /home/driller/repo/pytest-hands-on/.venv/bin/python\n",
      "cachedir: .pytest_cache\n",
      "rootdir: /home/driller/repo/pytest-hands-on/docs\n",
      "plugins: anyio-3.6.2\n",
      "\u001b[1mcollecting ... \u001b[0mcollected 1 item\n",
      "\n",
      "tests/test_sample.py::test_results \u001b[32mPASSED\u001b[0m\u001b[32m                                [100%]\u001b[0m\n",
      "\n",
      "\u001b[32m============================== \u001b[32m\u001b[1m1 passed\u001b[0m\u001b[32m in 0.20s\u001b[0m\u001b[32m ===============================\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "\n",
    "pytest -vv"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "特定の関数だけをテストする場合は次のように `モジュール名::関数名` で記述して実行します。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m============================= test session starts ==============================\u001b[0m\n",
      "platform linux -- Python 3.10.8, pytest-7.2.1, pluggy-1.0.0\n",
      "rootdir: /home/driller/repo/pytest-hands-on/docs\n",
      "plugins: anyio-3.6.2\n",
      "collected 1 item\n",
      "\n",
      "tests/test_sample.py \u001b[32m.\u001b[0m\u001b[32m                                                   [100%]\u001b[0m\n",
      "\n",
      "\u001b[32m============================== \u001b[32m\u001b[1m1 passed\u001b[0m\u001b[32m in 0.19s\u001b[0m\u001b[32m ===============================\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "\n",
    "pytest tests/test_sample.py::test_results"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "be84b01a73632845050dad9804d3f2cd1400ce2d098b570f9c38f629644dc27f"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
