程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-05(1)

python+appium自动化测试-pytest+allure测试报告【建议收藏】

发布于2021-06-07 22:01     阅读(476)     评论(0)     点赞(28)     收藏(2)


来自APP Android端自动化测试初学者的笔记,写的不对的地方大家多多指教哦 之前发布的python+appium自动化测试-pytest+allure测试报告有不懂的朋友可以加软件测试群:175317069 群里大牛分享经验,还有海量免费的软件测试资源。

一、Allure安装

1、pytest和allure插件安装

  1. pip install allure-pytest
  2. pip install pytest
  3. 复制代码

2、Allure帮助文档

  1. https://docs.qameta.io/allure/#_about
  2. 复制代码

3、Allure安装

a.scoop install allure b.使用安装包安装

image.png

  • allure2下载下来是一个zip的压缩包,我们要解压至自己的文件目录下(可解压放至项目的测试用例下或python安装目录下),自己可找到文件即可。
  • 打开allure2目录,找到bin目录,复制bin文件目录, 然后进行环境变量的配置,设置环境变量的目的就是让系统无论在哪个目录下都可以运行allure2。
  • 环境变量设置:(桌面——我的电脑——右键属性——高级系统配置——环境变量——系统变量——Path——编辑环境变量——把我们上面复制的目录路径新增至环境变量中即可)

image.png

  • 配置好后,打开cmd终端,输入allure,出现以下帮助文档,就说明配置成功了。

image.png

若后续有新版本,建议使用最新的版本

二、生成json格式的测试报告

运行环境:

  • 设备:U4AIUKFAL7W4MJLR
  • 测试app:微博APP(V10.12.0)Andriod版
  • 测试功能:账号密码登录

方法一:在终端(terminal),生成json格式测试报告

终端(terminal)输入以下内容,运行

  1. pytest 运行的py文件 --alluredir=测试报告存放地址
  2. 例如:
  3. pytest add_weibo_test.py --alluredir=../report/json
  4. 复制代码

运行测试用例,在测试报告存放位置会生成一份或多份json或xml格式的测试报告

image.png

方法二:在测试用例配置Additional Arguments,生成json格式测试报告

1.选择需要运行的测试用例,右键点击Create Run Configuration:"测试用例文件名“

image.png

2.进入后在Additional Arguments输入:- -alluredir=生成的json格式测试报告存放的位置

image.png

3.设置完后,点击APPLY→OK,在测试函数中运行测试文件

image.png

运行后在测试报告存放位置会生成一份或多份json或xml格式的测试报告

image.png

三、测试报告由json格式转换为html格式

在终端(terminal)转换

1.测试用例运行完成生成json格式的测试报告后,打开terminal,输入命令:

  1. allure generate ./report/ -o ./report/html --clean
  2. ./report/:表示执行需要转换的文件所在的位置,需要转换的文件在report文件夹中
  3. ./report/html:表示转换成功的html文件存放的位置,即存放在report下的html文件夹中
  4. --clean:表示清除之前的测试报告,因为重复生成相同的测试报告会报错
  5. 复制代码

注意:在terminal可以通过cd返回上一级或进入其它文件

2.执行完成后,在report文件夹下会生成一个html文件,在html目录下会生成index.html文件,即为可视化报告,如下图所示

image.png

3.打开html文件,右键点击index.html文件,选择open in Broswer,选择Chrome浏览器,如下图

image.png

4.谷歌浏览器打开后的测试报告图片呈现为下图:

image.png

四、Allure相关注解

1. @allure.feature:用于描述被测试产品需求

2. @allure.story:用于描述feature的用户场景,即测试需求,与feature是父子关系

3. @allure.title:用户描述测试用例的标题,不设置默认为用例名称

4. @allure.description:用于对测试用例的一些附加描述

5. @allure.step:用于将一些通用的函数作为测试步骤输出到报告,调用此函数的地方会向报告中输出步骤

  • with allure.step:用于描述测试步骤,将会输出到报告中
  • allure.attach:用于向测试报告中输入一些附加的信息,通常是一些测试数据,截图等

代码如下:

  1. # 手机账号密码登录测试用例
  2. import allure
  3. import pytest
  4. from common.init import AppStart
  5. @allure.feature("这是测试feature")
  6. class TestAccountPwd:
  7. def setup_class(self):
  8. self.account_login_page = AppStart.start().enter_account_login()
  9. @allure.story("story_one")
  10. @allure.title("title_one")
  11. def test_one(self):
  12. with allure.step("step--输入账号"):
  13. allure.attach("123123231321313", "账号")
  14. account = "123123231321313"
  15. with allure.step("step--输入密码"):
  16. pwd = "asdfgh"
  17. self.account_login_page.input_account_pwd(account, pwd)
  18. with allure.step("step--断言"):
  19. allure.attach("手机格式有问题,若非中国大陆手机号码请点击国际手机登录", "期望结果")
  20. assert self.account_login_page.get_bounced_context() == "手机格式有问题,若非中国大陆手机号码请点击国际手机登录"
  21. print("\naccount的值:", account, "\npwd的值:", pwd)
  22. @allure.story("story_two")
  23. @allure.title("title_two")
  24. @allure.step("这是测试step")
  25. def test_two(self):
  26. account = "w124hhh77"
  27. pwd = "asdfg"
  28. self.account_login_page.input_account_pwd(account, pwd)
  29. assert self.account_login_page.get_bounced_context() == "你尚未注册微博,是否立即注册"
  30. print("\naccount的值:", account, "\npwd的值:", pwd)
  31. @allure.story("story_three")
  32. @allure.title("title_three")
  33. def test_three(self):
  34. account = "hhhhhhhhh"
  35. pwd = "asdfg"
  36. self.account_login_page.input_account_pwd(account, pwd)
  37. assert self.account_login_page.get_bounced_context() == "你尚未注册微博,是否立即注册"
  38. print("\naccount的值:", account, "\npwd的值:", pwd)
  39. @allure.story("story_four")
  40. @allure.title("title_four")
  41. @allure.description("description")
  42. def test_four(self):
  43. account = "15059941156"
  44. pwd = "123123"
  45. self.account_login_page.input_account_pwd(account, pwd)
  46. assert self.account_login_page.get_account_pwd_tips() == "帐号或密码错误"
  47. print("\naccount的值:", account, "\npwd的值:", pwd)
  48. def teardown_class(self):
  49. AppStart.quit()
  50. if __name__ == '__main__':
  51. pytest.main(["account_pwd_test.py"])
  52. 复制代码

1.@allure.feature、@allure.story、@allure.title结果如下:

image.png

2.@allure.description结果如下

image.png

3.@allure.step结果如下:

image.png

4.with allure.step和allure.attach结果如下:

image.png


 



所属网站分类: 技术文章 > 博客

作者:来呀来呀开心吧

链接:http://www.phpheidong.com/blog/article/90143/e9dccca30552454f00cb/

来源:php黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

28 0
收藏该文
已收藏

评论内容:(最多支持255个字符)