Python+Appium+Pytest+Allure实战APP自动化测试框架

发布时间:2025-12-10 11:17:14 浏览次数:10

Hi,大家好。今天我们来聊聊Python+Appium+Pytest+Allure实战APP自动化测试,pytest只是单独的一个单元测试框架,要完成app测试自动化需要把pytest和appium进行整合,同时利用allure完成测试报告的产出。

编写常规的线性脚本具体的步骤如下:

1、设计待测试APP的自动化测试用例

2、新建app测试项目

3、配置conftest.py文件等

4、编写整体app测试用例运行文件

5、把设计好的自动化测试用例转化成脚本备注

为了保证脚本的稳定性,又把pytest常用功能应用,以下示例采用android计算器为示例讲解。

前置条件:下载第三方库

1、下载 appium-python-client

2、下载pytest

3、下载 allure-pytest

一、设计待测试APP的自动化测试用例

二、新建APP测试项目

三、配置文件信息

1、先配置外层conftest.py文件

import pytest# 配置app的各种连接信息@pytest.fixture(scope='session')def android_setting():des = {'automationName': 'appium','platformName': 'Android','platformVersion': '6.0.1', # 填写android虚拟机/真机的系统版本号'deviceName': 'MuMu', # 填写安卓虚拟机/真机的设备名称'appPackage': 'com.sky.jisuanji', # 填写被测app包名'appActivity': '.JisuanjizixieActivity', # 填写被测app的入口'udid': '127.0.0.1:7555', # 填写通过命令行 adb devices 查看到的udid'noReset': True, # 是否重置APP'noSign': True, # 是否不签名'unicodeKeyboard': True, # 是否支持中文输入'resetKeyboard': True, # 是否支持重置键盘'newCommandTimeout': 30 # 30秒没发送新命令就断开连接}return des

 2、再配置用例层的conftest.py文件

import timeimport pytestfrom appium import webdriverdriver = None# 启动安卓系统中的计算器app@pytest.fixture()def start_app(android_setting):global driverdriver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',android_setting)return driver# 关闭安卓系统中的计算器app@pytest.fixture()def close_app():yield drivertime.sleep(2)driver.close_app()

 

3、配置pytest.ini文件进行分组设置

四、编写run_all_cases.py测试执行入口文件

import osimport pytest# 当前路径(使用 abspath 方法可通过dos窗口执行)current_path = os.path.dirname(os.path.abspath(__file__))# json报告路径json_report_path = os.path.join(current_path,'report/json')# html报告路径html_report_path = os.path.join(current_path,'report/html')# 执行pytest下的用例并生成json文件pytest.main(['-s','-v','--alluredir=%s'%json_report_path,'--clean-alluredir'])# 把json文件转成html报告os.system('allure generate %s -o %s --clean'%(json_report_path,html_report_path))

 

五、编写测试用例

在testcases层下有两个业务子模块 test_add_sub_module 和 test_mul_p_module;

1、test_add_sub_module模块下test_add.py文件

代码如下:

import allurefrom appium.webdriver.webdriver import By@allure.epic('安卓计算机项目')@allure.feature('V1.0版本')class TestAddSub():@allure.story('加法运算')@allure.title('[case01] 验证计算机能否正常完成加法功能')# @pytest.mark.add_basicdef test_cases01(self,start_app,close_app):with allure.step('1、启动安卓系统中的计算机app'):driver = start_appwith allure.step('2、依次按下9、+、8、='):driver.find_element(By.XPATH,'//android.widget.Button[@resource-id="com.sky.jisuanji:id/btn9"]').click()driver.find_element(By.XPATH, '//android.widget.Button[@resource-id="com.sky.jisuanji:id/jia"]').click()driver.find_element(By.XPATH, '//android.widget.Button[@resource-id="com.sky.jisuanji:id/btn8"]').click()driver.find_element(By.XPATH, '//android.widget.Button[@resource-id="com.sky.jisuanji:id/denyu"]').click()actual_result = driver.find_element(By.XPATH, '//android.widget.EditText[@resource-id="com.sky.jisuanji:id/text"]').textwith allure.step('3、验证实际结果是否正确'):# 断言 实际结果 == 17.0assert actual_result == '17.0'

2、test_add_sub_module模块下test_sub.py文件

代码如下:

import allurefrom appium.webdriver.webdriver import By@allure.epic('安卓计算机项目')@allure.feature('V1.0版本')class TestAddSub():@allure.story('减法运算')@allure.title('[case01] 验证计算机能否正常完成减法功能')def test_cases01(self,start_app,close_app):with allure.step('1、启动安卓系统中的计算机app'):driver = start_appwith allure.step('2、依次按下6、-、2、='):driver.find_element(By.XPATH,'//android.widget.Button[@resource-id="com.sky.jisuanji:id/btn6"]').click()driver.find_element(By.XPATH, '//android.widget.Button[@resource-id="com.sky.jisuanji:id/jian"]').click()driver.find_element(By.XPATH, '//android.widget.Button[@resource-id="com.sky.jisuanji:id/btn2"]').click()driver.find_element(By.XPATH, '//android.widget.Button[@resource-id="com.sky.jisuanji:id/denyu"]').click()actual_result = driver.find_element(By.XPATH, '//android.widget.EditText[@resource-id="com.sky.jisuanji:id/text"]').textwith allure.step('3、验证实际结果是否正确'):# 断言 实际结果 == 4.0assert actual_result == '4.0'

3、test_mul_p_module模块下test_mul.py文件

代码如下:

import allurefrom appium.webdriver.webdriver import By@allure.epic('安卓计算机项目')@allure.feature('V1.0版本')class TestAddSub():@allure.story('乘法运算')@allure.title('[case01] 验证计算机能否正常完成乘法功能')def test_cases01(self,start_app,close_app):with allure.step('1、启动安卓系统中的计算机app'):driver = start_appwith allure.step('2、依次按下3、*、4、='):driver.find_element(By.XPATH,'//android.widget.Button[@resource-id="com.sky.jisuanji:id/btn3"]').click()driver.find_element(By.XPATH, '//android.widget.Button[@resource-id="com.sky.jisuanji:id/chen"]').click()driver.find_element(By.XPATH, '//android.widget.Button[@resource-id="com.sky.jisuanji:id/btn4"]').click()driver.find_element(By.XPATH, '//android.widget.Button[@resource-id="com.sky.jisuanji:id/denyu"]').click()actual_result = driver.find_element(By.XPATH, '//android.widget.EditText[@resource-id="com.sky.jisuanji:id/text"]').textwith allure.step('3、验证实际结果是否正确'):# 断言 实际结果 == 12.0assert actual_result == '12.0'

 

4、test_mul_p_module模块下test_p.py文件

代码如下:

import allurefrom appium.webdriver.webdriver import By@allure.epic('安卓计算机项目')@allure.feature('V1.0版本')class TestAddSub():@allure.story('除法运算')@allure.title('[case01] 验证计算机能否正常完成除法功能')def test_cases01(self,start_app,close_app):with allure.step('1、启动安卓系统中的计算机app'):driver = start_appwith allure.step('2、依次按下8、*、4、='):driver.find_element(By.XPATH,'//android.widget.Button[@resource-id="com.sky.jisuanji:id/btn8"]').click()driver.find_element(By.XPATH, '//android.widget.Button[@resource-id="com.sky.jisuanji:id/chu"]').click()driver.find_element(By.XPATH, '//android.widget.Button[@resource-id="com.sky.jisuanji:id/btn4"]').click()driver.find_element(By.XPATH, '//android.widget.Button[@resource-id="com.sky.jisuanji:id/denyu"]').click()actual_result = driver.find_element(By.XPATH, '//android.widget.EditText[@resource-id="com.sky.jisuanji:id/text"]').textwith allure.step('3、验证实际结果是否正确'):# 断言 实际结果 == 2.0assert actual_result == '2.0'

六、运行结果生成测试报告

项目小结

上述代码中也只封装了几个方法,一个基本的架子算是搭建完成,感兴趣的小伙伴可以试试。

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取 

 

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477