{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## 导入依赖" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from unittest.mock import patch\n", "\n", "from module import call_func" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 错误的用法" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def test_call_func():\n", " def mocked_funny_func():\n", " return \"not funny at all\"\n", "\n", " with patch(\"dependency.some_funny_func\", mocked_funny_func):\n", " return_value = call_func()\n", "\n", " assert return_value == \"not funny at all\"\n", " print(\"pass\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "scrolled": true }, "outputs": [ { "ename": "AssertionError", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtest_call_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mtest_call_func\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mreturn_value\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcall_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mreturn_value\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"not funny at all\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAssertionError\u001b[0m: " ] } ], "source": [ "test_call_func()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 正确的用法" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def test_call_func():\n", " def mocked_funny_func():\n", " return \"not funny at all\"\n", "\n", " with patch(\"module.some_funny_func\", mocked_funny_func):\n", " return_value = call_func()\n", "\n", " assert return_value == \"not funny at all\"\n", " print(\"pass\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pass\n" ] } ], "source": [ "test_call_func()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 解析原理" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "some_other_value\n", "some_value\n" ] } ], "source": [ "# 模拟 dependency.some_funny_func\n", "dependency = {}\n", "dependency[\"some_funny_func\"] = \"some_value\"\n", "\n", "# 模拟 module.some_funny_func\n", "module = {}\n", "module[\"some_funny_func\"] = dependency[\"some_funny_func\"]\n", "\n", "# 模拟 mock\n", "dependency[\"some_funny_func\"] = \"some_other_value\"\n", "\n", "# 查看结果\n", "print(dependency[\"some_funny_func\"])\n", "print(module[\"some_funny_func\"])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }