文本主要介绍下 Pytest+Allure+Appium 记录一些过程和经历。

法主要用了啥:

Python3
Appium
Allure-pytest
Pytest

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.instance.Action.quit()
request.addfinalizer(driver_teardown)

初始化实例
1.setup_class 方式调用
class Singleton(object):
“”“单例
ElementActions 为自己封装操作类”“”
Action = None

def new(cls, *args, **kw):
if not hasattr(cls, ‘_instance’):
desired_caps={}
host = “http://localhost:4723/wd/hub”
driver = webdriver.Remote(host, desired_caps)
Action = ElementActions(driver, desired_caps)
orig = super(Singleton, cls)
cls._instance = orig.new(cls, *args, **kw)
cls._instance.Action = Action
return cls._instance

class DriverClient(Singleton):
pass

测试用例中调用

class TestExample:
def setup_class(cls):
cls.Action = DriverClient().Action

def teardown_class(cls):
cls.Action.clear()

def test_demo(self)
self.Action.driver.launch_app()
self.Action.set_text(‘123’)

2.pytest.fixture() 方式调用
class DriverClient():

def init_driver(self,device_name):
desired_caps={}
host = “http://localhost:4723/wd/hub”
driver = webdriver.Remote(host, desired_caps)
Action = ElementActions(driver, desired_caps)
return Action

#该函数需要放置在 conftest.py, pytest 运行时会自动拾取
@pytest.fixture()
def driver_setup(request):
request.instance.Action = DriverClient().init_driver()
def driver_teardown():
request.instance.Action.clear()
request.addfinalizer(driver_teardown)

测试用例中调用

#该装饰器会直接引入driver_setup函数
@pytest.mark.usefixtures(‘driver_setup’)
class TestExample:

def test_demo(self):
self.Action.driver.launch_app()
self.Action.set_text(‘123’)

Pytest 参数化方法
1.第一种方法 parametrize 装饰器参数化方法
@pytest.mark.parametrize((‘kewords’), [(u"小明"), (u"小红"), (u"小白")])
def test_kewords(self,kewords):
print(kewords)

#多个参数
@pytest.mark.parametrize(“test_input,expected”, [
(“3+5”, 8),
(“2+4”, 6),
(“6*9”, 42),
])
def test_eval(test_input, expected):
assert eval(test_input) == expected

2.第二种方法,使用 pytest hook 批量加参数化
#conftest.py
def pytest_generate_tests(metafunc):
“”"
使用 hook 给用例加加上参数
metafunc.cls.params 对应类中的 params 参数

“”"
try:
if metafunc.cls.params and metafunc.function.name in metafunc.cls.params: ## 对应 TestClass params
funcarglist = metafunc.cls.params[metafunc.function.name]
argnames = list(funcarglist[0])
metafunc.parametrize(argnames, [[funcargs[name] for name in argnames] for funcargs in funcarglist])
except AttributeError:
pass

#test_demo.py
class TestClass:
“”"
:params 对应 hook 中 metafunc.cls.params
“”"
#params = Parameterize(‘TestClass.yaml’).getdata()

params = {
‘test_a’: [{‘a’: 1, ‘b’: 2}, {‘a’: 1, ‘b’: 2}],
‘test_b’: [{‘a’: 1, ‘b’: 2}, {‘a’: 1, ‘b’: 2}],
}
def test_a(self, a, b):
assert a == b
def test_b(self, a, b):
assert a == b

Pytest 用例依赖关系
使用 pytest-dependency 库可以创造依赖关系
当上层用例没通过,后续依赖关系用例将直接跳过,可以跨 Class 类筛选
如果需要跨.py 文件运行 需要将 site-packages/pytest_dependency.py 文件的

class DependencyManager(object):
“”“Dependency manager, stores the results of tests.
“””

ScopeCls = {‘module’:pytest.Module, ‘session’:pytest.Session}

@classmethod
def getManager(cls, item, scope=‘session’): # 这里修改成 session

如果

pip install pytest-dependency

class TestExample(object):

@pytest.mark.dependency()
def test_a(self):
assert False

@pytest.mark.dependency()
def test_b(self):
assert False

@pytest.mark.dependency(depends=[“TestExample::test_a”])
def test_c(self):
#TestExample::test_a 没通过则不执行该条用例
#可以跨 Class 筛选
print(“Hello I am in test_c”)

@pytest.mark.dependency(depends=[“TestExample::test_a”,“TestExample::test_b”])
def test_d(self):
print(“Hello I am in test_d”)

pytest -v test_demo.py
2 failed

  • test_1.py:6 TestExample.test_a
  • test_1.py:10 TestExample.test_b
    2 skipped

Pytest 自定义标记,执行用例筛选作用
1.使用 @pytest.mark 模块给类或者函数加上标记,用于执行用例时进行筛选
@pytest.mark.webtest
def test_webtest():
pass

@pytest.mark.apitest
class TestExample(object):
def test_a(self):
pass

@pytest.mark.httptest
def test_b(self):
pass

仅执行标记 webtest 的用例

pytest -v -m webtest

Results (0.03s):
1 passed
2 deselected

执行标记多条用例

pytest -v -m “webtest or apitest”

Results (0.05s):
3 passed

仅不执行标记 webtest 的用例

pytest -v -m “not webtest”

Results (0.04s):
2 passed
1 deselected

不执行标记多条用例

pytest -v -m “not webtest and not apitest”

Results (0.02s):
3 deselected

2.根据 test 节点选择用例
pytest -v Test_example.py::TestClass::test_a
pytest -v Test_example.py::TestClass
pytest -v Test_example.py Test_example2.py

3.使用 pytest hook 批量标记用例
#conftet.py

def pytest_collection_modifyitems(items):
“”"
获取每个函数名字,当用例中含有该字符则打上标记
“”"
for item in items:
if “http” in item.nodeid:
item.add_marker(pytest.mark.http)
elif “api” in item.nodeid:
item.add_marker(pytest.mark.api)

class TestExample(object):
def test_api_1(self):
pass

def test_api_2(self):
pass

def test_http_1(self):
pass

def test_http_2(self):
pass
def test_demo(self):
pass

仅执行标记 api 的用例

pytest -v -m api
Results (0.03s):
2 passed
3 deselected
可以看到使用批量标记之后,测试用例中只执行了带有 api 的方法

用例错误处理截图,app 日志等
1.第一种使用 python 函数装饰器方法

def monitorapp(function):
“”"
用例装饰器,截图,日志,是否跳过等
获取系统log,Android logcat、ios 使用syslog
“”"

@wraps(function)
def wrapper(self, *args, **kwargs):
try:
allure.dynamic.description(‘用例开始时间:{}’.format(datetime.datetime.now()))
function(self, *args, **kwargs)
self.Action.driver.get_log(‘logcat’)
except Exception as E:
f = self.Action.driver.get_screenshot_as_png()
allure.attach(f, ‘失败截图’, allure.attachment_type.PNG)
logcat = self.Action.driver.get_log(‘logcat’)
c = ‘\n’.join([i[‘message’] for i in logcat])
allure.attach(c, ‘APPlog’, allure.attachment_type.TEXT)
raise E
finally:
if self.Action.get_app_pid() != self.Action.Apppid:
raise Exception(‘设备进程 ID 变化,可能发生崩溃’)
return wrapper

2.第二种使用 pytest hook 方法 (与方法一选一)

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
Action = DriverClient().Action
outcome = yield
rep = outcome.get_result()
if rep.when == “call” and rep.failed:
f = Action.driver.get_screenshot_as_png()
allure.attach(f, ‘失败截图’, allure.attachment_type.PNG)
logcat = Action.driver.get_log(‘logcat’)
c = ‘\n’.join([i[‘message’] for i in logcat])
allure.attach(c, ‘APPlog’, allure.attachment_type.TEXT)
if Action.get_app_pid() != Action.apppid:
raise Exception(‘设备进程 ID 变化,可能发生崩溃’)

Pytest 另一些 hook 的使用方法
1.自定义 Pytest 参数

pytest -s -all

#content of conftest.py
def pytest_addoption(parser):
“”"
自定义参数
“”"
parser.addoption(“–all”, action=“store_true”,default=“type1”,help=“run all combinations”)

def pytest_generate_tests(metafunc):
if ‘param’ in metafunc.fixturenames:
if metafunc.config.option.all: # 这里能获取到自定义参数
paramlist = [1,2,3]
else:
paramlist = [1,2,4]
metafunc.parametrize(“param”,paramlist) # 给用例加参数化

#怎么在测试用例中获取自定义参数呢
#content of conftest.py
def pytest_addoption(parser):
“”"
自定义参数
“”"
parser.addoption(“–cmdopt”, action=“store_true”,default=“type1”,help=“run all combinations”)

@pytest.fixture
def cmdopt(request):
return request.config.getoption(“–cmdopt”)

#test_sample.py 测试用例中使用
def test_sample(cmdopt):
if cmdopt == “type1”:
print(“first”)
elif cmdopt == “type2”:
print(“second”)
assert 1

pytest -q --cmdopt=type2
second
.
1 passed in 0.09 seconds

2.Pytest 过滤测试目录
#过滤 pytest 需要执行的文件夹或者文件名字
def pytest_ignore_collect(path,config):
if ‘logcat’ in path.dirname:
return True #返回 True 则该文件不执行

Pytest 一些常用方法
Pytest 用例优先级(比如优先登录什么的)

pip install pytest-ordering

@pytest.mark.run(order=1)
class TestExample:
def test_a(self):

Pytest 用例失败重试
#原始方法
pytet -s test_demo.py
pytet -s --lf test_demo.py #第二次执行时,只会执行失败的用例
pytet -s --ll test_demo.py #第二次执行时,会执行所有用例,但会优先执行失败用例
#使用第三方插件
pip install pytest-rerunfailures #使用插件
pytest --reruns 2 # 失败case重试两次

Pytest 其他常用参数
pytest --maxfail=10 #失败超过10次则停止运行
pytest -x test_demo.py #出现失败则停止

最后感谢每一个认真阅读我文章的人,下面这个网盘链接也是我费了几天时间整理的非常全面的,希望也能帮助到有需要的你!

这些资料,对于想转行做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助……

如果你不想一个人野蛮生长,找不到系统的资料,问题得不到帮助,坚持几天便放弃的感受的话,可以点击下方小卡片加入我们群,大家可以一起讨论交流,里面会有各种软件测试资料和技术交流。

敲字不易,如果此文章对你有帮助的话,点个赞收个藏来个关注,给作者一个鼓励。也方便你下次能够快速查找。

自学推荐B站视频:

零基础转行软件测试:38天自学完软件测试,拿到了字节的测试岗offer,堪称B站最好的视频!

自动化测试进阶:已上岸华为,涨薪20K,2022最适合自学的python自动化测试教程,自己花16800买的,无偿分享

【自动化测试】Pytest+Appium+Allure 做 UI 自动化的那些事相关推荐

  1. 深圳软件测试培训:Pytest+Appium+Allure 做 UI 自动化的那些事

    深圳软件测试培训:Pytest+Appium+Allure 做 UI 自动化的那些事 文本主要介绍下 Pytest+Allure+Appium 记录一些过程和经历. 法主要用了啥: Python3 A ...

  2. 用 Pytest+Appium+Allure 做 UI 自动化测试的那些事儿

    本文首发于 TesterHome 社区, 文末有福利 !链接 https://testerhome.com/topics/19327 前言 做 UI 自动化测试有段时间了,在 TesterHome 社 ...

  3. 自动化测试难?WPS的UI自动化落地方案甩给你

    项目背景分析 UI自动化测试,即通过模拟手动操作用户UI界面的方式,以代码方式实现自动操作和验证的一种自动化测试手段.如今互联网的主战场已经从web端逐渐过渡到了app端.现在,app在UI自动化方面 ...

  4. 面试中有关UI自动化的那些事 ~

    面试官过程中UI自动化必问的几个问题总结: 一.有做过UI自动化吗?怎么做的?这个问题在面试中问UI自动化相关技能的时候常常被问到,那该如何去回答呢? 接下来我就UI自动化相关的一些面试的问题做一个解 ...

  5. 做UI自动化一定要跨过这些坑

    一.引子 UI自动化,在移动互联网时代的今天,一直都是在各大测试测试社区最为火爆的一个TOPIC.甚至在测试同行面前一提起自动化,大家就会自然而然的问:"恩,你们是用的什么框架?appium ...

  6. Python+Requests+Pytest+YAML+Allure实现接口自动化

    作者:wintest 链接:https://www.cnblogs.com/wintest/p/13423231.html 本项目实现接口自动化的技术选型:Python+Requests+Pytest ...

  7. bcb6通过https接口post数据_Python+Requests+Pytest+YAML+Allure实现接口自动化

    点击上方"编程派",选择设为"设为星标" 优质文章,第一时间送达! 本项目实现接口自动化的技术选型:Python+Requests+Pytest+YAML+Al ...

  8. 小心!做UI自动化一定要跨过这些坑

    楚杰 2017年03月24日 一.引子 UI自动化,在移动互联网时代的今天,一直都是在各大测试测试社区最为火爆的一个TOPIC.甚至在测试同行面前一提起自动化,大家就会自然而然的问:"恩,你 ...

  9. python做ui自动化_[python]RobotFramework自定义库实现UI自动化

    1.安装教程 环境搭建不多说,网上资料一大堆,可参考https://www.cnblogs.com/puresoul/p/3854963.html,写的比较详细,值得推荐.目前python3是不支持r ...

最新文章

  1. rhel5 配置yum到centos源
  2. [转]FFT倒序算法—雷德算法
  3. python3数据类型:Dictionary(字典)
  4. MariaDB10 主从配置
  5. python中ls是什么_使用Python代码实现Linux中的ls遍历目录命令的实例代码
  6. Java - 常用工具类 - 集合框架
  7. html正则判断全数字,javascript如何判断是不是数字?
  8. flume监控一个文件实时采集新增的数据输出到控制台
  9. 微信公开课讲师王泓渊:小游戏开放能力
  10. Java多线程-while死循环
  11. 北京2020积分落户名单
  12. 概率论在实际生活的例子_日常生活中的概率统计
  13. 移动端VIN码识别应用范围
  14. 译文Deep Learning in Bioinformatics --深度学习在生物信息学领域的应用(1)
  15. 概率图模型(D分离)
  16. 机器学习如何提高GPU利用率
  17. 软件测试需求频繁变更,测试中如何应对需求变更问题
  18. 斯坦福NLP名课带学详解 | CS224n 第11讲 - NLP中的卷积神经网络(NLP通关指南·完结)
  19. 全志JAVA_全志R11处理器参数详细说明
  20. 9月27日科技资讯|余承东吐槽苹果续航;贾扬清担任阿里巴巴开源技术委员会负责人;React Native 0.61.0 发布

热门文章

  1. java 泛型方法 实例化_Java让泛型实例化的方法
  2. Android的显示色彩位数
  3. 安卓恢复大师怎样恢复手机删除的照片
  4. python调用jsonrpc接口_微信小程序通过jsonrpc调用python服务端接口
  5. 怎么做一份漂亮的地质图
  6. 精神小伙: 写代码是世界上最好的工作, 不接受反驳
  7. uniapp开发写了key 但微信小程序时警告-Now you can provide attr `wx:key` for a `wx:for` to improve performance.
  8. 如何配置在线Yum源?
  9. PaperTigerOS(第四天)
  10. Cocos Creator 3D 粒子系统初战,不要钱的酷炫火焰拿走不谢!