Appium 并发多进程基于 Pytest框架
前言:
之前通过重写unittest的初始化方法加入设备参数进行并发,实现了基于unittest的appium多设备并发,但是考虑到unittest的框架实在过于简陋,也不方便后期的Jenkins的持续集成,所以想换一个框架来使用。
那么通过调研,pyhon+pytest+allure 这套框架很不错,pytest是一个单元测试框架,他可以集成很多插件,包括出错重试,参数化,等。在此特别是基于他的allure插件,能够和Jenkins完美兼容,生成美观强大的测试报告。
改造思路:
pytest框架和unittest框架明显不同,通过命令行启动,读取响应目录下的test开头的文件,进行执行用例。
而unittest却是通过将用例加载到TestSuite中,运行随测试集来执行用例
所以这边多进程就要换一种思路进行了。
基于pytest的结构和运行方式,那么思路如下:
运行方式:
1. pytest目录下会先加载conftest.py运行。
2. 该目录下加载test开头的py文件
3. 加载文件中Test开头的类
4. 加载Test类下test开头的方法
5. 通过命令行pytest.main([1, 2 ,3])带入1 2 3参数进行运行
解决思路:
1. 通过命令行把不同设备的参数传递给conftest.py
2. conftest中,使用传递过来的设备参数,连接设备到appium,并生成driver对象
3. 在各自的测试类和测试方法中,调用driver对象,进行测试操作
4. 生成allure测试报告
实现:
1. 通过命令行传递参数:
run中的设备池:
def devices_Pool():
devices_list = []
for i in range(0, len(getDevices())):
_initApp = {}
_initCaps = {}
_initApp["devices"] = getDevices()[i]
_initCaps["deviceName"] = getDevices()[i]
_initCaps["platformVersion"] = getPhoneInfo(devices=_initCaps["deviceName"])["release"]
_initCaps["platformName"] = "Android"
_initApp["port"] = str(random.randint(4700, 4900))
_initApp["bport"] = str(random.randint(4700, 4900))
_initApp["systemPort"] = str(random.randint(4700, 4900))
_initCaps["automationName"] = "UiAutomator2"
_initCaps["appPackage"] = 'cn.vsx.vc'
_initCaps["appActivity"] = '.activity.RegistActivity'
_initApp["Caps"] = _initCaps
devices_list.append(_initApp)
print(len(getDevices()))
print(len(devices_list))
return devices_list
run中,多进程调用启动命令行,并传递参数:
def runnerPool(device_list):
getdevice = getDevices()
with ProcessPoolExecutor(len(getdevice)) as pool:
pool.map(runPytest, device_list) def runPytest(device):
print(f"cmdopt is {device}")
report = f"report-{device['Caps']['deviceName']}".split(":", 1)[0]
try:
os.system(f"del /s /q E:\\appium-pytest\\{report}")
time.sleep(1)
os.system(f"rd /s /q E:\\appium-pytest\\{report}")
time.sleep(1)
print(f"{report} report has deleted")
except:
print("no directory existed")
finally:
print(f"pool run device is {device['devices']}")
pytest.main(["../TestCases/", f"--cmdopt={device}", "--alluredir", f"../{report}/xml"])
time.sleep(1)
os.system(f"allure generate ../{report}/xml -o ../{report}/html")
conftest文件中,获取命令行传递过来的参数:
def pytest_addoption(parser):
parser.addoption("--cmdopt", action="store", default="device", help="None") @pytest.fixture(scope="session")
def cmdopt(request):
return request.config.getoption("--cmdopt")
conftest中通过传递的参数,生成连接对象:
@pytest.fixture(scope="session")
def connectDevice(cmdopt):
device = eval(cmdopt)
device_caps = {}
device_caps["platformVersion"] = getPhoneInfo(device["Caps"]["deviceName"])["release"]
device_caps["platformName"] = "Android"
device_caps["automationName"] = "UiAutomator2"
device_caps["deviceName"] = device["Caps"]['deviceName']
device_caps["udid"] = device["Caps"]['deviceName']
device_caps["appPackage"] = "cn.vsx.vc"
device_caps["appActivity"] = ".activity.RegistActivity"
device_caps["noReset"] = True
device_caps["noSign"] = True
device_caps["unicodeKeyboard"] = True
device_caps["resetKeyboard"] = True
device_caps["systemPort"] = int(device["systemPort"])
remote = "http://127.0.0.1:" + str(device["port"]) + "/wd/hub"
print(f"wo shi pytest {device_caps}")
driver = webdriver.Remote(remote, device_caps)
return driver
测试用例中,使用对象来进行操作:
class Test_groupCall():
@allure.feature("group_call")
@allure.story("login")
def test001_login(self, connectDevice):
'''登入选择单位'''
WebDriverWait(connectDevice, 10).until(
lambda x: x.find_element_by_xpath(
"//android.widget.TextView[contains(@text, '选择单位')]").is_displayed()) # 验证等待10秒超时
x = connectDevice.get_window_size()['width'] # 获取当前屏幕宽
y = connectDevice.get_window_size()['height'] # 获取当前屏幕高
a, b = 170 / 768, 790 / 1184 # 选择单位222系数
connectDevice.find_element_by_xpath("//android.widget.TextView[contains(@text, '选择单位')]").click()
最后:
多设备连接时,一定要注意给每个desired_caps中加入每个设备自己的systemPort,否则会连接不上多设备,至此改造成功,最后生成的报告也让人满意:

Appium 并发多进程基于 Pytest框架的更多相关文章
- Appium 并发测试基于unitest
前言: 在回归测试阶段,UI测试,兼容测试是测试的必要步骤.UI自动化的本身是比较冗余的测试,但是换个角度思考,UI自动化同时连接多台设备,那么在回归测试时,在同一个脚本下产生的测试结果是非常有价值的 ...
- 使用allure工具生成测试报告(基于pytest框架)
一.allure简介:一个轻量级的,灵活的,支持多语言,多平台的开源report框架 Allure基于标准的xUnit结果输出,但是添加了一些补充数据.任何报告都是通过两个步骤生成的.在测试执行期间( ...
- 自从学会了Python自动化Pytest框架,领导再也不敢在我背后指手划脚了
前言 大家都知道Python有自带的单元测试框架unittest,那为什么还要学习Pytest呢?先了解下Pytest优点 pytest: pytest是一个非常成熟的全功能的Python测试框架,是 ...
- 『德不孤』Pytest框架 — 1、Pytest测试框架介绍
目录 1.什么是单元测试框架 2.单元测试框架主要做什么 3.单元测试框架和自动化测试框架有什么关系 4.Pytest测试框架说明 5.Pytest框架和Unittest框架区别 (1)Unittes ...
- 基于netty框架的Socket传输
一.Netty框架介绍 什么是netty?先看下百度百科的解释: Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开 ...
- 基于VUE框架 与 其他框架间的基本对比
基于VUE框架的基本描述 与 其他框架间的基本对比 2018-11-03 11:01:14 A B React React 和 Vue 有许多相似之处,它们都有: 使用 Virtual DOM 提供 ...
- pytest框架之fixture详细使用
本人之前写了一套基于unnitest框架的UI自动化框架,但是发现了pytest框架之后觉得unnitest太low,现在重头开始学pytest框架,一边学习一边记录,和大家分享,话不多说,那就先从p ...
- 【前端框架系列】浅谈当前基于bootstrap框架的几种主流前端框架
一 概述 当新开发一个项目或产品时,技术选型是一个不可缺少的环节,在软件架构中有着举足轻重的作用,可以这么说,技术选型的好坏直接影响项目或产品的成败优劣,因此,在进行软件架构时,一定要想好技术选型. ...
- 基于SSH框架的在线考勤系统开发的质量属性
我要开发的是一个基于SSH框架的在线考勤系统. 质量属性是指影响质量的相关因素,下面我将分别从6个系统质量属性(可用性,易用性,可修改性,性能,安全性,可测试性)来分析我的系统,以及如何实现这些质量属 ...
随机推荐
- 转载:Nginx负载均衡的5种策略
nginx可以根据客户端IP进行负载均衡,在upstream里设置ip_hash,就可以针对同一个C类地址段中的客户端选择同一个后端服务器,除非那个后端服务器宕了才会换一个. nginx的upstre ...
- Python day18模块介绍2(使用BASE_DIR修改临时path,os模块)
1.BASE_DIR修改path(别人导入py项目时不会因为绝对路径无法解释) #sys修改环境变量 #使用BASE_DIR将绝对路径改为相对路径 import sys,os BASE_DIR=os. ...
- [库][c++]tinyxml2使用小结
参考:http://blog.csdn.net/educast/article/details/12908455 1.配置TinyXML2 去这里把项目弄下来,然后解压,我们之需要里面的tinyxml ...
- Qt与FFmpeg联合开发指南(一)——解码(1):功能实现
前言:对于从未接触过音视频编解码的同学来说,使用FFmpeg的学习曲线恐怕略显陡峭.本人由于工作需要,正好需要在项目中使用.因此特地将开发过程总结下来.只当提供给有兴趣的同学参考和学习. 由于FFmp ...
- 推荐一款基于Angular实现的企业级中后台前端/设计解决方案脚手架
ng-alain 是一个企业级中后台前端/设计解决方案脚手架,我们秉承 Ant Design 的设计价值观,目标也非常简单,希望在Angular上面开发企业后台更简单.更快速.随着『设计者』的不断反馈 ...
- mysql 问题 Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdb
异常错误:Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.c ...
- SpringBoot+Mybatis-Generator自动生成
原文链接 1.版本 Spring Boot 1.5.10 mybatis-generator-core 1.3.5 mybatis-generator-maven-plugin 1.3.5 2.项目目 ...
- WPF自定义控件的两种方式
方法A: 第一步:My自定义控件:Control 第二步:针对 “My自定义控件” 类型,编写<style>或<模板>(UI的外观完全由用户自己定义) 第三步: 使用My自定 ...
- ContentPresenter
这是2年前写了一篇文章 http://www.cnblogs.com/Clingingboy/archive/2008/07/03/wpfcustomcontrolpart-1.html 我们先来看M ...
- Docker安装及基础知识
一.安装 & 启动 1.安装Docker [root@tokyo ~]# yum install docker 2.启动Docker服务 (1)旧式的 sysv 语法 [root@tokyo ...