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

pytest只是单独的一个单元测试框架,要完成app测试自动化需要把pytest和appium进行整合,同时利用allure完成测试报告的产出。

如果你想学习app自动化测试, 我这边给你推荐一套视频

app自动化测试高频面试题:Appium工作原理_哔哩哔哩_bilibili【Python接口自动化测试笔记及视频源码 +微信:mashang-nn 备注(b站555)即可通过 收藏等于白嫖,点赞三连才是真情!】, 视频播放量 74、弹幕量 0、点赞数 1、投硬币枚数 0、收藏人数 2、转发人数 0, 视频作者 自动化测试码尚科技, 作者简介 领取笔记加微信:mashang-nn 备注:B站555,相关视频:在华为工作了10年的大佬出的Web自动化测试教程,华为现用技术教程!,为B站打造api接口自动化测试框架,2023年B站最新Jmeter接口测试实战教程,精通接口自动化测试只需要这一套视频,2023最新pytest接口自动化测试框架,三天带你精通pytest,带你写出最好的代码!(已更新2023新版),Selenium自动化测试环境搭建自动化测试框架实站全套教程,为B站测试猿打造的api接口自动化测试框架,Postman接口测试使用教程+下载+安装及项目实战教程,App自动化测试Appium+UiAutomator2技巧,2023B站最全RobotFramework自动化测试框架零基础入门到实战!(更新版),接口自动化测试yaml+requests+allure技术https://www.bilibili.com/video/BV1NT411x7fy/?spm_id_from=333.999.0.0

编写常规的线性脚本具体的步骤如下:
1、设计待测试APP的自动化测试用例
2、新建app测试项目
3、配置conftest.py文件等
4、编写整体app测试用例运行文件
5、把设计好的自动化测试用例转化成脚本备注:
为了保证脚本的稳定性,又把pytest常用功能应用,以下示例采用android计算器为示例讲解。

Gitee上完整代码:
https://gitee.com/YouJeffrey/AUTO_TEST_APP

前置条件:下载第三方库

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 time
import pytest
from appium import webdriver

driver = None
# 启动安卓系统中的计算器app
@pytest.fixture()
def start_app(android_setting):
    global driver
    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',android_setting)
    return driver

# 关闭安卓系统中的计算器app
@pytest.fixture()
def close_app():
    yield driver
    time.sleep(2)
    driver.close_app()

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

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

import os
import 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_div_module;

1、test_add_sub_module模块下test_add.py文件

代码如下: 

import allure
from appium.webdriver.webdriver import By

@allure.epic('安卓计算机项目')
@allure.feature('V1.0版本')
class TestAddSub():
    @allure.story('加法运算')
    @allure.title('[case01] 验证计算机能否正常完成加法功能')
    # @pytest.mark.add_basic
    def test_cases01(self,start_app,close_app):
        with allure.step('1、启动安卓系统中的计算机app'):
            driver = start_app
        with 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"]').text
        with allure.step('3、验证实际结果是否正确'):
            # 断言 实际结果 == 17.0
            assert actual_result == '17.0'

2、test_add_sub_module模块下test_sub.py文件

代码如下:

import allure
from 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_app
        with 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"]').text
        with allure.step('3、验证实际结果是否正确'):
            # 断言 实际结果 == 4.0
            assert actual_result == '4.0'

 

3、test_mul_div_module模块下test_mul.py文件

代码如下:

import allure
from 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_app
        with 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"]').text
        with allure.step('3、验证实际结果是否正确'):
            # 断言 实际结果 == 12.0
            assert actual_result == '12.0'

4、test_mul_div_module模块下test_div.py文件

代码如下:

import allure
from 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_app
        with 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"]').text
        with allure.step('3、验证实际结果是否正确'):
            # 断言 实际结果 == 2.0
            assert actual_result == '2.0'

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

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值