{"id":6023,"date":"2020-07-23T08:40:18","date_gmt":"2020-07-22T23:40:18","guid":{"rendered":"http:\/\/blog.jansnap.com\/?p=6023"},"modified":"2021-12-17T22:45:12","modified_gmt":"2021-12-17T13:45:12","slug":"python%e3%81%ae%e7%b5%84%e3%81%bf%e8%be%bc%e3%81%bf%e9%96%a2%e6%95%b0open%e3%81%ae%e3%83%a2%e3%83%83%e3%82%af%e3%82%92%e4%bd%9c%e3%81%a3%e3%81%a6%e5%8d%98%e4%bd%93%e3%83%86%e3%82%b9%e3%83%88%e3%81%99","status":"publish","type":"post","link":"https:\/\/blog.jansnap.com\/?p=6023","title":{"rendered":"Python\u306e\u7d44\u307f\u8fbc\u307f\u95a2\u6570open\u306e\u30e2\u30c3\u30af\u3092\u4f5c\u3063\u3066\u5358\u4f53\u30c6\u30b9\u30c8\u3059\u308b"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%B5%84%E3%81%BF%E8%BE%BC%E3%81%BF%E9%96%A2%E6%95%B0%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E3%81%84%E3%82%8B%E3%83%A1%E3%82%BD%E3%83%83%E3%83%89%E3%81%AE%E5%8D%98%E4%BD%93%E3%83%86%E3%82%B9%E3%83%88%E3%82%92%E8%A1%8C%E3%81%86\"><\/span>\n\u7d44\u307f\u8fbc\u307f\u95a2\u6570\u3092\u4f7f\u3063\u3066\u3044\u308b\u30e1\u30bd\u30c3\u30c9\u306e\u5358\u4f53\u30c6\u30b9\u30c8\u3092\u884c\u3046<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5b9f\u30d5\u30a1\u30a4\u30eb\u3092\u4f7f\u308f\u305a\u306b\u30c6\u30b9\u30c8\u3057\u305f\u3044\u5834\u5408\u3001\u30d5\u30a1\u30a4\u30ebopen\u30e1\u30bd\u30c3\u30c9\u306e\u30b9\u30bf\u30d6(\u30e2\u30c3\u30af)\u3092\u4f5c\u3063\u3066\u30c6\u30b9\u30c8\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\n<code>unittest<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f7f\u3046<\/li><li>\n<code>mock_open<\/code>\u3068\u3044\u3046\u4fbf\u5229\u306a\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308b<\/li><li>\n<code>open<\/code>\u306e\u3088\u3046\u306a<code>\u7d44\u307f\u8fbc\u307f\u95a2\u6570<\/code>\u306e\u30e2\u30c3\u30af\u3092\u4f5c\u6210\u3059\u308b\u5834\u5408\u306f\u3001\u540d\u524d\u306b<code>builtins<\/code>\u3092\u4f7f\u3044\u3001<code>builtins.open<\/code>\u306e\u3088\u3046\u306b\u66f8\u304f<\/li><li>\n<code>mock_open(read_data='str1\u00a5nstr2\u00a5n')<\/code>\u306e\u3088\u3046\u306b\u3001\u30e2\u30c3\u30af\u306e\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u8aad\u307f\u51fa\u3059\u30c7\u30fc\u30bf\u3092\u6307\u5b9a\u3067\u304d\u308b<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E3%82%B3%E3%83%BC%E3%83%89\"><\/span>\n\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E3%83%86%E3%82%B9%E3%83%88%E3%81%97%E3%81%9F%E3%81%84%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%80%82%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%AE%E8%AA%AD%E3%81%BF%E6%9B%B8%E3%81%8D%E3%82%92%E8%A1%8C%E3%81%A3%E3%81%A6%E3%81%84%E3%82%8B\"><\/span>\n(\u30c6\u30b9\u30c8\u3057\u305f\u3044\u30d5\u30a1\u30a4\u30eb\u3002\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u66f8\u304d\u3092\u884c\u3063\u3066\u3044\u308b)<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\"># file_mock_sample.py\n\nclass FileMockSample:\n    def my_func_1(self):\n        path1 = '\/tmp\/not_exist_file_1.txt'\n        with open(path1, mode='r') as f1:\n            lines = f1.readlines()\n            print(lines)    # ['line 1\\n, 'line 2\\n', 'line 3']\n        return lines\n\n    def my_func_2(self):\n        path2 = '\/tmp\/not_exist_file_2.txt'\n        with open(path2, mode='w') as f2:\n            f2.write('line1\\n')\n            f2.write('line2\\n')\n            f2.write('line3\\n')<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E3%83%86%E3%82%B9%E3%83%88%E3%82%B3%E3%83%BC%E3%83%89%E3%80%82mock_open%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%AE%E3%83%A2%E3%83%83%E3%82%AF%E3%82%92%E4%BD%9C%E6%88%90%E3%81%97%E3%81%A6%E3%83%86%E3%82%B9%E3%83%88%E3%81%99%E3%82%8B\"><\/span>\n(\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3002mock_open\u3092\u4f7f\u3063\u3066\u30d5\u30a1\u30a4\u30eb\u306e\u30e2\u30c3\u30af\u3092\u4f5c\u6210\u3057\u3066\u30c6\u30b9\u30c8\u3059\u308b)<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\"># test_file_mock_sample.py\nfrom unittest.mock import patch, mock_open\n\nfrom file_mock_sample import FileMockSample\n\nclass TestFileMockSample:\n    def test_my_func_1(self):\n        dt = 'readline1\\nreadline2\\nreadline3\\n'\n        mock_io = mock_open(read_data=dt)\n        # open\u306f\u7d44\u307f\u8fbc\u307f\u95a2\u6570\u306a\u306e\u3067\u3001\u300cbuiltins\u300d\u3092\u4f7f\u3046\n        with patch('builtins.open', mock_io):\n            fm = FileMockSample()\n            result = fm.my_func_1()\n            mock_io.assert_called_once_with('\/tmp\/not_exist_file_1.txt', mode='r')\n            assert result == ['readline1\\n', 'readline2\\n', 'readline3\\n']\n\n    def test_my_func_2(self):\n        mock_io = mock_open()\n        # open\u306f\u7d44\u307f\u8fbc\u307f\u95a2\u6570\u306a\u306e\u3067\u3001\u300cbuiltins\u300d\u3092\u4f7f\u3046\n        with patch('builtins.open', mock_io):\n            fm = FileMockSample()\n            fm.my_func_2()\n            mock_io.assert_called_once_with('\/tmp\/not_exist_file_2.txt', mode='w')<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%8F%82%E8%80%83\"><\/span>\n\u53c2\u8003<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>\u516c\u5f0f\u30da\u30fc\u30b8\n\n<ul>\n<li><a href=\"https:\/\/docs.python.org\/ja\/3\/library\/unittest.mock.html\" rel=\"nofollow noopener\" target=\"_blank\">unittest.mock \u2014 \u30e2\u30c3\u30af\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30e9\u30a4\u30d6\u30e9\u30ea<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/ja\/3\/library\/unittest.mock-examples.html\" rel=\"nofollow noopener\" target=\"_blank\">unittest.mock \u2014 \u5165\u9580<\/a><\/li>\n<\/ul>\n<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9f\u30d5\u30a1\u30a4\u30eb\u3092\u4f7f\u308f\u305a\u306b\u30c6\u30b9\u30c8\u3057\u305f\u3044\u5834\u5408\u3001\u30d5\u30a1\u30a4\u30ebopen\u30e1\u30bd\u30c3\u30c9\u306e\u30b9\u30bf\u30d6(\u30e2\u30c3\u30af)\u3092\u4f5c\u3063\u3066\u30c6\u30b9\u30c8\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4148,4070,4071,4145,4144],"tags":[4147,4065,4066,4142,4141],"class_list":["post-6023","post","type-post","status-publish","format-standard","hentry","category-mock","category-python","category-python3","category-unittest","category-4144","tag-mock","tag-python","tag-python3","tag-unittest","tag-4141"],"_links":{"self":[{"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=\/wp\/v2\/posts\/6023","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6023"}],"version-history":[{"count":4,"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=\/wp\/v2\/posts\/6023\/revisions"}],"predecessor-version":[{"id":6282,"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=\/wp\/v2\/posts\/6023\/revisions\/6282"}],"wp:attachment":[{"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}