用 Pytest+Appium+Allure 做 UI 自动化的那些事~(有点干)

本文介绍了使用Pytest、Appium和Allure进行UI自动化测试的经验,涵盖了Appium的adb shell方法、元素截图、日志获取、文件传输,Pytest与Unittest的区别、初始化、参数化、用例依赖、自定义标记、错误处理及测试报告等方面,旨在帮助测试人员提升自动化测试效率。
摘要由CSDN通过智能技术生成

目录

前言:

Appium 不常见却好用的方法

Appium 直接执行 adb shell 方法

Appium 直接截取元素图片的方法

Appium 直接获取手机端日志

Appium 直接与设备传输文件

Pytest 与 Unittest 初始化上的区别

1.Pytest 与 unitest 类似,有些许区别,以下是 Pytest

2.使用 pytest.fixture()

初始化实例

1.setup_class 方式调用

2.pytest.fixture() 方式调用

Pytest 参数化方法

1.第一种方法 parametrize 装饰器参数化方法

2.第二种方法,使用 pytest hook 批量加参数化

Pytest 用例依赖关系

Pytest 自定义标记,执行用例筛选作用

1.使用 @pytest.mark 模块给类或者函数加上标记,用于执行用例时进行筛选

2.根据 test 节点选择用例

3.使用 pytest hook 批量标记用例

用例错误处理截图,app 日志等

Pytest 另一些 hook 的使用方法

1.自定义 Pytest 参数

2.Pytest 过滤测试目录

Pytest 一些常用方法

Pytest 用例优先级(比如优先登录什么的)

Pytest 用例失败重试

Pytest 其他常用参数


前言:

Pytest、Appium和Allure是一组强大的工具和框架,可以帮助开发人员实现高效的UI自动化测试。Pytest是一个功能强大的Python测试框架,Appium是一个用于移动应用自动化测试的开源工具,而Allure是一个用于生成漂亮测试报告的框架。

文本主要介绍下 Pytest+Allure+Appium 记录一些过程和经历,一些好用的方法什么的。

主要用了啥:

  • Python3
  • Appium
  • Allure-pytest
  • 用 Pytest+Appium+Allure 做 UI 自动化的那些事~(有点干)

Appium 不常见却好用的方法

Appium 直接执行 adb shell 方法
# Appium 启动时增加 --relaxed-security 参数 Appium 即可执行类似adb shell的方法
> appium -p 4723 --relaxed-security
# 使用方法
def adb_shell(self, command, args, includeStderr=False):
    """
    appium --relaxed-security 方式启动
    adb_shell('ps',['|','grep','android'])

    :param command:命令
    :param args:参数
    :param includeStderr: 为 True 则抛异常
    :return:
    """
    result = self.driver.execute_script('mobile: shell', {
        'command': command,
        'args': args,
        'includeStderr': includeStderr,
        'timeout': 5000
        })
    return result['stdout']
Appium 直接截取元素图片的方法
element = self.driver.find_element_by_id('cn.xxxxxx:id/login_sign')
pngbyte = element.screenshot_as_png
image_data = BytesIO(pngbyte)
img = Image.open(image_data)
img.save('element.png')
# 该方式能直接获取到登录按钮区域的截图

Appium 直接获取手机端日志
# 使用该方法后,手机端 logcat 缓存会清除归零,从新记录
# 建议每条用例执行完执行一边清理,遇到错误再保存减少陈余 log 输出
# Android
logcat = self.driver.get_log('logcat')

# iOS 需要安装 brew install libimobiledevice 
logcat = self.driver.get_log('syslog')

# web 获取控制台日志
logcat = self.driver.get_log('browser')

c = '\n'.join([i['message'] for i in logcat])
allure.attach(c, 'APPlog', allure.attachment_type.TEXT)
#写入到 allure 测试报告中
Appium 直接与设备传输文件
# 发送文件
#Android
driver.push_file('/sdcard/element.png', source_path='D:\works\element.png')

# 获取手机文件
png = driver.pull_file('/sdcard/element.png')
with open('element.png', 'wb') as png1:
    png1.write(base64.b64decode(png))

# 获取手机文件夹,导出的是zip文件
folder = driver.pull_folder('/sdcard/test')
with open('test.zip', 'wb') as folder1:
    folder1.write(base64.b64decode(folder))

# iOS
# 需要安装 ifuse
# > brew install ifuse 或者 > brew cask install osxfuse 或者 自行搜索安装方式

driver.push_file('/Documents/xx/element.png', source_path='D:\works\element.png')

# 向 App 沙盒中发送文件
# iOS 8.3 之后需要应用开启 UIFileSharingEnabled 权限不然会报错
bundleId = 'cn.xxx.xxx' # APP名字
driver.push_file('@{bundleId}:Documents/xx/element.png'.format(bundleId=bundleId), source_path='D:\works\element.png')


Pytest 与 Unittest 初始化上的区别

很多人都使用过 unitest 先说一下 pytest 和 unitest 在 Hook method 上的一些区别

1.Pytest 与 unitest 类似,有些许区别,以下是 Pytest
class TestExample:
    def setup(self):
        print("setup             class:TestStuff")

    def teardown(self):
        print ("teardown          class:TestStuff")

    def setup_class(cls):
        print ("setup_class       class:%s" % cls.__name__)

    def teardown_class(cls):
        print ("teardown_class    class:%s" % cls.__name__)

    def setup_method(self, method):
        print ("setup_method      method:%s" % method.__name__)

    def teardown_method(self, method):
        print ("teardown_method   method:%s" % method.__name__)
2.使用 pytest.fixture()
@pytest.fixture()
def driver_setup(request):
    request.instance.Action = DriverClient().init_driver('android')
    def driver_teardown():
        request.
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值