{"id":45554,"date":"2025-07-30T00:26:12","date_gmt":"2025-07-29T16:26:12","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/45554.html"},"modified":"2025-07-30T00:26:12","modified_gmt":"2025-07-29T16:26:12","slug":"pytest%e4%b8%ba%e4%bb%80%e4%b9%88%e7%a8%8b%e5%ba%8f%e8%bf%90%e8%a1%8c%e5%85%a5%e5%8f%a3%e5%91%bd%e5%90%8d%e4%b8%bacli-py","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/45554.html","title":{"rendered":"(pytest)\u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u8fd0\u884c\u5165\u53e3\u547d\u540d\u4e3acli.py"},"content":{"rendered":"<p>\u5c06\u7a0b\u5e8f\u7684\u8fd0\u884c\u5165\u53e3\u547d\u540d\u4e3a\u00a0cli.py&#xff08;Command Line Interface \u7684\u7f29\u5199&#xff09;\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u547d\u540d\u7ea6\u5b9a&#xff0c;\u4e3b\u8981\u7528\u4e8e\u4ee5\u4e0b\u539f\u56e0&#xff1a;<\/p>\n<hr \/>\n<h4>1. \u200b\u660e\u786e\u529f\u80fd\u5b9a\u4f4d\u200b<\/h4>\n<ul>\n<li>cli.py\u00a0\u76f4\u63a5\u8868\u660e\u8be5\u6587\u4ef6\u662f\u7a0b\u5e8f\u7684\u547d\u4ee4\u884c\u63a5\u53e3&#xff08;CLI&#xff09;\u5165\u53e3&#xff0c;\u5f00\u53d1\u8005\u6216\u7528\u6237\u4e00\u773c\u5c31\u80fd\u7406\u89e3\u5b83\u7684\u4f5c\u7528&#xff1a;\u901a\u8fc7\u547d\u4ee4\u884c\u4e0e\u7a0b\u5e8f\u4ea4\u4e92\u3002<\/li>\n<li>\u8fd9\u79cd\u547d\u540d\u6e05\u6670\u533a\u5206\u4e86\u5176\u4ed6\u53ef\u80fd\u7684\u5165\u53e3&#xff08;\u5982 GUI \u5165\u53e3\u3001API \u670d\u52a1\u7b49&#xff09;&#xff0c;\u7b26\u5408\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u539f\u5219\u3002<\/li>\n<\/ul>\n<hr \/>\n<h4>2. \u200b\u9075\u5faa\u884c\u4e1a\u60ef\u4f8b\u200b<\/h4>\n<ul>\n<li>\u8bb8\u591a\u5f00\u6e90\u9879\u76ee\u548c\u5de5\u5177&#xff08;\u5982\u00a0git\u3001pip\u3001docker\u00a0\u7b49&#xff09;\u90fd\u4f7f\u7528\u00a0cli.py\u00a0\u6216\u7c7b\u4f3c\u7684\u547d\u540d&#xff08;\u5982\u00a0main.py\u3001app.py&#xff09;\u4f5c\u4e3a CLI \u7684\u5165\u53e3\u6587\u4ef6\u3002<\/li>\n<li>\u8fd9\u79cd\u4e00\u81f4\u6027\u964d\u4f4e\u4e86\u5b66\u4e60\u6210\u672c&#xff0c;\u7528\u6237\u6216\u8d21\u732e\u8005\u80fd\u5feb\u901f\u627e\u5230\u7a0b\u5e8f\u7684\u542f\u52a8\u70b9\u3002<\/li>\n<\/ul>\n<hr \/>\n<h4>3. \u200b\u6a21\u5757\u5316\u8bbe\u8ba1\u200b<\/h4>\n<ul>\n<li>\u5728\u5927\u578b\u9879\u76ee\u4e2d&#xff0c;\u901a\u5e38\u4f1a\u5c06\u4e0d\u540c\u529f\u80fd\u7684\u4ee3\u7801\u5206\u79bb\u5230\u4e0d\u540c\u6a21\u5757\u4e2d\u3002\u4f8b\u5982&#xff1a;\n<ul>\n<li>cli.py&#xff1a;\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u548c\u7528\u6237\u4ea4\u4e92\u3002<\/li>\n<li>core.py&#xff1a;\u5b9e\u73b0\u6838\u5fc3\u4e1a\u52a1\u903b\u8f91\u3002<\/li>\n<li>api.py&#xff1a;\u63d0\u4f9b HTTP API \u63a5\u53e3\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u8fd9\u79cd\u5206\u79bb\u4f7f\u5f97\u4ee3\u7801\u66f4\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002<\/li>\n<\/ul>\n<hr \/>\n<h4>4. \u200b\u5de5\u5177\u94fe\u652f\u6301\u200b<\/h4>\n<ul>\n<li>\u8bb8\u591a Python \u5de5\u5177&#xff08;\u5982\u00a0setuptools\u3001click\u3001argparse&#xff09;\u9ed8\u8ba4\u6216\u63a8\u8350\u5c06 CLI \u5165\u53e3\u547d\u540d\u4e3a\u00a0cli.py&#xff0c;\u65b9\u4fbf\u901a\u8fc7\u547d\u4ee4\u884c\u76f4\u63a5\u8c03\u7528&#xff08;\u4f8b\u5982\u00a0python cli.py\u00a0\u6216\u901a\u8fc7\u5305\u540d\u8c03\u7528&#xff09;\u3002<\/li>\n<\/ul>\n<hr \/>\n<h4>5. \u200b\u4e0e\u5305\u7ed3\u6784\u534f\u8c03\u200b<\/h4>\n<ul>\n<li>\u5982\u679c\u9879\u76ee\u662f\u4e00\u4e2a Python \u5305&#xff08;\u901a\u8fc7\u00a0pip\u00a0\u5b89\u88c5&#xff09;&#xff0c;cli.py\u00a0\u901a\u5e38\u4f1a\u88ab\u653e\u5728\u5305\u7684\u6839\u76ee\u5f55\u6216\u00a0bin\/\u00a0\u76ee\u5f55\u4e0b&#xff0c;\u5e76\u901a\u8fc7\u00a0entry_points\u00a0\u5728\u00a0setup.py\u00a0\u4e2d\u6ce8\u518c\u4e3a\u547d\u4ee4\u884c\u5de5\u5177\u3002\u4f8b\u5982&#xff1a;\n<p># setup.py<br \/>\nentry_points&#061;{<br \/>\n    &#039;console_scripts&#039;: [<br \/>\n        &#039;mytool&#061;cli:main&#039;,  # \u7528\u6237\u5b89\u88c5\u540e\u53ef\u901a\u8fc7 &#096;mytool&#096; \u547d\u4ee4\u8c03\u7528 cli.py \u7684 main \u51fd\u6570<br \/>\n    ],<br \/>\n}\n<\/li>\n<\/ul>\n<hr \/>\n<h4>\u5176\u4ed6\u5e38\u89c1\u547d\u540d\u5bf9\u6bd4<\/h4>\n<ul>\n<li>main.py&#xff1a;\u901a\u7528\u5165\u53e3&#xff0c;\u4f46\u65e0\u6cd5\u76f4\u63a5\u4f53\u73b0\u662f CLI \u8fd8\u662f\u5176\u4ed6\u63a5\u53e3\u3002<\/li>\n<li>app.py&#xff1a;\u5e38\u7528\u4e8e Web \u5e94\u7528&#xff08;\u5982 Flask\/Django&#xff09;\u6216 GUI \u7a0b\u5e8f\u3002<\/li>\n<li>run.py&#xff1a;\u53ef\u80fd\u7528\u4e8e\u811a\u672c\u6216\u6d4b\u8bd5&#xff0c;\u529f\u80fd\u4e0d\u660e\u786e\u3002<\/li>\n<\/ul>\n<hr \/>\n<h4>\u603b\u7ed3<\/h4>\n<p>cli.py\u00a0\u7684\u547d\u540d\u662f\u4e00\u79cd\u6e05\u6670\u3001\u7ea6\u5b9a\u4fd7\u6210\u7684\u505a\u6cd5&#xff0c;\u5c24\u5176\u9002\u5408\u4ee5\u547d\u4ee4\u884c\u4e3a\u4e3b\u8981\u4ea4\u4e92\u65b9\u5f0f\u7684\u7a0b\u5e8f\u3002\u5b83\u76f4\u63a5\u4f20\u8fbe\u4e86\u6587\u4ef6\u7684\u7528\u9014&#xff0c;\u540c\u65f6\u4e0e\u5de5\u5177\u94fe\u548c\u9879\u76ee\u7ed3\u6784\u826f\u597d\u914d\u5408\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb60\u6b21\u3002cli.py\u7684\u547d\u540d\u662f\u4e00\u79cd\u6e05\u6670\u3001\u7ea6\u5b9a\u4fd7\u6210\u7684\u505a\u6cd5\uff0c\u5c24\u5176\u9002\u5408\u4ee5\u547d\u4ee4\u884c\u4e3a\u4e3b\u8981\u4ea4\u4e92\u65b9\u5f0f\u7684\u7a0b\u5e8f\u3002\u5b83\u76f4\u63a5\u4f20\u8fbe\u4e86\u6587\u4ef6\u7684\u7528\u9014\uff0c\u540c\u65f6\u4e0e\u5de5\u5177\u94fe\u548c\u9879\u76ee\u7ed3\u6784\u826f\u597d\u914d\u5408\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4327,3789],"topic":[],"class_list":["post-45554","post","type-post","status-publish","format-standard","hentry","category-server","tag-pytest","tag-3789"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>(pytest)\u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u8fd0\u884c\u5165\u53e3\u547d\u540d\u4e3acli.py - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/45554.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"(pytest)\u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u8fd0\u884c\u5165\u53e3\u547d\u540d\u4e3acli.py - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb60\u6b21\u3002cli.py\u7684\u547d\u540d\u662f\u4e00\u79cd\u6e05\u6670\u3001\u7ea6\u5b9a\u4fd7\u6210\u7684\u505a\u6cd5\uff0c\u5c24\u5176\u9002\u5408\u4ee5\u547d\u4ee4\u884c\u4e3a\u4e3b\u8981\u4ea4\u4e92\u65b9\u5f0f\u7684\u7a0b\u5e8f\u3002\u5b83\u76f4\u63a5\u4f20\u8fbe\u4e86\u6587\u4ef6\u7684\u7528\u9014\uff0c\u540c\u65f6\u4e0e\u5de5\u5177\u94fe\u548c\u9879\u76ee\u7ed3\u6784\u826f\u597d\u914d\u5408\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/45554.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-29T16:26:12+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/45554.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/45554.html\",\"name\":\"(pytest)\u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u8fd0\u884c\u5165\u53e3\u547d\u540d\u4e3acli.py - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-29T16:26:12+00:00\",\"dateModified\":\"2025-07-29T16:26:12+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/45554.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/45554.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/45554.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"(pytest)\u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u8fd0\u884c\u5165\u53e3\u547d\u540d\u4e3acli.py\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"(pytest)\u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u8fd0\u884c\u5165\u53e3\u547d\u540d\u4e3acli.py - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/45554.html","og_locale":"zh_CN","og_type":"article","og_title":"(pytest)\u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u8fd0\u884c\u5165\u53e3\u547d\u540d\u4e3acli.py - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb60\u6b21\u3002cli.py\u7684\u547d\u540d\u662f\u4e00\u79cd\u6e05\u6670\u3001\u7ea6\u5b9a\u4fd7\u6210\u7684\u505a\u6cd5\uff0c\u5c24\u5176\u9002\u5408\u4ee5\u547d\u4ee4\u884c\u4e3a\u4e3b\u8981\u4ea4\u4e92\u65b9\u5f0f\u7684\u7a0b\u5e8f\u3002\u5b83\u76f4\u63a5\u4f20\u8fbe\u4e86\u6587\u4ef6\u7684\u7528\u9014\uff0c\u540c\u65f6\u4e0e\u5de5\u5177\u94fe\u548c\u9879\u76ee\u7ed3\u6784\u826f\u597d\u914d\u5408\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/45554.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-29T16:26:12+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/45554.html","url":"https:\/\/www.wsisp.com\/helps\/45554.html","name":"(pytest)\u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u8fd0\u884c\u5165\u53e3\u547d\u540d\u4e3acli.py - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-29T16:26:12+00:00","dateModified":"2025-07-29T16:26:12+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/45554.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/45554.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/45554.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"(pytest)\u4e3a\u4ec0\u4e48\u7a0b\u5e8f\u8fd0\u884c\u5165\u53e3\u547d\u540d\u4e3acli.py"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/45554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=45554"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/45554\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=45554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=45554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=45554"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=45554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}