{"id":416,"date":"2019-05-07T13:21:56","date_gmt":"2019-05-07T05:21:56","guid":{"rendered":"https:\/\/www.yinyubo.cn\/?p=416"},"modified":"2022-05-17T09:42:45","modified_gmt":"2022-05-17T01:42:45","slug":"pytest","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=416","title":{"rendered":"jenkins\u8fdb\u9636\u4e4bdocker\u8fd0\u884cpytest\u5e76\u4e14\u51faallure\u62a5\u544a"},"content":{"rendered":"<h1>\u80cc\u666f<\/h1>\n<p>\u6700\u8fd1\u60f3\u505a\u4e00\u4e2a\u7b80\u5355\u7684pytest \u6d4b\u8bd5\uff0c\u7528allure\u51fa\u62a5\u544a\uff0c\u7ed3\u679c\u53d1\u73b0\u7f51\u4e0a\u7684\u65b9\u6cd5\u90fd\u662f\u5728windows\u4e0a\u88c5jenkins,\u7136\u540e\u7528jenkins\u8dd1\u4e00\u4e2a\u672c\u5730\u7684\u8fd0\u884c\u73af\u5883\u3002\u8fd9\u79cd\u505a\u6cd5\u660e\u663e\u5f88\u4e0d2019\u5e74\u3002\u4e8e\u662f\u6211\u51b3\u5b9a\u505a\u4e00\u4e2a\u5728jenkins\u4e0a\u4f7f\u7528docker\u8fd0\u884cpytest\uff0c\u7136\u540e\u518d\u51faallure\u62a5\u544a\u7684\u6587\u7ae0\u3002<\/p>\n<h1>\u601d\u8def<\/h1>\n<ol>\n<li>\u5728\u4e00\u53f0\u7535\u8111\u4e0a\u5b89\u88c5jenkins,\u53ef\u4ee5\u53c2\u8003\u6211\u7684\u6587\u7ae0https:\/\/www.yinyubo.cn\/?p=268<\/li>\n<li>\u51c6\u5907python\u4ee3\u7801\uff0c\u653e\u5230git\u4e0a\uff0cjenkins\u901a\u8fc7git\u62c9\u53d6\u4ee3\u7801\u3002\u4ee3\u7801\u4e2d\u5305\u542bDockerfile,\u6d4b\u8bd5\u7528\u4f8b,requirements.txt<\/li>\n<li>\u62c9\u53d6\u4ee3\u7801\u4e4b\u540e\uff0c\u901a\u8fc7Dockerfile\u4ea7\u751f\u4e00\u4e2apython\u7684\u955c\u50cf\uff0c\u5e76\u4e14\u5728\u955c\u50cf\u4e2d\u901a\u8fc7requirements.txt\u53bb\u5b89\u88c5pytest,allure\u7684\u76f8\u5173\u4f9d\u8d56<\/li>\n<li>\u901a\u8fc7\u8fd9\u4e2a\u955c\u50cf\uff0c\u4ea7\u751f\u4e00\u4e2a\u5bb9\u5668\uff0c<span style=\"color: #ff0000;\">\u5bb9\u5668\u547d\u4ee4\u8fd0\u884cpytest<\/span>\uff0c\u4ea7\u751f\u6d4b\u8bd5\u62a5\u544a\uff0c\u5e76\u4e14\u6302\u8f7d\u5230jenkins\u670d\u52a1\u5668\u4e0a.<\/li>\n<li>(<span style=\"color: #ff0000;\">\u6ce8\u610f\uff0c\u8fd9\u91cc\u4e3a\u4ec0\u4e48\u8981\u5728\u5bb9\u5668\u91cc\u8fd0\u884cpytest\u5462\uff1f\u56e0\u4e3a\u5982\u679c\u5728Dockerfile\u91cc\u8fd0\u884cpytest\uff0c\u4e00\u65e6\u4ea7\u751f\u6d4b\u8bd5\u5931\u8d25\uff0c\u662f\u4f1a\u5bfc\u81f4jenkins\u9000\u51fashell\u6267\u884c\u7684<\/span>)<\/li>\n<li>Jenkins\u901a\u8fc7allure\u63d2\u4ef6\u8bfb\u53d6\u7b2c4\u6b65\u4ea7\u751f\u7684\u6d4b\u8bd5\u62a5\u544a\u3002\u751f\u6210allure\u62a5\u544a<\/li>\n<\/ol>\n<h1>\u5177\u4f53\u6b65\u9aa4<\/h1>\n<ol>\n<li><strong>\u7b2c\u4e00\u6b65\u5ffd\u7565\u3002\u8bf7\u53c2\u8003\u6587\u7ae0https:\/\/www.yinyubo.cn\/?p=268<\/strong><\/li>\n<li><strong>\u51c6\u5907python\u4ee3\u7801\u3002\u4ea7\u751fDockerfile,\u6d4b\u8bd5\u7528\u4f8b,requirements.txt\uff0c\u5982\u4e0b\u56fe<\/strong><\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-417 size-full\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/Image-9-1.png\" alt=\"\" width=\"443\" height=\"387\" \/><br \/>\nDockerfile\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">FROM python:3.7.3\nWORKDIR .\nADD . .\nRUN pip install -r requirements.txt\nCMD [\"pytest\", \"-q\",\"TestCase\/AIMP\/Connect\/AIMP_Connect_01.py\",\"--alluredir\",\"allure-results\"]<\/code><\/pre>\n<p>requirements.txt\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:python decode:true \"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">allure-pytest==2.6.2\nallure-python-commons==2.6.2\natomicwrites==1.3.0\nattrs==19.1.0\ncolorama==0.4.1\nmore-itertools==7.0.0\npluggy==0.9.0\npy==1.8.0\npytest==4.4.1\nsix==1.12.0\n<\/code><\/pre>\n<p>\u6d4b\u8bd5\u7528\u4f8b\u53ef\u4ee5\u5982\u4e0b<\/p>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">import pytest\ndef test_success():\n    \"\"\"this test succeeds\"\"\"\n    print(1)\n    assert True\ndef test_failure():\n    print(2)\n    \"\"\"this test fails\"\"\"\n    assert False\ndef test_skip():\n    print(3)\n    \"\"\"this test is skipped\"\"\"\n    pytest.skip('for a reason!')\ndef test_broken():\n    raise Exception('oops')\ndef test_success2():\n    print(4)\n    assert True<\/code><\/pre>\n<p><strong>\u00a0 \u00a0 \u00a03.jenkins\u914d\u7f6e\uff0c\u8fd9\u91cc\u5982\u4f55\u62c9\u53d6git\u4ee3\u7801\u5c31\u4e0d\u5199\u4e86\u3002\u8fd9\u4e2a\u5f88\u7b80\u5355\uff0c\u4e3b\u8981\u662f\u6784\u5efa\u547d\u4ee4\u548c\u6784\u5efa\u540e\u64cd\u4f5c\uff0c\u8fd9\u91cc\u6211\u590d\u5236\u4e86\u6211\u7684\u6784\u5efa\u547d\u4ee4\u5982\u4e0b\uff1a<\/strong><\/p>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">name=\"apitest\"\ndocker build -t $name .\ndocker run -d --name $name -v $PWD\/allure-results:\/allure-results $name<\/code><\/pre>\n<p>\u6548\u679c\u5982\u56fe\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-418 size-full\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/Image-10-1.png\" alt=\"\" width=\"1413\" height=\"712\" \/><br \/>\n<strong> \u00a0 \u00a0 4.\u8fd0\u884cjob\uff0c\u67e5\u770ballure\u7ed3\u679c<\/strong><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-419 size-large\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/Image-11-1024x516.png\" alt=\"\" width=\"900\" height=\"454\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u6700\u8fd1\u60f3\u505a\u4e00\u4e2a\u7b80\u5355\u7684pytest \u6d4b\u8bd5\uff0c\u7528allure\u51fa\u62a5\u544a\uff0c\u7ed3\u679c\u53d1\u73b0\u7f51\u4e0a\u7684\u65b9\u6cd5\u90fd\u662f\u5728windows\u4e0a\u88c5j [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,5],"tags":[],"class_list":["post-416","post","type-post","status-publish","format-standard","hentry","category-k8s","category-python"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=416"}],"version-history":[{"count":2,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/416\/revisions"}],"predecessor-version":[{"id":913,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/416\/revisions\/913"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}