Python+Appium+Pytest+Allure Practical App Automation Testing

pytest is just a single unit testing framework. To complete app test automation, it is necessary to integrate pytest and appium, and use allure to complete the output of test reports.

The specific steps for writing a regular linear script are as follows:
1. Design the automated test case 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 run file
5. Automate the design The test case is converted into a script Note:
In order to ensure the stability of the script, the common functions of pytest are applied. The following example uses the android calculator as an example to explain.


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

Precondition: 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 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 Tested app package name 
        'appActivity': '.JisuanjizixieActivity', # Fill in the entry of the tested app 
        'udid': '127.0.0.1:7555', # Fill in the udid viewed through the command line adb devices 
        'noReset': True, # Whether to reset APP 
        'noSign': True,# Whether to not sign 
        'unicodeKeyboard': True, # Whether to support Chinese input 
        'resetKeyboard': True, # Whether to support reset keyboard
        'newCommandTimeout': 30 # 30 seconds without sending a new command to disconnect  
    }
    return des

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

import time 
import pytest 
from appium import webdriver 

driver = None 
# Start the calculator app in Android 
@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 the Android system 
@pytest.fixture() 
def close_app(): 
    yield driver 
    time.sleep(2) 
    driver.close_app()

3. Configure the pytest.ini file for grouping settings

4. Write run_all_cases.py test execution entry file

import os 
import pytest 

# The current path (using the abspath method can be executed 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. Write 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 that 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 the Android system' ): 
            driver = start_app 
        with allure.step('2, press 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. Verify that the actual result is correct'): 
            # Assert that the 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') 
    @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 the Android system'): 
            driver = start_app 
        with allure .step('2, press 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. Verify that the actual result is correct'): 
            # Assert that the actual result == 4.0 
            assert actual_result == '4.0'

 3. The test_mul.py file under the 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 the Android system'): 
            driver = start_app 
        with allure .step('2, press 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. Verify that the actual result is correct'): 
            # Assert the actual result == 12.0 
            assert actual_result == '12.0'

 4. The test_div.py file under the 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 the Android system'): 
            driver = start_app 
        with allure .step('2, press 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. Verify that the actual result is correct'): 
            # Assert that the actual result == 2.0 
            assert actual_result == '2.0'

Six, run the results to generate test reports

Practical case

Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.

If it is helpful to you, please like and collect it to give the author an encouragement. It is also convenient for you to quickly find it next time.

If you don’t understand, please consult the small card below. The blogger also hopes to learn and progress with like-minded testers

At the right age, choose the right position, and try to give full play to your own advantages.

My road of automated test development is inseparable from the plan of each stage along the way, because I like planning and summarizing,

Test and develop video tutorials, study notes and receive portals! ! !

Guess you like

Origin blog.csdn.net/Liuyanan990830/article/details/132307987