{"id":7473,"date":"2023-05-08T23:24:25","date_gmt":"2023-05-08T15:24:25","guid":{"rendered":"https:\/\/www.5x44.cn\/?p=7473"},"modified":"2023-05-09T22:40:02","modified_gmt":"2023-05-09T14:40:02","slug":"python%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%e6%a8%a1%e5%9d%97unittest%e7%ae%80%e5%8d%95%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=7473","title":{"rendered":"python\u5355\u5143\u6d4b\u8bd5\u6a21\u5757unittest\u7b80\u5355\u7528\u6cd5"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>\u5bfc\u5165unittest\u7c7b\uff0cimport unittest\u3001from unittest import TestCase<\/li>\n\n\n\n<li>\u5982\u679c\u9700\u8981\u6d4b\u8bd5\u952e\u76d8\u8f93\u5165\u548c\u8f93\u51fa\u9700\u8981\u5bfc\u5165mock\u7c7b\u7684patch\u88c5\u9970\u5668\u548cStringIO\uff0cfrom unittest.mock import patch\u3001from io import StringIO<\/li>\n\n\n\n<li>\u5efa\u7acb\u6d4b\u8bd5\u7c7bclass MyTestCase(TestCase)\uff0c\u8fd9\u91cc\u7684\u540d\u5b57\u968f\u610f\uff0c\u4f46\u662f\u4e00\u5b9a\u8981\u7ee7\u627f\u81eaTestCase\u3002<\/li>\n\n\n\n<li>\u5728\u8fd9\u4e2a\u7c7b\u91cc\u5efa\u7acb\u6d4b\u8bd5\u65b9\u6cd5def test_Function(self)<\/li>\n\n\n\n<li>\u6700\u540e\u901a\u8fc7unittest.main()\u8c03\u7528\u6240\u6709\u6d4b\u8bd5\u65b9\u6cd5<\/li>\n<\/ol>\n\n\n\n<p>\u6211\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u6765\u8bf4\u660e\u4f7f\u7528\u65b9\u6cd5:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import unittest\nimport test_First#\u8fd9\u662f\u6211\u81ea\u5df1\u5199\u7684\u4e00\u4e2a\u811a\u672c\u6587\u4ef6\nfrom unittest.mock import patch\nfrom io import StringIO\nfrom unittest import TestCase\n\nclass MyTestCase(TestCase):\n    def test_DNA_func_inner(self):\r\n        with open(r'test_data.txt','r') as f:\r\n            test_data_arr=f.readlines()\r\n\r        #\u4f7f\u7528test_data_arr\u5217\u8868\u4e2d\u7684\u6570\u636e\u66ff\u6362input()\uff0c\u6bcf\u4e2a\u8bb0\u5f55\u66ff\u6362\u4e00\u884c\u7684input()\u51fd\u6570\n        <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">with patch<\/mark><\/strong>('builtins.input', side_effect=test_data_arr) as mock_stdin:\r\n            <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\"><strong>with patch<\/strong><\/mark>('sys.stdout', new_callable=StringIO) as mock_stdout:\r\n                test_First.func1()\r\n                output=mock_stdout.getvalue()\r\n        self.assertEqual(output, '11674463 11674463\\n')\n\n    <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">@patch<\/mark>('builtins.input', side_effect=&#91;'6', 'a b c aa d b','1 2 3 4 5 6','3','1 5 caaab', '0 4 xyz', '2 4 bcdybc'])\n    <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">@patch<\/mark>('sys.stdout', new_callable=StringIO)\n    def test_DNA_func_essy1(self, mock_stdout,mock_stdin):\n    <strong>#\u8fd9\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u4e3a\u4ec0\u4e48\u9700\u89813\u4e2a\u53c2\u6570\uff0c\u800c\u4e0a\u9762\u7684\u53ea\u9700\u8981\u4e24\u4e2a\uff0c\u4e0d\u6e05\u695a\u4e3a\u4ec0\u4e48\uff0c\u53ef\u80fd\u662f\u56e0\u4e3a\u6bcf\u4e2apatch\u90fd\u4f1a\u4e3a\u65b9\u6cd5\u589e\u52a0\u4e00\u4e2a\u4f4d\u7f6e\u53c2\u6570\uff0c\u79bb\u65b9\u6cd5\u6700\u8fd1\u7684patch\u5bf9\u5e94\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u53c2\u6570<\/strong>\n        test_First.func()\n        output=mock_stdout.getvalue()\n        self.assertEqual(output, '0 19\\n')\nif __name__=='__main__':\n    unittest.main()<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u4e0d\u7528\u6d4b\u8bd5\u952e\u76d8\u8f93\u5165\u3001\u8f93\u51fa\u7684\u8bdd\uff0c\u5c31\u7b80\u5355\u5f88\u591a\uff0c\u76f4\u63a5\u5728\u7c7b\u4e2d\u5b9a\u4e49\u6d4b\u8bd5\u51fd\u6570\uff0c\u7136\u540e\u8c03\u7528\u81ea\u5df1\u7684\u51fd\u6570\u5c31\u53ef\u4ee5\u4e86\uff0c\u88ab\u6d4b\u51fd\u6570\u4f7f\u7528\u53c2\u6570\uff0c\u5728\u51fd\u6570\u91cc\u7ed9\u4e88\u5c31\u53ef\u4ee5\u4e86\uff0c\u7701\u53bb\u4e86\u5d4c\u5957\u65b9\u6cd5\u548c\u6a21\u62df\u8f93\u5165\u3001\u8f93\u51fa\u7684\u6b65\u9aa4\u3002<\/p>\n\n\n\n<p>\u5173\u4e8e\u6a21\u62df\u5bf9\u8c61\u5e93mock\u7684\u8be6\u7ec6\u5185\u5bb9\u8fd8\u662f\u8981\u770b\u5b98\u65b9\u6587\u6863\uff1a<\/p>\n\n\n\n<p>mock\u7c7b\u7684\u8be6\u7ec6\u4f7f\u7528\u65b9\u6cd5\uff1a<a href=\"https:\/\/docs.python.org\/3\/library\/unittest.mock.html\">https:\/\/docs.python.org\/3\/library\/unittest.mock.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u6765\u8bf4\u660e\u4f7f\u7528\u65b9\u6cd5: \u5982\u679c\u4e0d\u7528\u6d4b\u8bd5\u952e\u76d8\u8f93\u5165\u3001\u8f93\u51fa\u7684\u8bdd\uff0c\u5c31\u7b80\u5355\u5f88\u591a\uff0c\u76f4\u63a5\u5728\u7c7b\u4e2d\u5b9a\u4e49\u6d4b\u8bd5\u51fd\u6570\uff0c\u7136\u540e\u8c03\u7528\u81ea\u5df1\u7684\u51fd\u6570\u5c31\u53ef\u4ee5\u4e86\uff0c\u88ab\u6d4b\u51fd\u6570\u4f7f\u7528\u53c2\u6570\uff0c\u5728\u51fd\u6570\u91cc\u7ed9&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=7473\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-7473","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7473"}],"version-history":[{"count":5,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7473\/revisions"}],"predecessor-version":[{"id":7480,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7473\/revisions\/7480"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}