济南网站建设公司大全,wordpress收款,godaddy的wordpress,域名访问网站入口一、前言
最近通过群友了解到了allure这个报告#xff0c;开始还不以为然#xff0c;但还是逃不过真香定律。
经过试用之后#xff0c;发现这个报告真的很好#xff0c;很适合自动化测试结果的展示。下面说说我的探索历程吧。
选用的项目为Selenium自动化测试Pytest框架…一、前言
最近通过群友了解到了allure这个报告开始还不以为然但还是逃不过真香定律。
经过试用之后发现这个报告真的很好很适合自动化测试结果的展示。下面说说我的探索历程吧。
选用的项目为Selenium自动化测试Pytest框架实战在这个项目的基础上说allure报告。
二、allure安装
首先安装python的allure-pytest包 span stylecolor:#596172span stylebackground-color:#ffffffcode classlanguage-shellpip install allure-pytest /code/span/span
然后安装allure的command命令行程序 MacOS直接使用homebrew工具执行 brew install allure 即可安装不用配置下载包和配置环境 在GitHub下载安装程序https://github.com/allure-framework/allure2/releases
但是由于GitHub访问太慢我已经下载好并放在了群文件里面
下载完成后解压放到一个文件夹。我的路径是D:\Program Files\allure-2.13.3
然后配置环境变量 在系统变量path中添加D:\Program Files\allure-2.13.3\bin然后确定保存。
打开cmd输入allure如果结果显示如下则表示成功了 span stylecolor:#596172span stylebackground-color:#ffffffcode classlanguage-powershellC:\Users\hoouallure Usage: allure [span stylecolor:#d19a66options/span] [span stylecolor:#d19a66command/span] [span stylecolor:#d19a66command/span span stylecolor:#d19a66options/span] Options: span stylecolor:#56b6c2--help/span Print commandline help. span stylecolor:#56b6c2-q/span, span stylecolor:#56b6c2--quiet/span span stylecolor:#7171bfSwitch/span on the quiet mode. Default: false span stylecolor:#56b6c2-v/span, span stylecolor:#56b6c2--verbose/span span stylecolor:#7171bfSwitch/span on the verbose mode. Default: false span stylecolor:#56b6c2--version/span Print commandline version. Default: false Commands: generate Generate the report Usage: generate [span stylecolor:#d19a66options/span] The directories with allure results Options: span stylecolor:#56b6c2-c/span, span stylecolor:#56b6c2--clean/span Clean Allure report directory before generating a new one. Default: false span stylecolor:#56b6c2--config/span Allure commandline config path. span stylecolor:#7171bfIf/span specified overrides values from span stylecolor:#56b6c2--profile/span and span stylecolor:#56b6c2--configDirectory/span. span stylecolor:#56b6c2--configDirectory/span Allure commandline configurations directory. By default uses ALLURE_HOME directory. span stylecolor:#56b6c2--profile/span Allure commandline configuration profile. span stylecolor:#56b6c2-o/span, span stylecolor:#56b6c2--report-dir/span, span stylecolor:#56b6c2--output/span The directory to generate Allure report into. Default: allurespan stylecolor:#56b6c2-report/span serve Serve the report Usage: serve [span stylecolor:#d19a66options/span] The directories with allure results Options: span stylecolor:#56b6c2--config/span Allure commandline config path. span stylecolor:#7171bfIf/span specified overrides values from span stylecolor:#56b6c2--profile/span and span stylecolor:#56b6c2--configDirectory/span. span stylecolor:#56b6c2--configDirectory/span Allure commandline configurations directory. By default uses ALLURE_HOME directory. span stylecolor:#56b6c2-h/span, span stylecolor:#56b6c2--host/span This host will be used to span stylecolor:#7171bfstart/span web server span stylecolor:#7171bffor/span the report. span stylecolor:#56b6c2-p/span, span stylecolor:#56b6c2--port/span This port will be used to span stylecolor:#7171bfstart/span web server span stylecolor:#7171bffor/span the report. Default: span stylecolor:#d19a660/span span stylecolor:#56b6c2--profile/span Allure commandline configuration profile. open Open generated report Usage: open [span stylecolor:#d19a66options/span] The report directory Options: span stylecolor:#56b6c2-h/span, span stylecolor:#56b6c2--host/span This host will be used to span stylecolor:#7171bfstart/span web server span stylecolor:#7171bffor/span the report. span stylecolor:#56b6c2-p/span, span stylecolor:#56b6c2--port/span This port will be used to span stylecolor:#7171bfstart/span web server span stylecolor:#7171bffor/span the report. Default: span stylecolor:#d19a660/span plugin Generate the report Usage: plugin [span stylecolor:#d19a66options/span] Options: span stylecolor:#56b6c2--config/span Allure commandline config path. span stylecolor:#7171bfIf/span specified overrides values from span stylecolor:#56b6c2--profile/span and span stylecolor:#56b6c2--configDirectory/span. span stylecolor:#56b6c2--configDirectory/span Allure commandline configurations directory. By default uses ALLURE_HOME directory. span stylecolor:#56b6c2--profile/span Allure commandline configuration profile. /code/span/span
三、allure初体验
改造一下之前的测试用例代码 span stylecolor:#596172span stylebackground-color:#ffffffcode classlanguage-pythonspan stylecolor:#5c6370em#!/usr/bin/env python3/em/span span stylecolor:#5c6370em# -*- coding:utf-8 -*-/em/span span stylecolor:#7171bfimport/span re span stylecolor:#7171bfimport/span pytest span stylecolor:#7171bfimport/span allure span stylecolor:#7171bffrom/span utils.logger span stylecolor:#7171bfimport/span log span stylecolor:#7171bffrom/span common.readconfig span stylecolor:#7171bfimport/span ini span stylecolor:#7171bffrom/span page_object.searchpage span stylecolor:#7171bfimport/span SearchPage span stylecolor:#61aeeeallure.feature(span stylecolor:#3388aa测试百度模块/span)/span span stylecolor:#7171bfclass/span span stylecolor:#61aeeeTestSearch/span: span stylecolor:#61aeee pytest.fixture(scopespan stylecolor:#3388aafunction/span, autousespan stylecolor:#56b6c2True/span)/span span stylecolor:#7171bfdef/span span stylecolor:#61aeeeopen_baidu/span(self, drivers): span stylecolor:#98c379打开百度/span search SearchPage(drivers) search.get_url(ini.url) span stylecolor:#61aeee allure.story(span stylecolor:#3388aa搜索selenium结果用例/span)/span span stylecolor:#7171bfdef/span span stylecolor:#61aeeetest_001/span(self, drivers): span stylecolor:#98c379搜索/span search SearchPage(drivers) search.input_search(span stylecolor:#98c379selenium/span) search.click_search() result re.search(span stylecolor:#98c379rselenium/span, search.get_source) log.info(result) span stylecolor:#7171bfassert/span result span stylecolor:#61aeee allure.story(span stylecolor:#3388aa测试搜索候选用例/span)/span span stylecolor:#7171bfdef/span span stylecolor:#61aeeetest_002/span(self, drivers): span stylecolor:#98c379测试搜索候选/span search SearchPage(drivers) search.input_search(span stylecolor:#98c379selenium/span) log.info(span stylecolor:#7171bflist/span(search.imagine)) span stylecolor:#7171bfassert/span span stylecolor:#7171bfall/span([span stylecolor:#98c379selenium/span span stylecolor:#7171bfin/span i span stylecolor:#7171bffor/span i span stylecolor:#7171bfin/span search.imagine]) span stylecolor:#7171bfif/span __name__ span stylecolor:#98c379__main__/span: pytest.main([span stylecolor:#98c379TestCase/test_search.py/span, span stylecolor:#98c379--alluredir/span, span stylecolor:#98c379./allure/span]) os.system(span stylecolor:#98c379allure serve allure/span) /code/span/span
然后运行一下
注意如果你使用的是pycharm编辑器请跳过该运行方式直接使用.bat或.sh的方式运行 span stylecolor:#596172span stylebackground-color:#ffffffcode classlanguage-shell*** ------------------------------- generated html file: file://C:\Users\hoou\PycharmProjects\web-demotest\report.html -------------------------------- Results (12.97s): 2 passed Generating report to temp directory... Report successfully generated to C:\Users\hoou\AppData\Local\Temp\112346119265936111\allure-report Starting web server... 2020-06-18 22:52:44.500:INFO::main: Logging initialized 1958ms to org.eclipse.jetty.util.log.StdErrLog Server started at http://172.18.47.241:6202/. Press CtrlC to exit /code/span/span
命令行会出现如上提示接着浏览器会自动打开 点击左下角En即可选择切换为中文。 是不是很清爽很友好比pytest-html更舒服。
四、allure装饰器介绍 五、报告的生成和展示
刚才的两个命令
生成allure原始报告到report/allure目录下生成的全部为json或txt文件。 span stylecolor:#596172span stylebackground-color:#ffffffcode classlanguage-powershellpytest TestCase/test_search.py span stylecolor:#56b6c2--alluredir/span ./allure /code/span/span
在一个临时文件中生成报告并启动浏览器打开 span stylecolor:#596172span stylebackground-color:#ffffffcode classlanguage-powershellallure serve allure /code/span/span
但是在关闭浏览器之后这个报告就再也打不开了。不建议使用这种。
所以我们必须使用其他的命令让allure可以指定生成的报告目录。
我们在项目的根目录中创建run_case.py文件内容如下 span stylecolor:#596172span stylebackground-color:#ffffffcode classlanguage-pythonspan stylecolor:#5c6370em#!/usr/bin/env python3/em/span span stylecolor:#5c6370em# -*- coding:utf-8 -*-/em/span span stylecolor:#7171bfimport/span sys span stylecolor:#7171bfimport/span subprocess WIN sys.platform.startswith(span stylecolor:#98c379win/span) span stylecolor:#7171bfdef/span span stylecolor:#61aeeemain/span(): span stylecolor:#98c379主函数/span steps [ span stylecolor:#98c379venv\\Script\\activate/span span stylecolor:#7171bfif/span WIN span stylecolor:#7171bfelse/span span stylecolor:#98c379source venv/bin/activate/span, span stylecolor:#98c379pytest --alluredir allure-results --clean-alluredir/span, span stylecolor:#98c379allure generate allure-results -c -o allure-report/span, span stylecolor:#98c379allure open allure-report/span ] span stylecolor:#7171bffor/span step span stylecolor:#7171bfin/span steps: subprocess.run(span stylecolor:#98c379call /span step span stylecolor:#7171bfif/span WIN span stylecolor:#7171bfelse/span step, shellspan stylecolor:#56b6c2True/span) span stylecolor:#7171bfif/span __name__ span stylecolor:#98c379__main__/span: main() /code/span/span
命令释义
1、使用pytest生成原始报告里面大多数是一些原始的json数据加入--clean-alluredir参数清除allure-results历史数据。 span stylecolor:#596172span stylebackground-color:#ffffffcode classlanguage-powershellpytest span stylecolor:#56b6c2--alluredir/span allurespan stylecolor:#56b6c2-results/span span stylecolor:#56b6c2--clean-alluredir/span /code/span/span
--clean-alluredir 清除allure-results历史数据
2、使用generate命令导出HTML报告到新的目录 span stylecolor:#596172span stylebackground-color:#ffffffcode classlanguage-powershellallure generate allurespan stylecolor:#56b6c2-results/span span stylecolor:#56b6c2-o/span allurespan stylecolor:#56b6c2-report/span /code/span/span
-c 在生成报告之前先清理之前的报告目录-o 指定生成报告的文件夹
3、使用open命令在浏览器中打开HTML报告 span stylecolor:#596172span stylebackground-color:#ffffffcode classlanguage-powershellallure open allurespan stylecolor:#56b6c2-report/span /code/span/span
好了我们运行一下该文件。 span stylecolor:#596172span stylebackground-color:#ffffffcode classlanguage-powershellResults (span stylecolor:#d19a6612.85/spans): span stylecolor:#d19a662/span passed Report successfully generated to c:\Users\hoou\PycharmProjects\webspan stylecolor:#56b6c2-demotest/span\allurespan stylecolor:#56b6c2-report/span Starting web server... span stylecolor:#d19a662020/spanspan stylecolor:#56b6c2-06-18/span span stylecolor:#d19a6623/span:span stylecolor:#d19a6630/span:span stylecolor:#d19a6624.122/span:INFO::main: Logging initialized span stylecolor:#d19a66260/spanms to org.eclipse.jetty.util.log.StdErrLog Server started at http://span stylecolor:#d19a66172.18/span.span stylecolor:#d19a6647.241/span:span stylecolor:#d19a667932/span/. Press CtrlC to span stylecolor:#7171bfexit/span /code/span/span 可以看到运行成功了。
在项目中的allure-report文件夹也生成了相应的报告。 六、allure发生错误截图
上面的用例全是运行成功的没有错误和失败的那么发生了错误怎么样在allure报告中生成错误截图呢我们一起来看看。
首先我们先在config/conf.py文件中添加一个截图目录和截图文件的配置。 span stylecolor:#596172span stylebackground-color:#ffffffcode classlanguage-python span stylecolor:#7171bfclass/span span stylecolor:#61aeeeConfigManager/span(span stylecolor:#61aeeeobject/span): ... span stylecolor:#61aeee property/span span stylecolor:#7171bfdef/span span stylecolor:#61aeeescreen_path/span(self): span stylecolor:#98c379截图目录/span screenshot_dir os.path.join(self.BASE_DIR, span stylecolor:#98c379screen_capture/span) span stylecolor:#7171bfif/span span stylecolor:#7171bfnot/span os.path.exists(screenshot_dir): os.makedirs(screenshot_dir) now_time dt_strftime(span stylecolor:#98c379%Y%m%d%H%M%S/span) screen_file os.path.join(screenshot_dir, span stylecolor:#98c379{}.png/span.span stylecolor:#7171bfformat/span(now_time)) span stylecolor:#7171bfreturn/span now_time, screen_file ... /code/span/span
然后我们修改项目目录中的conftest.py span stylecolor:#596172span stylebackground-color:#ffffffcode classlanguage-pythonspan stylecolor:#5c6370em#!/usr/bin/env python3/em/span span stylecolor:#5c6370em# -*- coding:utf-8 -*-/em/span span stylecolor:#7171bfimport/span base64 span stylecolor:#7171bfimport/span pytest span stylecolor:#7171bfimport/span allure span stylecolor:#7171bffrom/span py.xml span stylecolor:#7171bfimport/span html span stylecolor:#7171bffrom/span selenium span stylecolor:#7171bfimport/span webdriver span stylecolor:#7171bffrom/span config.conf span stylecolor:#7171bfimport/span cm span stylecolor:#7171bffrom/span common.readconfig span stylecolor:#7171bfimport/span ini span stylecolor:#7171bffrom/span utils.times span stylecolor:#7171bfimport/span timestamp span stylecolor:#7171bffrom/span utils.send_mail span stylecolor:#7171bfimport/span send_report driver span stylecolor:#56b6c2None/span span stylecolor:#61aeeepytest.fixture(scopespan stylecolor:#3388aasession/span, autousespan stylecolor:#56b6c2True/span)/span span stylecolor:#7171bfdef/span span stylecolor:#61aeeedrivers/span(request): span stylecolor:#7171bfglobal/span driver span stylecolor:#7171bfif/span driver span stylecolor:#7171bfis/span span stylecolor:#56b6c2None/span: driver webdriver.Chrome() driver.maximize_window() span stylecolor:#7171bfdef/span span stylecolor:#61aeeefn/span(): driver.quit() request.addfinalizer(fn) span stylecolor:#7171bfreturn/span driver span stylecolor:#61aeeepytest.hookimpl(hookwrapperspan stylecolor:#56b6c2True/span)/span span stylecolor:#7171bfdef/span span stylecolor:#61aeeepytest_runtest_makereport/span(item): span stylecolor:#98c379 当测试失败的时候自动截图展示到html报告中 :param item: /span pytest_html item.config.pluginmanager.getplugin(span stylecolor:#98c379html/span) outcome span stylecolor:#7171bfyield/span report outcome.get_result() report.description span stylecolor:#7171bfstr/span(item.function.__doc__) extra span stylecolor:#7171bfgetattr/span(report, span stylecolor:#98c379extra/span, []) span stylecolor:#7171bfif/span report.when span stylecolor:#98c379call/span span stylecolor:#7171bfor/span report.when span stylecolor:#98c379setup/span: xfail span stylecolor:#7171bfhasattr/span(report, span stylecolor:#98c379wasxfail/span) span stylecolor:#7171bfif/span (report.skipped span stylecolor:#7171bfand/span xfail) span stylecolor:#7171bfor/span (report.failed span stylecolor:#7171bfand/span span stylecolor:#7171bfnot/span xfail): screen_img _capture_screenshot() span stylecolor:#7171bfif/span screen_img: html span stylecolor:#98c379divimg srcdata:image/png;base64,%s altscreenshot stylewidth:1024px;height:768px; /span \ span stylecolor:#98c379onclickwindow.open(this.src) alignright//div/span % screen_img extra.append(pytest_html.extras.html(html)) report.extra extra span stylecolor:#7171bfdef/span span stylecolor:#61aeeepytest_html_results_table_header/span(cells): cells.insert(span stylecolor:#d19a661/span, html.th(span stylecolor:#98c379用例名称/span)) cells.insert(span stylecolor:#d19a662/span, html.th(span stylecolor:#98c379Test_nodeid/span)) cells.pop(span stylecolor:#d19a662/span) span stylecolor:#7171bfdef/span span stylecolor:#61aeeepytest_html_results_table_row/span(report, cells): cells.insert(span stylecolor:#d19a661/span, html.td(report.description)) cells.insert(span stylecolor:#d19a662/span, html.td(report.nodeid)) cells.pop(span stylecolor:#d19a662/span) span stylecolor:#7171bfdef/span span stylecolor:#61aeeepytest_html_results_table_html/span(report, data): span stylecolor:#7171bfif/span report.passed: span stylecolor:#7171bfdel/span data[:] data.append(html.div(span stylecolor:#98c379通过的用例未捕获日志输出./span, class_span stylecolor:#98c379empty log/span)) span stylecolor:#7171bfdef/span span stylecolor:#61aeeepytest_html_report_title/span(report): report.title span stylecolor:#98c379pytest示例项目测试报告/span span stylecolor:#7171bfdef/span span stylecolor:#61aeeepytest_configure/span(config): config._metadata.clear() config._metadata[span stylecolor:#98c379测试项目/span] span stylecolor:#98c379测试百度官网搜索/span config._metadata[span stylecolor:#98c379测试地址/span] ini.url span stylecolor:#7171bfdef/span span stylecolor:#61aeeepytest_html_results_summary/span(prefix, summary, postfix): span stylecolor:#5c6370em# prefix.clear() # 清空summary中的内容/em/span prefix.extend([html.p(span stylecolor:#98c379所属部门: XX公司测试部/span)]) prefix.extend([html.p(span stylecolor:#98c379测试执行人: 随风挥手/span)]) span stylecolor:#7171bfdef/span span stylecolor:#61aeeepytest_terminal_summary/span(terminalreporter, exitstatus, config): span stylecolor:#98c379收集测试结果/span result { span stylecolor:#98c379total/span: terminalreporter._numcollected, span stylecolor:#98c379passed/span: span stylecolor:#7171bflen/span(terminalreporter.stats.get(span stylecolor:#98c379passed/span, [])), span stylecolor:#98c379failed/span: span stylecolor:#7171bflen/span(terminalreporter.stats.get(span stylecolor:#98c379failed/span, [])), span stylecolor:#98c379error/span: span stylecolor:#7171bflen/span(terminalreporter.stats.get(span stylecolor:#98c379error/span, [])), span stylecolor:#98c379skipped/span: span stylecolor:#7171bflen/span(terminalreporter.stats.get(span stylecolor:#98c379skipped/span, [])), span stylecolor:#5c6370em# terminalreporter._sessionstarttime 会话开始时间/em/span span stylecolor:#98c379total times/span: timestamp() - terminalreporter._sessionstarttime } span stylecolor:#7171bfprint/span(result) span stylecolor:#7171bfif/span result[span stylecolor:#98c379failed/span] span stylecolor:#7171bfor/span result[span stylecolor:#98c379error/span]: send_report() span stylecolor:#7171bfdef/span span stylecolor:#61aeee_capture_screenshot/span(): span stylecolor:#98c379截图保存为base64/span now_time, screen_file cm.screen_path driver.save_screenshot(screen_file) allure.attach.file(screen_file, span stylecolor:#98c379失败截图{}/span.span stylecolor:#7171bfformat/span(now_time), allure.attachment_type.PNG) span stylecolor:#7171bfwith/span span stylecolor:#7171bfopen/span(screen_file, span stylecolor:#98c379rb/span) span stylecolor:#7171bfas/span f: imagebase64 base64.b64encode(f.read()) span stylecolor:#7171bfreturn/span imagebase64.decode() /code/span/span
来看看我们修改了什么
我们修改了_capture_screenshot函数
在里面我们使用了webdriver截图生成文件并使用allure.attach.file方法将文件添加到了allure测试报告中。
并且我们还返回了图片的base64编码这样可以让pytest-html的错误截图和allure都能生效。
运行一次得到两份报告一份是简单的一份是好看内容丰富的。
现在我们在测试用例中构建一个预期的错误测试一个我们的这个代码。 修改test_002测试用例 span stylecolor:#596172span stylebackground-color:#ffffffcode classlanguage-python span stylecolor:#7171bfassert/span span stylecolor:#7171bfnot/span span stylecolor:#7171bfall/span([span stylecolor:#98c379selenium/span span stylecolor:#7171bfin/span i span stylecolor:#7171bffor/span i span stylecolor:#7171bfin/span search.imagine]) /code/span/span
运行一下 可以看到allure报告中已经有了这个错误的信息。
再来看看pytest-html中生成的报告 可以看到两份生成的报告都附带了错误的截图真是鱼和熊掌可以兼得呢。
好了到这里可以说allure的报告就先到这里了以后发现allure其他的精彩之处我再来分享。
总结
感谢每一个认真阅读我文章的人
作为一位过来人也是希望大家少走一些弯路如果你不想再体验一次学习时找不到资料没人解答问题坚持几天便放弃的感受的话在这里我给大家分享一些自动化测试的学习资源希望能给你前进的路上带来帮助。 这份文档对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库这个仓库也陪伴我走过了最艰难的路程希望也能帮助到你 以上均可以分享只需要你搜索vx公众号程序员雨果即可免费领取