目录
1.Pytest 与 unitest 类似,有些许区别,以下是 Pytest
1.使用 @pytest.mark 模块给类或者函数加上标记,用于执行用例时进行筛选
前言:
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.insta