{"id":152,"date":"2020-11-29T21:34:20","date_gmt":"2020-11-29T12:34:20","guid":{"rendered":"http:\/\/localhost:8000\/?p=152"},"modified":"2021-01-25T00:19:49","modified_gmt":"2021-01-24T15:19:49","slug":"python-unittest","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2020\/11\/python-unittest.html","title":{"rendered":"\u6a19\u6e96\u30e2\u30b8\u30e5\u30fc\u30ebunittest\u3067python\u306e\u30c6\u30b9\u30c8\u3092\u66f8\u304f"},"content":{"rendered":"
Python\u3067\u672c\u683c\u7684\uff1f\u306b\u30c6\u30b9\u30c8\u3092\u66f8\u304d\u59cb\u3081\u307e\u3057\u305f\u3002\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306e unittest<\/a> \u304c\u666e\u901a\u306b\u307e\u3042\u307e\u3042\u4f7f\u3044\u3084\u3059\u3044\u306e\u3067\u3001\u4f7f\u3044\u65b9\u3092\u307e\u3068\u3081\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n \u57fa\u672c\u306f\u4ee5\u4e0b\u306e\u69cb\u9020\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n \u5b9f\u884c\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u30ed\u30b0\u304c\u51fa\u529b\u3055\u308c\u307e\u3059\u3002\u5b9f\u884c\u9806\u756a\u3001\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3001\u30b9\u30ad\u30c3\u30d7\u306a\u3069\u3092\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n \u3060\u3044\u305f\u3044\u4f7f\u3044\u65b9\u304c\u5206\u304b\u308a\u307e\u3059\u306d\u3002<\/p>\n \u57fa\u672c\u7684\u306a\u5b9f\u884c\u65b9\u6cd5\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u3057\u305f\u3002<\/p>\n \u307e\u3068\u3081\u3066\u5b9f\u884c\u3059\u308b\u969b\u306f\u3001\u30c6\u30b9\u30c8\u30c7\u30a3\u30b9\u30ab\u30d0\u30ea\uff08discover\uff09<\/a>\u306e\u6a5f\u80fd\u3092\u78ba\u8a8d\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u5148\u307b\u3069\u78ba\u8a8d\u3057\u305fhelp\u306e\u3046\u3061\u3001\u4ee5\u4e0b\u306e\u4e09\u3064\u304cdiscover\u306e\u5f15\u6570\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n \u300c\u30c6\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u306e\u540d\u524d\u304c \u5b50\u30d5\u30a9\u30eb\u30c0\u306b\u30c6\u30b9\u30c8\u30b9\u30af\u30ea\u30d7\u30c8\u304c\u914d\u7f6e\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306b<\/p>\n \u30c6\u30b9\u30c8\u30c7\u30a3\u30b9\u30ab\u30d0\u30ea\u306b\u5bfe\u5fdc\u3059\u308b\u306b\u306f\u3001\u5168\u30c6\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u306f\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u6700\u4e0a\u4f4d\u306e\u30c7\u30a3\u30b9\u30ab\u30d0\u30ea\u304b\u3089\u30a4\u30f3\u30dd\u30fc\u30c8\u53ef\u80fd\u306a \u30e2\u30b8\u30e5\u30fc\u30eb \u304b\u30d1\u30c3\u30b1\u30fc\u30b8 \u3067\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093<\/p>\n \u30d0\u30fc\u30b8\u30e7\u30f3 3.4 \u3067\u5909\u66f4: \u30c7\u30a3\u30b9\u30ab\u30d0\u30ea\u304c \u540d\u524d\u7a7a\u9593\u30d1\u30c3\u30b1\u30fc\u30b8 \u3092\u30b5\u30dd\u30fc\u30c8\u3057\u307e\u3057\u305f\u3002<\/p>\n<\/blockquote>\n \u3068\u66f8\u3044\u3066\u3042\u308a\u3001\u540d\u524d\u7a7a\u9593\u30d1\u30c3\u30b1\u30fc\u30b8<\/a>\u306b\u5bfe\u5fdc\u3057\u305f\u306e\u3067\u3042\u308c\u3070\u3001 \u7d50\u5c40\u3001\u5b50\u30d5\u30a9\u30eb\u30c0\u306b \u3053\u306e\u72b6\u614b\u3067\u89aa\u30d5\u30a9\u30eb\u30c0\u306bcd\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u89aa\u30d5\u30a9\u30eb\u30c0\uff0b\u5b50\u30d5\u30a9\u30eb\u30c0\u914d\u4e0b\u306e\u30c6\u30b9\u30c8\u3092\u5168\u3066\u307e\u3068\u3081\u3066\u5b9f\u884c\u3057\u3066\u304f\u308c\u307e\u3057\u305f\u3002<\/p>\n \u5b9f\u969b\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u3001\u30c6\u30b9\u30c8\u3057\u305f\u3044Python\u30e2\u30b8\u30e5\u30fc\u30eb\/\u30af\u30e9\u30b9\u5358\u4f4d\u306b\u3001\u4e00\u304b\u3089\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u30af\u30e9\u30b9\u3092\u4f5c\u308b\u306e\u306f\u5927\u5909\u306a\u306e\u3067\u3001\u30d9\u30fc\u30b9\u3068\u306a\u308b\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u30af\u30e9\u30b9\u3092\u4f5c\u3063\u3066\u3001\u5404\u30c6\u30b9\u30c8\u30af\u30e9\u30b9\u306f\u305d\u308c\u3092\u7d99\u627f\u3059\u308b\u3068\u826f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n \u4eca\u56de\u306f\u3001Web\u30b5\u30fc\u30d0\u3092\u8d77\u52d5\u3057\u3066\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u306e\u30c6\u30b9\u30c8\u3092\u884c\u3046\u305f\u3081\u306e \u30d5\u30a9\u30eb\u30c0\u69cb\u6210\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n \u307e\u305a\u3001 \u5b9f\u88c5\u4e0a\u306e\u30dd\u30a4\u30f3\u30c8\u306f\u4ee5\u4e0b\u3067\u3059\u3002<\/p>\n \u6b21\u306b\u3001 \u5b9f\u88c5\u4e0a\u306e\u30dd\u30a4\u30f3\u30c8\u306f\u4ee5\u4e0b\u3067\u3059\u3002<\/p>\n \u6700\u5f8c\u306b \u3053\u306e\u72b6\u614b\u3067\u3001 \u4ee5\u4e0b\u306e\u70b9\u3092\u78ba\u8a8d\u3067\u304d\u305f\u306e\u3067\u671f\u5f85\u901a\u308a\u52d5\u3044\u3066\u3044\u308b\u3068\u5224\u65ad\u3057\u307e\u3057\u305f\u3002<\/p>\n BaseTestCase\u30af\u30e9\u30b9\u306b\u3001Web\u30b5\u30fc\u30d0\u8d77\u52d5\u30fbDB\u521d\u671f\u5316\u30fb\u30fb\u30fb\u306a\u3069\u8272\u3005\u306a\u6a5f\u80fd\u3092\u8a70\u3081\u8fbc\u3093\u3067\u3044\u304f\u3068\u3001setUp\u51e6\u7406\u304c\u91cd\u304f\u306a\u3063\u3066\u3057\u307e\u3044\u3001\u30b7\u30f3\u30d7\u30eb\u306aFunctionalTest\u3092\u3084\u308a\u305f\u3044\u3060\u3051\u306a\u306e\u306b\u3084\u3051\u306b\u6642\u9593\u304c\u304b\u304b\u308b\u3053\u3068\u304c\u6c17\u306b\u306a\u3063\u3066\u304f\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n \u305d\u306e\u69d8\u306a\u30b1\u30fc\u30b9\u3067\u306f\u3001Web\u30b5\u30fc\u30d0\u8d77\u52d5\u30fbDB\u521d\u671f\u5316\u306a\u3069\u3092\u884c\u3046Feature\u30af\u30e9\u30b9\u3092\u4f5c\u3063\u3066\u3001\u305d\u306e\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u30af\u30e9\u30b9\u306b\u5fc5\u8981\u306aFeature\u30af\u30e9\u30b9\u3060\u3051mixin\uff08\u591a\u91cd\u7d99\u627f\u306e1\u5f62\u614b\uff09\u3059\u308b\u3088\u3046\u306b\u3059\u308b\u3068\u3044\u3044\u3068\u3082\u601d\u3044\u307e\u3059\u3002<\/p>\n Web\u30b5\u30fc\u30d0\u3092\u8d77\u52d5\u3059\u308bFeature\u30af\u30e9\u30b9\uff08TestWebserver\u30af\u30e9\u30b9\uff09\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n BaseTestCase\u30af\u30e9\u30b9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n \u6700\u5f8c\u306b\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u30af\u30e9\u30b9\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n Web\u30b5\u30fc\u30d0\u8d77\u52d5\u304c\u4e0d\u8981\u306a\u3089\u3001<\/p>\n Web\u30b5\u30fc\u30d0\u8d77\u52d5\u304c\u5fc5\u8981\u306a\u3089\u3001<\/p>\n \u3068\u3044\u3046\u611f\u3058\u3067mixin\u3057\u3066\u3042\u3052\u308c\u3070\u5927\u4e08\u592b\u3067\u3059\u3002<\/p>\n \u3055\u3089\u306b\u30c6\u30b9\u30c8DB\u3092\u63d0\u4f9b\u3059\u308bFeature\u30af\u30e9\u30b9\uff08TestDatabase\u30af\u30e9\u30b9\uff09\u3092\u4f5c\u3063\u305f\u3068\u3057\u305f\u3089\u3001\u4e8c\u3064\u306eFeature\u30af\u30e9\u30b9\u3092mixin\u3057\u307e\u3059\u3002<\/p>\n \u30c6\u30b9\u30c8\u3059\u308b\u969b\u306b\u3001\u7279\u5b9a\u306e\u95a2\u6570\u3084\u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u30e2\u30c3\u30af\u3057\u305f\u304f\u306a\u308b\u30b1\u30fc\u30b9\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306f\u3053\u3061\u3089<\/a>\u3067\u3059\u3002<\/p>\n \u8272\u3005\u306a\u30e2\u30c3\u30af\u306e\u4ed5\u65b9\u304c\u3042\u3063\u3066\u5168\u90e8\u8a66\u3057\u305f\u308f\u3051\u3067\u306f\u306a\u3044\u3067\u3059\u304c\u3001\u300c\u81ea\u5206\u304c\u3088\u304f\u4f7f\u3046\u30b1\u30fc\u30b9\u300d\u3068\u300c\u30cf\u30de\u3063\u305f\u30b1\u30fc\u30b9\u300d\u306b\u3064\u3044\u3066\u8a18\u8f09\u3057\u307e\u3059\u3002<\/p>\n \u3053\u3093\u306a\u611f\u3058\u306ehoge.py\u3092\u30c6\u30b9\u30c8\u3059\u308b\u30a4\u30e1\u30fc\u30b8\u3067\u3059\u3002<\/p>\n \u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u30af\u30e9\u30b9\u306e\u4f8b\u3067\u3059\u3002<\/p>\n hoge.py<\/p>\n fuga.py<\/p>\n \u3053\u306e\u3088\u3046\u306b\u3001\u30c6\u30b9\u30c8\u5bfe\u8c61\u306ehoge.py\u306b\u304a\u3044\u3066\u3001fuga.py\u30e2\u30b8\u30e5\u30fc\u30eb\u76f4\u4e0b\u306e\u95a2\u6570get_value\u3068\u3044\u3046\u95a2\u6570\u3092import\u3057\u3066\u5229\u7528\u3057\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u3053\u306e\u3088\u3046\u306a\u30b1\u30fc\u30b9\u306f\u30e2\u30c3\u30af\u6642\u306b\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n \u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306eunittest\u306f\u6a5f\u80fd\u7684\u306f\u5341\u5206\u6e80\u8db3\u306a\u3082\u306e\u306e\u3001\u3044\u304f\u3064\u304b\u4e0d\u6e80\u306a\u70b9\u3082\u898b\u3048\u3066\u304d\u307e\u3057\u305f\u3002<\/p>\n \u5b9f\u904b\u7528\u3067\u30ac\u30c4\u30ac\u30c4\u4f7f\u3044\u51fa\u3059\u3068\u3082\u3057\u304b\u3057\u305f\u3089\u4e0d\u6e80\u70b9\u304c\u8868\u306b\u51fa\u3066\u304f\u308b\u306e\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u3057\u3070\u3089\u304f\u306f\u3053\u306e\u307e\u307e\u4f7f\u3044\u7d9a\u3051\u3066\u307f\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n \u307e\u3060\u5168\u7136\u8abf\u3079\u3066\u307e\u305b\u3093\u304c pytest \u3084 nose2 \u306f\u3044\u305a\u308c\u8abf\u3079\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":" Python\u3067\u672c\u683c\u7684\uff1f\u306b\u30c6\u30b9\u30c8\u3092\u66f8\u304d\u59cb\u3081\u307e\u3057\u305f\u3002\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306e unittest \u304c\u666e\u901a\u306b\u307e\u3042\u307e\u3042\u4f7f\u3044\u3084\u3059\u3044\u306e\u3067\u3001\u4f7f\u3044\u65b9\u3092\u307e\u3068\u3081\u3066\u3044\u304d\u307e\u3059\u3002 \u57fa\u672c\u7684\u306a\u66f8\u304d\u65b9 \u57fa\u672c\u306f\u4ee5\u4e0b\u306e\u69cb\u9020\u306b\u306a\u308a\u307e\u3059\u3002 import unittest class TestSimple(unittest.TestCase): @classmethod def setUpClass(cls): """\u5404\u30af\u30e9\u30b9\u304c\u5b9f\u884c\u3055\u308c\u308b\u76f4\u524d\u306b\u4e00\u5ea6\u3060\u3051\u547c\u3073\u51fa\u3055\u308c\u308b""" print('setUpClass called.') @classmethod def tearDownClass(cls): """\u5404\u30af\u30e9\u30b9\u304c\u5b9f\u884c\u3055\u308c\u305f\u76f4\u5f8c\u306b\u4e00\u5ea6\u3060\u3051\u547c\u3073\u51fa\u3055\u308c\u308b""" print('tearDownCla <\/span>Continue Reading<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,16],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/152"}],"collection":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/comments?post=152"}],"version-history":[{"count":1,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/152\/revisions"}],"predecessor-version":[{"id":153,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/152\/revisions\/153"}],"wp:attachment":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/categories?post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tags?post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\u57fa\u672c\u7684\u306a\u66f8\u304d\u65b9<\/h2>\n
import unittest\n\nclass TestSimple(unittest.TestCase):\n\n @classmethod\n def setUpClass(cls):\n """\u5404\u30af\u30e9\u30b9\u304c\u5b9f\u884c\u3055\u308c\u308b\u76f4\u524d\u306b\u4e00\u5ea6\u3060\u3051\u547c\u3073\u51fa\u3055\u308c\u308b"""\n print('setUpClass called.')\n\n @classmethod\n def tearDownClass(cls):\n """\u5404\u30af\u30e9\u30b9\u304c\u5b9f\u884c\u3055\u308c\u305f\u76f4\u5f8c\u306b\u4e00\u5ea6\u3060\u3051\u547c\u3073\u51fa\u3055\u308c\u308b"""\n print('tearDownClass called.')\n\n def setUp(self):\n """\u5404\u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u304c\u5b9f\u884c\u3055\u308c\u308b\u76f4\u524d\u306b\u547c\u3073\u51fa\u3055\u308c\u308b"""\n print('setUp called.')\n\n def tearDown(self):\n """\u5404\u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u304c\u5b9f\u884c\u3055\u308c\u305f\u76f4\u5f8c\u306b\u547c\u3073\u51fa\u3055\u308c\u308b"""\n print('tearDown called.')\n\n def test_lower(self):\n print(f'test_lower called. self_id: {id(self)}')\n self.assertEqual('HOGE'.lower(), 'hoge')\n\n def test_upper(self):\n print(f'test_upper called. self_id: {id(self)}')\n self.assertEqual('hoge'.upper(), 'HOGE')\n\n @unittest.skip("this test method will be skipped")\n def test_split(self):\n print(f'test_split called. self_id: {id(self)}')\n self.assertEqual(len('aa bb'.split()), 2)\n\nif __name__ == '__main__':\n unittest.main()<\/code><\/pre>\n
\n
\n
\n
\n
\n
\n
python -m unittest<\/code>\u306e\u3088\u3046\u306bunittest\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u6307\u5b9a\u3057\u306a\u304f\u3066\u3082\u30c6\u30b9\u30c8\u3092\u5b9f\u884c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b<\/li>\n<\/ul>\n<\/li>\n
\n
test*.py<\/code>\u306b\u3057\u3066\u304a\u304f\u3068\u3001\u30c6\u30b9\u30c8\u30c7\u30a3\u30b9\u30ab\u30d0\u30ea\u306e\u691c\u7d22\u30d1\u30bf\u30fc\u30f3\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306a\u306e\u3067\u3001\u5c11\u3057\u4fbf\u5229<\/li>\n<\/ul>\n<\/li>\n
\n
test_*<\/code>\u306e\u5f62\u5f0f\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u30c6\u30b9\u30c8\u5bfe\u8c61\u3068\u306a\u308b\uff08\u3053\u306e\u4f8b\u3067\u306f
test_lower<\/code>
test_upper<\/code>\uff09<\/li>\n<\/ul>\n<\/li>\n
\n
self.assertEqual()<\/code>\u3001
self.assertIsNone()<\/code>\u3001
self.assertRaises()<\/code>\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u547c\u3073\u51fa\u3059<\/li>\n
$ python test_simple.py \nsetUpClass called. # \u30af\u30e9\u30b9\u5358\u4f4d\u306e\u524d\u51e6\u7406\nsetUp called. # \u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u5358\u4f4d\u306e\u524d\u51e6\u7406\ntest_lower called. self_id: 4555136160 # \u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u306e\u5b9f\u884c\ntearDown called. # \u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u5358\u4f4d\u306e\u5f8c\u51e6\u7406\nsetUp called. # \u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u5358\u4f4d\u306e\u524d\u51e6\u7406\ntest_upper called. self_id: 4547337472 # \u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u306e\u5b9f\u884c\uff08\u4e00\u3064\u76ee\u3068\u7570\u306a\u308b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\uff09\ntearDown called. # \u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u5358\u4f4d\u306e\u5f8c\u51e6\u7406\ntearDownClass called. # \u30af\u30e9\u30b9\u5358\u4f4d\u306e\u3042\u3068\u51e6\u7406\n\n----------------------------------------------------------------------\nRan 3 tests in 0.000s\n\nOK (skipped=1) # \u4e00\u3064\u306e\u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u304cskip\u3055\u308c\u305f<\/code><\/pre>\n
\u5b9f\u884c\u65b9\u6cd5<\/h2>\n
\u30b9\u30af\u30ea\u30d7\u30c8\u3068\u3057\u3066\u5b9f\u884c<\/h3>\n
unittest.main()<\/code>\u3092\u5404\u30c6\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u306b\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3067\u3001\u5b9f\u884c\u6642\u306b
python -m unittest<\/code>\u306e\u3088\u3046\u306bunittest\u3092\u30e2\u30b8\u30e5\u30fc\u30eb\u3068\u3057\u3066\u5b9f\u884c\u3057\u306a\u304f\u3066\u3082\u5bfe\u8c61\u30c6\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u3092\u30b9\u30af\u30ea\u30d7\u30c8\u3068\u3057\u3066\u5b9f\u884c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n
# \u7279\u5b9a\u306e\u30c6\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u3092\u5b9f\u884c\u3059\u308b\npython test_simple.py\n\n# \u7279\u5b9a\u306e\u30c6\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u5185\u306e\u7279\u5b9a\u306e\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u3092\u5b9f\u884c\u3059\u308b\npython test_simple.py TestSimple\n\n# \u7279\u5b9a\u306e\u30c6\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u5185\u306e\u7279\u5b9a\u306e\u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3059\u308b\npython test_simple.py TestSimple.test_upper<\/code><\/pre>\n
unittest\u3092\u30e2\u30b8\u30e5\u30fc\u30eb\u3068\u3057\u3066\u5b9f\u884c<\/h3>\n
-m unittest<\/code>\u3067unittest\u3092\u30e2\u30b8\u30e5\u30fc\u30eb\u3068\u3057\u3066\u5b9f\u884c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u307e\u305a\u306fhelp\u3092\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n
$ python -m unittest -h\nusage: python -m unittest [-h] [-v] [-q] [--locals] [-f] [-c] [-b] [-k TESTNAMEPATTERNS] [tests [tests ...]]\n\npositional arguments:\n tests a list of any number of test modules, classes and test methods.\n\noptional arguments:\n -h, --help show this help message and exit\n -v, --verbose Verbose output\n -q, --quiet Quiet output\n --locals Show local variables in tracebacks\n -f, --failfast Stop on first fail or error\n -c, --catch Catch Ctrl-C and display results so far\n -b, --buffer Buffer stdout and stderr during tests\n -k TESTNAMEPATTERNS Only run tests which match the given substring\n\nExamples:\n python -m unittest test_module - run tests from test_module\n python -m unittest module.TestClass - run tests from module.TestClass\n python -m unittest module.Class.test_method - run specified test method\n python -m unittest path\/to\/test_file.py - run tests from test_file.py\n\nusage: python -m unittest discover [-h] [-v] [-q] [--locals] [-f] [-c] [-b] [-k TESTNAMEPATTERNS] [-s START] [-p PATTERN] [-t TOP]\n\noptional arguments:\n -h, --help show this help message and exit\n -v, --verbose Verbose output\n -q, --quiet Quiet output\n --locals Show local variables in tracebacks\n -f, --failfast Stop on first fail or error\n -c, --catch Catch Ctrl-C and display results so far\n -b, --buffer Buffer stdout and stderr during tests\n -k TESTNAMEPATTERNS Only run tests which match the given substring\n -s START, --start-directory START\n Directory to start discovery ('.' default)\n -p PATTERN, --pattern PATTERN\n Pattern to match tests ('test*.py' default)\n -t TOP, --top-level-directory TOP\n Top level directory of project (defaults to start directory)\n\nFor test discovery all test modules must be importable from the top level directory of the project.<\/code><\/pre>\n
\n
discover<\/code>\u306e\u6709\u7121\n
\n
discover<\/code>\uff08\u30c6\u30b9\u30c8\u30c7\u30a3\u30b9\u30ab\u30d0\u30ea\uff09\u306b\u5f15\u6570\u3092\u6e21\u3057\u305f\u3044\u6642\u3060\u3051\u3001
discover<\/code>\u3092\u6307\u5b9a\u3059\u308b<\/li>\n<\/ul>\n<\/li>\n
-f, --failfast<\/code>\n
\n
-c, --catch<\/code>\n
\n
tearDown()\/tearDownClass()<\/code>\u3092\u5b9f\u884c\u3057\u3066\u304f\u308c\u308b\u3088\u3046\u306b\u306a\u308b<\/li>\n
# \u7279\u5b9a\u306e\u30c6\u30b9\u30c8\u30e2\u30b8\u30e5\u30fc\u30eb\/\u30d5\u30a1\u30a4\u30eb\u3092\u5b9f\u884c\u3059\u308b\npython -m unittest test_simple\n\n# \u7279\u5b9a\u306e\u30c6\u30b9\u30c8\u30e2\u30b8\u30e5\u30fc\u30eb\u5185\u306e\u7279\u5b9a\u306e\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u3092\u5b9f\u884c\u3059\u308b\npython -m unittest test_simple.TestSimple\n\n# \u7279\u5b9a\u306e\u30c6\u30b9\u30c8\u30e2\u30b8\u30e5\u30fc\u30eb\u5185\u306e\u7279\u5b9a\u306e\u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3059\u308b\npython -m unittest test_simple.TestSimple.test_upper<\/code><\/pre>\n
\u8907\u6570\u306e\u30c6\u30b9\u30c8\u30b9\u30af\u30ea\u30d7\u30c8\uff08\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\uff09\u3092\u307e\u3068\u3081\u3066\u5b9f\u884c<\/h3>\n
\n
-s START, --start-directory START<\/code>\n
\n
-p PATTERN, --pattern PATTERN<\/code>\n
\n
test*.py<\/code><\/li>\n<\/ul>\n<\/li>\n
-t TOP, --top-level-directory TOP<\/code>\n
\n
\u540c\u3058\u30d5\u30a9\u30eb\u30c0\u914d\u4e0b\u3092\u307e\u3068\u3081\u3066\u5b9f\u884c<\/h4>\n
test<\/code>\u3067\u59cb\u307e\u308a\u300d\u300c\u540c\u3058\u30d5\u30a9\u30eb\u30c0\u306b\u914d\u7f6e\u3055\u308c\u3066\u3044\u308b\u300d\u5834\u5408\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306e\u307e\u307e\u3067\u4f55\u3082\u5f15\u6570\u3092\u6307\u5b9a\u305b\u305a\u30c6\u30b9\u30c8\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u307e\u3068\u3081\u3066\u5b9f\u884c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n
python -m unittest\n# \u3053\u308c\u306f\u4ee5\u4e0b\u3068\u540c\u3058\npython -m unittest discover -s "." -p "test*.py" -t "."\npython -m unittest discover --start-directory "." --pattern "test*.py" --top-level-directory "."<\/code><\/pre>\n
\u5b50\u30d5\u30a9\u30eb\u30c0\u3082\u542b\u3081\u3066\u307e\u3068\u3081\u3066\u5b9f\u884c<\/h4>\n
\n
sys.path<\/code>\u306b\u8ffd\u52a0\u3057\u3066\u3042\u3052\u308c\u3070\u826f\u3044\u306e\u304b\u306a\u3068\u601d\u3044\u3001\u74b0\u5883\u5909\u6570
PYTHONPATH<\/code>\u306b\u8ffd\u52a0\u3057\u3066\u307f\u305f\u306e\u3067\u3059\u304c\u3001\u305d\u308c\u3060\u3051\u3067\u306f\u5b50\u30d5\u30a9\u30eb\u30c0\u914d\u4e0b\u306e\u30c6\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u3092\u8a8d\u8b58\u3057\u3066\u304f\u308c\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/p>\n
__init__.py<\/code>\u3092\u914d\u7f6e\u3059\u308b\uff08\u3064\u307e\u308a\u30ec\u30ae\u30e5\u30e9\u30fc\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u3059\u308b\uff09\u3053\u3068\u3067\u5b50\u30d5\u30a9\u30eb\u30c0\u914d\u4e0b\u306e\u30c6\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u3082\u8a8d\u8b58\u3057\u3066\u304f\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002
\n\u3061\u306a\u307f\u306b\u3001\u5b6b\u30d5\u30a9\u30eb\u30c0\u306b\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u3092\u914d\u7f6e\u3059\u308b\u5834\u5408\u306f\u3001\u5b50\u30d5\u30a9\u30eb\u30c0\u30fb\u5b6b\u30d5\u30a9\u30eb\u30c0\u306e\u4e21\u65b9\u306b__init__.py<\/code>\u3092\u914d\u7f6e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3057\u305f\u3002<\/p>\n
\u251c\u2500\u2500 child\n\u2502 \u251c\u2500\u2500 __init__.py # \u3053\u308c\u3092\u914d\u7f6e\u3059\u308b\n\u2502 \u2514\u2500\u2500 test_simple_child.py\n\u2514\u2500\u2500 test_simple.py<\/code><\/pre>\n
python -m unittest<\/code><\/pre>\n
BaseTestCase\u30af\u30e9\u30b9\u3092\u4f5c\u308b\uff08\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u3092\u4f8b\u306b\uff09<\/h2>\n
BaseTestCase<\/code>\u30af\u30e9\u30b9\u3092\u4f5c\u3063\u3066\u307f\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n
\u251c\u2500\u2500 document_roots\n\u2502 \u2514\u2500\u2500 TestScraping # \u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u30af\u30e9\u30b9\u6bce\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u30eb\u30fc\u30c8\u3002\u30d5\u30a9\u30eb\u30c0\u540d=\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u30af\u30e9\u30b9\u540d\n\u2502 \u2514\u2500\u2500 hello_world.html\n\u251c\u2500\u2500 base_test_case.py # \u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u7528\u306eBaseTestCase\u30af\u30e9\u30b9\n\u2514\u2500\u2500 test_scraping.py # \u5404\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u30af\u30e9\u30b9<\/code><\/pre>\n
base_test_case.py<\/code>\u306b
BaseTestCase<\/code>\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002
\n\u5f79\u5272\u306f\u3001\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u30af\u30e9\u30b9\u306e\u5b9f\u884c\u524d\u306b\u3001\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u30af\u30e9\u30b9\u7528\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u30eb\u30fc\u30c8\u914d\u4e0b\u306e\u30d5\u30a1\u30a4\u30eb\u3092Web\u30b5\u30fc\u30d0\u3067serve\u3057\u3001\u30c6\u30b9\u30c8\u304c\u5b8c\u4e86\u3057\u305f\u3089Web\u30b5\u30fc\u30d0\u3092\u505c\u6b62\u3059\u308b\u3053\u3068\u3067\u3059\u3002<\/p>\nimport unittest\nfrom os.path import exists\nimport asyncio\nfrom http.server import SimpleHTTPRequestHandler\nfrom socketserver import TCPServer\nimport _thread\nimport requests\nimport time\n\ndef fire_and_forget(f):\n """\u5bfe\u8c61\u95a2\u6570\u3092\u975e\u540c\u671f\u3067\u6295\u3052\u3063\u3071\u306a\u3057\u306b\u3059\u308b\u305f\u3081\u306e\u30c7\u30b3\u30ec\u30fc\u30bf"""\n def wrapped(*args, **kwargs):\n return asyncio.get_event_loop().run_in_executor(None, f, *args, *kwargs)\n return wrapped\n\n@fire_and_forget\ndef start_web_server(port_num: int, test_class_name: str):\n """\u30c6\u30b9\u30c8\u7528\u306eWEB\u30b5\u30fc\u30d0\u3092\u8d77\u52d5\u3059\u308b"""\n document_root: str = f'document_roots\/{test_class_name}'\n if not exists(document_root):\n raise FileNotFoundError(f'document root is not exists. document_root: {document_root}')\n\n class __Handler(SimpleHTTPRequestHandler):\n def __init__(self, *args, **kwargs):\n super().__init__(*args, directory=document_root, **kwargs)\n\n def do_POST(self):\n if self.path.startswith('\/kill_server'):\n def kill_me_please(server):\n server.shutdown()\n _thread.start_new_thread(kill_me_please, (httpd,))\n self.send_error(500)\n print(f'webserver stopped. port_num: {port_num}, document_root: {document_root}')\n\n TCPServer.allow_reuse_address = True\n with TCPServer(('', port_num), __Handler) as httpd:\n print(f'webserver started. port_num: {port_num}, document_root: {document_root}')\n httpd.serve_forever()\n\ndef stop_web_server(port_num: int):\n """\u30c6\u30b9\u30c8\u7528\u306eWEB\u30b5\u30fc\u30d0\u3092\u505c\u6b62\u3059\u308b"""\n requests.post(f'http:\/\/localhost:{port_num}\/kill_server')\n\nclass BaseTestCase(unittest.TestCase):\n port_num: int = 9000 # default\u5024\u3002\u7d99\u627f\u5148\u3067\u4e0a\u66f8\u304d\u3057\u3066\u5229\u7528\u3059\u308b\n\n @classmethod\n def setUpClass(cls):\n print('BaseTestCase.setUpClass started.')\n start_web_server(cls.port_num, cls.__name__)\n time.sleep(1) # Web\u30b5\u30fc\u30d0\u304c\u7acb\u3061\u4e0a\u304c\u308b\u306e\u30921\u79d2\u5f85\u6a5f\n print('BaseTestCase.setUpClass finished.')\n\n @classmethod\n def tearDownClass(cls):\n print('BaseTestCase.tearDownClass started.')\n stop_web_server(cls.port_num)\n print('BaseTestCase.tearDownClass finished.')\n\nif __name__ == '__main__':\n unittest.main()<\/code><\/pre>\n
\n
start_web_server()<\/code>\u3067Web\u30b5\u30fc\u30d0\u3092\u8d77\u52d5\u3059\u308b\u304c\u3001\u540c\u671f\u7684\u306b\u5b9f\u884c\u3059\u308b\u3068
httpd.serve_forever()<\/code>\u3067\u6c38\u9060\u5f85\u3063\u3066\u3057\u307e\u3046\u305f\u3081\u3001
@fire_and_forget<\/code>\u30c7\u30b3\u30ec\u30fc\u30bf\u3067\u6295\u3052\u3063\u3071\u306a\u3057\u306b\u306a\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u308b\n
\n
setUpClass()<\/code>\u3067Web\u30b5\u30fc\u30d0\u3092\u8d77\u52d5\u3057\u3001
tearDownClass()<\/code>\u3067\u30b5\u30fc\u30d0\u3092\u505c\u6b62\u3059\u308b\n
\n
setUp()<\/code>
tearDown()<\/code>\u306b\u5b9f\u88c5\u3059\u308c\u3070OK<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n
test_scraping.py<\/code>\u306b
TestScraping<\/code>\u30af\u30e9\u30b9\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n
import requests\nfrom requests import Response\nfrom scraping_base_test_case import BaseTestCase # type: ignore\nfrom bs4 import BeautifulSoup\n\nclass TestScraping(BaseTestCase):\n\n @classmethod\n def setUpClass(cls):\n print('TestScraping.setUpClass started.')\n cls.port_num = 9001\n super().setUpClass()\n print('TestScraping.setUpClass finished.')\n\n def test_get_body_text(self):\n print('test_get_body_text started.')\n response: Response = requests.get(f'http:\/\/localhost:{TestScraping.port_num}\/hello_world.html')\n soup: BeautifulSoup = BeautifulSoup(response.text)\n self.assertEqual(soup.body.text, 'hello world!!')\n print('test_get_body_text finished.')\n\n def test_get_body_length(self):\n print('test_get_body_length started.')\n response: Response = requests.get(f'http:\/\/localhost:{TestScraping.port_num}\/hello_world.html')\n soup: BeautifulSoup = BeautifulSoup(response.text)\n self.assertEqual(len(soup.body.text), 13)\n print('test_get_body_length finished.')<\/code><\/pre>\n
\n
BaseTestCase<\/code>\u3092\u7d99\u627f\u3059\u308b<\/li>\n
setUpClass()<\/code>\u3067\u3053\u306e\u30af\u30e9\u30b9\u5c02\u7528\u306e\u30dd\u30fc\u30c8\u756a\u53f7\uff08port_num\uff09\u3092\u6307\u5b9a\u3057\u305f\u4e0a\u3067\u3001
super().setUpClass()<\/code>\u3092\u5b9f\u884c\u3059\u308b\n
\n
setUp()<\/code>\u3084
setUpClass()<\/code>\u3092\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3057\u3066\u3001
cls<\/code>\u3084
self<\/code>\u306b\u5024\u3092\u8a2d\u5b9a\u3057\u305f\u4e0a\u3067
super()<\/code>\u3067\u89aa\u30af\u30e9\u30b9\u306e\u51e6\u7406\u3092\u547c\u3073\u51fa\u3057\u3066\u3042\u3052\u308c\u3070\u3088\u3055\u305d\u3046<\/li>\n
setUp()<\/code>\u7b49\u3092\u4f7f\u308f\u305a\u30c7\u30b3\u30ec\u30fc\u30bf\u3067\u4f5c\u308a\u8fbc\u3093\u3067\u3082\u826f\u3055\u305d\u3046\uff08\u4f8b\u3048\u3070port\u756a\u53f7\u3092\u6e21\u3057\u3066\u30c7\u30b3\u30ec\u30fc\u30bf\u3067Web\u30b5\u30fc\u30d0\u3092\u7acb\u3061\u4e0a\u3052\u308b\uff09\u3060\u304c\u3001\u305b\u3063\u304b\u304funittest\u304c\u63d0\u4f9b\u3057\u3066\u304f\u308c\u3066\u3044\u308b\u6a5f\u80fd\u3068\u88ab\u308b\u3057\u3001\u72ec\u81ea\u611f\u304c\u5f37\u3059\u304e\u308b\u306e\u3067\u3084\u3081\u3066\u304a\u3044\u305f<\/li>\n<\/ul>\n<\/li>\n
test_get_body_text() <\/code>\u3068
test_get_body_length()<\/code>\u306e\u4e8c\u3064\u306e\u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u308b<\/li>\n<\/ul>\n
hello_world.html<\/code>\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002
hello world!!<\/code>\u3063\u3066\u8868\u793a\u3059\u308b\u3060\u3051\u306e\u30b7\u30f3\u30d7\u30eb\u306aHTML\u30d5\u30a1\u30a4\u30eb\u3067\u3059\u3002<\/p>\n
<html><body>hello world!!<\/body><\/html><\/code><\/pre>\n
test_scraping.py<\/code>\u3092\u5b9f\u884c\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30ed\u30b0\u304c\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n
$ python test_scraping.py\nTestScraping.setUpClass started.\nBaseTestCase.setUpClass started.\nwebserver started. port_num: 9001, document_root: document_roots\/TestScraping # \u3053\u3053\u3067Web\u30b5\u30fc\u30d0\u3092\u8d77\u52d5\nBaseTestCase.setUpClass finished.\nTestScraping.setUpClass finished.\ntest_get_body_length started. # \u4e00\u3064\u76ee\u306e\u30c6\u30b9\u30c8\n127.0.0.1 - - [29\/Nov\/2020 12:30:52] "GET \/hello_world.html HTTP\/1.1" 200 -\ntest_get_body_length finished.\ntest_get_body_text started. # \u4e8c\u3064\u76ee\u306e\u30c6\u30b9\u30c8\n127.0.0.1 - - [29\/Nov\/2020 12:30:52] "GET \/hello_world.html HTTP\/1.1" 200 -\ntest_get_body_text finished.\nBaseTestCase.tearDownClass started.\n127.0.0.1 - - [29\/Nov\/2020 12:30:52] code 500, message Internal Server Error\n127.0.0.1 - - [29\/Nov\/2020 12:30:52] "POST \/kill_server HTTP\/1.1" 500 -\nwebserver stopped. port_num: 9001, document_root: document_roots\/TestScraping # \u3053\u3053\u3067Web\u30b5\u30fc\u30d0\u3092\u505c\u6b62\nBaseTestCase.tearDownClass finished.\n\n----------------------------------------------------------------------\nRan 2 tests in 0.020s\n\nOK<\/code><\/pre>\n
\n
9000<\/code>\u3067\u306f\u306a\u304f\u3001\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u30af\u30e9\u30b9\u5074\u3067\u8a2d\u5b9a\u3057\u305f
9001<\/code>\u3067\u8d77\u52d5\u3057\u3066\u3044\u308b<\/li>\n<\/ul>\n
BaseTestCase\u30af\u30e9\u30b9\u304b\u3089Feature\u30af\u30e9\u30b9\u3092\u5207\u308a\u51fa\u3057\u3066mixin<\/h2>\n
def fire_and_forget(f):\n # \u7701\u7565\n\nclass TestWebserver:\n\n @classmethod\n @fire_and_forget\n def start_web_server(cls, port_num: int, test_class_name: str):\n # \u7701\u7565\n\n @classmethod\n def stop_web_server(cls, port_num: int):\n # \u7701\u7565<\/code><\/pre>\n
\n
import unittest\nimport time\nfrom test_webserver import TestWebserver\n\nclass BaseTestCase(unittest.TestCase):\n port_num: int = 9000 # default\u5024\u3002\u7d99\u627f\u5148\u3067\u4e0a\u66f8\u304d\u3057\u3066\u5229\u7528\u3059\u308b\n\n @classmethod\n def setUpClass(cls):\n if TestWebserver in cls.__mro__:\n cls.start_web_server(cls.port_num, cls.__name__)\n time.sleep(1) # Web\u30b5\u30fc\u30d0\u304c\u7acb\u3061\u4e0a\u304c\u308b\u306e\u30921\u79d2\u5f85\u6a5f\n\n @classmethod\n def tearDownClass(cls):\n if TestWebserver in cls.__mro__:\n cls.stop_web_server(cls.port_num)<\/code><\/pre>\n
\n
cls.__mro__<\/code>\u3067\u78ba\u8a8d\u3057\u3001\u7d99\u627f\u3057\u3066\u3044\u305f\u3089web\u30b5\u30fc\u30d0\u3092\u8d77\u52d5\u3059\u308b<\/li>\n<\/ul>\n
class TestScraping(BaseTestCase):<\/code><\/pre>\n
class TestScraping(BaseTestCase, TestWebserver):<\/code><\/pre>\n
class TestScraping(BaseTestCase, TestWebserver, TestDatabase):<\/code><\/pre>\n
\u30e2\u30c3\u30af<\/h2>\n
\u81ea\u5206\u304c\u3088\u304f\u4f7f\u3046\u30b1\u30fc\u30b9<\/h3>\n
\ndef my_public_method() -> str:\n pass\n\ndef __my_private_method() -> str:\n pass\n\nclass Fuga():\n def get_piyo(self) -> str:\n pass\n\n def __get_piyo(self) -> str:\n pass<\/code><\/pre>\n
from hoge import Fuga\nfrom hoge\n\nclass TestClass(unittest.TestCase):\n\n # \u30af\u30e9\u30b9\u5185\u306epublic\u95a2\u6570\u3092mock\u3057\u3066\u623b\u308a\u5024\u3092\u5909\u66f4\u3059\u308b\n @patch.object(Fuga, 'get_piyo', return_value='xxx')\n def test_case(self, mock):\n pass\n\n # \u30af\u30e9\u30b9\u5185\u306eprivate\u95a2\u6570\u3092mock\u3057\u3066\u623b\u308a\u5024\u3092\u5909\u66f4\u3059\u308b\n # \u30af\u30e9\u30b9\u5185\u306e\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u95a2\u6570\u306f`_\u30af\u30e9\u30b9\u540d`\u304c\u982d\u306b\u3064\u3044\u305f\u5f62\u3067\u5185\u90e8\u7684\u306b\u7ba1\u7406\u3055\u308c\u3066\u3044\u308b\uff08cls.__dict__\u3067\u78ba\u8a8d\u3067\u304d\u308b\uff09\n @patch.object(Fuga, '_Fuga__get_piyo', return_value='xxx')\n def test_case(self, mock):\n pass\n\n # \u30e2\u30b8\u30e5\u30fc\u30eb\u76f4\u4e0b\u306epublic\u95a2\u6570\u3092mock\u3057\u3066\u623b\u308a\u5024\u3092\u5909\u66f4\u3059\u308b\n @patch.object(hoge, 'my_public_method', return_value='xxx')\n def test_case(self, mock):\n pass\n\n # \u30e2\u30b8\u30e5\u30fc\u30eb\u76f4\u4e0b\u306eprivate\u95a2\u6570\u3092mock\u3057\u3066\u623b\u308a\u5024\u3092\u5909\u66f4\u3059\u308b\n # \u30e2\u30b8\u30e5\u30fc\u30eb\u76f4\u4e0b\u306eprivate\u95a2\u6570\u306f\u666e\u901a\u306b\u305d\u306e\u307e\u307emock\u3067\u304d\u308b\n @patch.object(hoge, '__my_private_method', return_value='xxx')\n def test_case(self, mock):\n pass\n\n # @patch\u3092\u5229\u7528\u3059\u308b\n # @patch.object(Fuga, 'get_piyo', return_value='xxx')\u3068\u540c\u3058\n @patch('hoge.Fuga.get_piyo', return_value='xxx')\n def test_case(self, mock):\n pass\n\n # patch.object\u3092\u5229\u7528\u3059\u308b\n # @patch.object(Fuga, 'get_piyo', return_value='xxx')\u3068\u540c\u3058\n def test_case(self):\n with patch.object(Fuga, 'get_piyo', return_value='xxx') as mock: # \u3053\u3053\u3067\u30b9\u30b3\u30fc\u30d7\u3092\u304d\u3063\u3066\u3044\u308b\n pass\n\n # \u547c\u3073\u51fa\u3057\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\n @patch.object(Fuga, 'get_piyo', return_value='xxx')\n def test_case(self, mock):\n mock.assert_not_called() # \u547c\u3073\u51fa\u3055\u308c\u306a\u3044\u3053\u3068\u3092\u78ba\u8a8d\n mock.assert_called_once() # \u4e00\u5ea6\u3060\u3051\u547c\u3073\u51fa\u3055\u308c\u308b\u3053\u3068\u3092\u78ba\u8a8d\n mock.assert_called_once_with('x', y='z') # \u4e00\u5ea6\u3060\u3051\u6307\u5b9a\u3057\u305f\u5f15\u6570\u3067\u547c\u3073\u51fa\u3055\u308c\u308b\u3053\u3068\u3092\u78ba\u8a8d\n mock.assert_called_with('x', y='z') # \u6307\u5b9a\u3057\u305f\u5f15\u6570\u3067\u547c\u3073\u51fa\u3055\u308c\u308b\u3053\u3068\u3092\u78ba\u8a8d\n self.assertEqual(mock.call_count, 5) # 5\u4f1a\u547c\u3073\u51fa\u3055\u308c\u308b\u3053\u3068\u3092\u78ba\u8a8d<\/code><\/pre>\n
\u30cf\u30de\u3063\u305f\u30b1\u30fc\u30b9\uff08\u89e3\u6c7a\u6e08\u307f\uff09<\/h3>\n
\u3042\u308b\u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u3067\u30e2\u30c3\u30af\u3057\u305f\u3082\u306e\u304c\u4ed6\u306e\u30c6\u30b9\u30c8\u306b\u5f71\u97ff\u3059\u308b<\/h4>\n
from hoge import Fuga\n\n# \u30c0\u30e1\u306a\u4f8b\n# mock\u304c\u30e1\u30bd\u30c3\u30c9\u306b\u9589\u3058\u306a\u3044\n# Fuga.get_piyo\u304cMagicMock\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u7f6e\u304d\u63db\u3048\u3089\u308c\u305f\u72b6\u614b\u3067\u5f8c\u7d9a\u306e\u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u304c\u5b9f\u884c\u3055\u308c\u3066\u3057\u307e\u3046\ndef test_case(self):\n mock = patch.object(Fuga, 'get_piyo', return_value='xxx')\n\n# OK\u306a\u4f8b\n# mock\u304cwith\u53e5\u306b\u9589\u3058\u308b\n# with\u53e5\u306e\u5916\u5074\u3067\u306fFuga.get_piyo\u306f\u5143\u306e\u95a2\u6570\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u623b\u308b\ndef test_case(self):\n with patch.object(Fuga, '_Fuga__get_piyo', return_value='xxx') as mock:\n pass\n\n# OK\u306a\u4f8b\n# mock\u304c\u30e1\u30bd\u30c3\u30c9\u306b\u9589\u3058\u308b\n# \u4ed6\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u306fFuga.get_piyo\u306f\u5143\u306e\u95a2\u6570\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u623b\u308b\n@patch.object(Fuga, '_Fuga__get_piyo', return_value='xxx')\ndef test_case(self, mock):\n pass<\/code><\/pre>\n
\u30c6\u30b9\u30c8\u5bfe\u8c61\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u3084\u30af\u30e9\u30b9\u3067import\u3055\u308c\u305f\u3001\u30e2\u30b8\u30e5\u30fc\u30eb\u76f4\u4e0b\u306e\u95a2\u6570\u3092\u30e2\u30c3\u30af\u3067\u304d\u306a\u3044<\/h4>\n
from fuga import get_value\ndef get_fuga_value() -> str:\n return get_value()<\/code><\/pre>\n
from fuga import get_value\ndef get_value() -> str:\n return 'fugafuga'<\/code><\/pre>\n
# \u30c0\u30e1\u306a\u4f8b\n# \u3053\u306e\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u30af\u30e9\u30b9\u5185\u3067fuga.get_value()\u3059\u308b\u30b1\u30fc\u30b9\u306f\u30e2\u30c3\u30af\u3055\u308c\u308b\u304c\n# hoge.get_fuga_value() -> fuga.get_value()\u306e\u3088\u3046\u306b\u547c\u3073\u51fa\u3059\u6642\u306f\u30e2\u30c3\u30af\u3055\u308c\u306a\u3044\nimport fuga\n@patch.object(fuga, 'get_value', return_value='mocked_value')\ndef test_case(self, mock):\n pass\n\n# \u30c0\u30e1\u306a\u4f8b\n# hoge.get_fuga_value() -> fuga.get_value()\u306e\u3088\u3046\u306b\u547c\u3073\u51fa\u3059\u6642\u306f\u30e2\u30c3\u30af\u3055\u308c\u306a\u3044\n@patch('fuga.get_value', return_value='mocked_value')\ndef test_case(self, mock):\n pass\n\n# OK\u306a\u4f8b\n# hoge.get_fuga_value() -> fuga.get_value()\u306e\u3088\u3046\u306b\u547c\u3073\u51fa\u3059\u6642\u306b\u3061\u3083\u3093\u3068\u30e2\u30c3\u30af\u3055\u308c\u308b\uff01\uff01\n# hoge.py\u3067\u306e`from fuga import get_value`\u306b\u3088\u308a\u3001`hoge`\u3068\u3044\u3046namespace\u306b`get_value`\u3068\u3044\u3046\u540d\u524d\u3067get_value\u95a2\u6570\u304c\u5b9a\u7fa9\u3055\u308c\u308b\n# \u305d\u3053\u3092\u30e2\u30c3\u30af\u3059\u308c\u3070\u3088\u3044\u306e\u3067\u3001`hoge.get_value`\u3092\u6307\u5b9a\u3059\u308b\u306e\u304c\u6b63\u89e3\n@patch('hoge.get_value', return_value='mocked_value')\ndef test_case(self, mock):\n pass<\/code><\/pre>\n
\u3055\u3044\u3054\u306b<\/h2>\n
\n
__init__.py<\/code>\u3092\u914d\u7f6e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b<\/li>\n