APP automated testing-Python+Appium+Pytest+Allure framework practical packaging (details)


Preface

Pytest is just a separate unit testing framework. To complete app test automation, you need to integrate pytest and appium, and use allure to complete the output of test reports.

The specific steps for writing a conventional linear script are as follows:
1. Design automated test cases for the APP to be tested
2. Create a new app test project
3. Configure the conftest.py file, etc.
4. Write the overall app test case running file
5. Automate the designed Convert test cases into script notes

The following example uses a calculator as an example

Prerequisites: Download third-party library
Download appium-python-client
Download pytest
Download allure-pytest

1. Design automated test cases for the APP to be tested

E1

2. Create a new APP test project

E2

3. Configuration file information

First configure the outer conftest.py file

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

Then configure the conftest.py file of the use case layer

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()

Configure the pytest.ini file for group settings

E3

4. Write the run_all_cases.py test execution entry file

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))

5. Write test cases

There are two business sub-modules test_add_sub_module and test_mul_div_module under the testcases layer;

The code of the test_add.py file under the test_add_sub_module module
is as follows:

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'

The code of the test_sub.py file under the test_add_sub_module module
is as follows:

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'

The code of the test_mul.py file under the test_mul_div_module module
is as follows:

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'

The code of the test_div.py file under the test_mul_div_module module
is as follows:

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'

6. Generate test report from running results

E4

The following is the most comprehensive software testing engineer learning knowledge architecture system diagram in 2023 that I compiled.

1. Python programming from entry to proficiency

Please add image description

2. Practical implementation of interface automation projects

Please add image description

3. Web automation project actual combat

Please add image description

4. Practical implementation of App automation project

Please add image description

5. Resumes of first-tier manufacturers

Please add image description

6. Test and develop DevOps system

Please add image description

7. Commonly used automated testing tools

Please add image description

8. JMeter performance test

Please add image description

9. Summary (little surprise at the end)

Dreams are sails and struggles are ships. Only by continuous pursuit can we reach the other side of success. Ride the waves, move forward bravely, and try your best to finally raise the sail of victory. Believe in yourself and venture bravely, and you will be able to sail towards glory and write your own magnificent chapter.

Life is like climbing a mountain. The steeper the mountain, the more courage we show. Overcome difficulties, surpass yourself, and work hard to achieve brilliance. Don’t forget your original intention, move forward with determination, and strengthen your faith, you will be able to conquer everything and create a brilliant life of your own.

The stage of destiny belongs to the brave, and every effort is an opportunity for change. Free your mind and forge ahead. Only struggle can create infinite possibilities. Believe in your own strength and persevere, and you will embark on a glorious journey and write your own magnificent legend.

Guess you like

Origin blog.csdn.net/m0_60054525/article/details/132007635