Python+Appium+Pytest+Allure practical APP automated testing!

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. Convert the designed automated test cases into scripts. Notes:
In order to ensure the stability of the script and apply the common functions of pytest, the following examples use the android calculator as an example.

Complete code on Gitee: https://gitee.com/YouJeffrey/AUTO_TEST_APP

Prerequisite: Download third-party library

1. Download appium-python-client

2. Download pytest

3. Download allure-pytest

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

2. Create a new APP test project

3. Configuration file information

1. First configure the outer conftest.py file

import pytest

# Configure various connection information of the app
@pytest.fixture(scope='session')
def android_setting():
    des = {
        'automationName': 'appium',
        'platformName': 'Android',
        'platformVersion': '6.0.1', # Fill in the system version number of the android virtual machine/real machine
        'deviceName': 'MuMu', # Fill in the device name of the Android virtual machine/real machine
        'appPackage': 'com.sky.jisuanji', # Fill in the package name of the app under test
        'appActivity': '.JisuanjizixieActivity', # Fill in the entrance of the app under test
        'udid': '127.0.0.1:7555', # Fill in the udid viewed through the command line adb devices
        'noReset': True, # Whether to reset the APP
        'noSign': True, # Whether not to sign
        'unicodeKeyboard': True, # Whether to support Chinese input
        'resetKeyboard': True, # Whether to support resetting the keyboard
        'newCommandTimeout': 30 # Disconnect if no new command is sent for 30 seconds
    }
    return des

2. Configure the conftest.py file of the use case layer

import time
importpytest
from appium import webdriver

driver=None
# Start the calculator app in Android system
@pytest.fixture()
def start_app(android_setting):
    global driver
    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',android_setting)
    return driver

# Close the calculator app in Android system
@pytest.fixture()
def close_app():
    yield driver
    time.sleep(2)
    driver.close_app()

3. Configure the pytest.ini file for group settings

4. Write the run_all_cases.py test execution entry file

import os
importpytest

#Current path (use the abspath method to execute through the dos window)
current_path = os.path.dirname(os.path.abspath(__file__))
# json report path
json_report_path = os.path.join(current_path,'report/json')
# html report path
html_report_path = os.path.join(current_path,'report/html')

# Execute the use case under pytest and generate a json file
pytest.main(['-s','-v','--alluredir=%s'%json_report_path,'--clean-alluredir'])
# Convert json file into html report
os.system('allure generate %s -o %s --clean'%(json_report_path,html_report_path))

5. Writing test cases

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

1. The test_add.py file under the test_add_sub_module module

code show as below:

import allure
from appium.webdriver.webdriver import By

@allure.epic('Android computer project')
@allure.feature('V1.0 version')
class TestAddSub():
    @allure.story('Addition operation')
    @allure.title('[case01] Verify whether the computer can complete the addition function normally')
    # @pytest.mark.add_basic
    def test_cases01(self,start_app,close_app):
        with allure.step('1. Start the computer app in Android system'):
            driver=start_app
        with allure.step('2, press 9, +, 8, =' in sequence):
            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. Verify whether the actual result is correct'):
            # Assert actual result == 17.0
            assert actual_result == '17.0'

2. The test_sub.py file under the test_add_sub_module module

code show as below:

import allure
from appium.webdriver.webdriver import By

@allure.epic('Android computer project')
@allure.feature('V1.0 version')
class TestAddSub():
    @allure.story('Subtraction operation')
    @allure.title('[case01] Verify whether the computer can complete the subtraction function normally')
    def test_cases01(self,start_app,close_app):
        with allure.step('1. Start the computer app in Android system'):
            driver=start_app
        with allure.step('2, press 6, -, 2, =' in sequence):
            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. Verify whether the actual result is correct'):
            # Assert actual result == 4.0
            assert actual_result == '4.0'

3. test_mul.py file under test_mul_div_module module

code show as below:

import allure
from appium.webdriver.webdriver import By

@allure.epic('Android computer project')
@allure.feature('V1.0 version')
class TestAddSub():
    @allure.story('Multiplication')
    @allure.title('[case01] Verify whether the computer can complete the multiplication function normally')
    def test_cases01(self,start_app,close_app):
        with allure.step('1. Start the computer app in Android system'):
            driver=start_app
        with allure.step('2, press 3, *, 4, =' in sequence):
            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. Verify whether the actual result is correct'):
            # Assert actual result == 12.0
            assert actual_result == '12.0'

4. test_div.py file under test_mul_div_module module

code show as below:

import allure
from appium.webdriver.webdriver import By

@allure.epic('Android computer project')
@allure.feature('V1.0 version')
class TestAddSub():
    @allure.story('Division operation')
    @allure.title('[case01] Verify whether the computer can complete the division function normally')
    def test_cases01(self,start_app,close_app):
        with allure.step('1. Start the computer app in Android system'):
            driver=start_app
        with allure.step('2, press 8, *, 4, =' in sequence):
            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. Verify whether the actual result is correct'):
            # Assert actual result == 2.0
            assert actual_result == '2.0'

6. Generate test report from running results

Finally, I would like to thank everyone who read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can just take it away:

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeHomepageOverview 383721 people are learning the system