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

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

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

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文件

python 复制代码
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文件

python 复制代码
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测试执行入口文件

python 复制代码
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文件

代码如下:

python 复制代码
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文件

代码如下:

python 复制代码
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文件

代码如下:

python 复制代码
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文件

代码如下:

python 复制代码
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'

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

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

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

相关推荐
天天进步2015几秒前
Python项目-基于Flask的个人博客系统设计与实现(1)
开发语言·python·flask
安然无虞2 分钟前
31天Python入门——第20天:魔法方法详解
开发语言·后端·爬虫·python
niuniu_6667 分钟前
安全性测试(Security Testing)
测试工具·单元测试·appium·测试·安全性测试
靠近彗星13 分钟前
基于 Vue + Django + MySQL 实现个人博客/CMS系统
前端·vue.js·python·mysql·django
励志成为大佬的小杨29 分钟前
pytorch模型的进阶训练和性能优化
人工智能·pytorch·python
m0_490240671 小时前
软件自动化测试(1):python+selenium自动化测试环境搭建
开发语言·python·selenium
橘猫云计算机设计2 小时前
基于ssm的食物营养成分数据分析平台设计与实现(源码+lw+部署文档+讲解),源码可白嫖!
后端·python·信息可视化·数据挖掘·数据分析·django·毕业设计
liuhaoran___3 小时前
计算机求职面试中高频出现的经典题目分类整理
python
不辉放弃4 小时前
零基础讲解pandas
开发语言·python
databook4 小时前
线性判别分析(LDA):降维与分类的完美结合
python·机器学习·scikit-learn