Hi, Hello everyone . Let's talk today Python+Appium+Pytest+Allure actual combat APP automated testing ,pytest Just a separate unit testing framework , To complete the app Test automation requires pytest and appium Integration , Simultaneous utilization allure Complete the output of test report .
Regular writing linear The specific steps of the script are as follows :
1、 Design to be tested APP Of automated testing Use cases
2、 newly build app Test project
3、 To configure conftest.py Documents, etc.
4、 Write the whole app The test case Run the file
5、 Turn the designed automated test cases into script notes :
To ensure the stability of the script , Also put pytest Common function application , The following example uses android The calculator is an example .
1、 download appium-python-client
2、 download pytest
3、 download allure-pytest
Two 、 newly build APP Test project
1、 Configure the outer layer first conftest.py file
import pytest # To configure app Various connection information @pytest.fixture(scope='session') def android_setting(): des = { 'automationName': 'appium', 'platformName': 'Android', 'platformVersion': '6.0.1', # Fill in android virtual machine / The system version number of the real machine 'deviceName': 'MuMu', # Fill in the Android virtual machine / The name of the real machine 'appPackage': 'com.sky.jisuanji', # Fill in the tested app Package name 'appActivity': '.JisuanjizixieActivity', # Fill in the tested app Entrance 'udid': '127.0.0.1:7555', # Fill in through the command line adb devices Check to udid 'noReset': True, # Reset... Or not APP 'noSign': True, # Whether not to sign 'unicodeKeyboard': True, # Whether Chinese input is supported 'resetKeyboard': True, # Whether keyboard reset is supported 'newCommandTimeout': 30 # 30 Seconds to disconnect without sending a new command } return des
2、 Reconfigure the use case layer conftest.py file
import time import pytest from appium import webdriver driver = None # Start the calculator in Android 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 # Turn off the calculator in Android app @pytest.fixture() def close_app(): yield driver time.sleep(2) driver.close_app()
3、 To configure pytest.ini Group files
import os import pytest # current path ( Use abspath The method can be found in dos Window execution ) 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') # perform pytest And generate json file pytest.main(['-s','-v','--alluredir=%s'%json_report_path,'--clean-alluredir']) # hold json Document conversion html The report os.system('allure generate %s -o %s --clean'%(json_report_path,html_report_path))
stay testcases There are two business sub modules under the layer test_add_sub_module and test_mul_div_module;
1、test_add_sub_module Under module test_add.py file
The code is as follows :
import allure from appium.webdriver.webdriver import By @allure.epic(' Android computer project ') @allure.feature('V1.0 edition ') 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 in the Android system app'): driver = start_app with allure.step('2、 Press in turn 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 results are correct '): # Assertion The actual result == 17.0 assert actual_result == '17.0'
2、test_add_sub_module Under module test_sub.py file
The code is as follows :
import allure from appium.webdriver.webdriver import By @allure.epic(' Android computer project ') @allure.feature('V1.0 edition ') 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 in the Android system app'): driver = start_app with allure.step('2、 Press in turn 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 results are correct '): # Assertion The actual result == 4.0 assert actual_result == '4.0'
3、test_mul_div_module Under module test_mul.py file
The code is as follows :
import allure from appium.webdriver.webdriver import By @allure.epic(' Android computer project ') @allure.feature('V1.0 edition ') 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 in the Android system app'): driver = start_app with allure.step('2、 Press in turn 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 results are correct '): # Assertion The actual result == 12.0 assert actual_result == '12.0'
4、test_mul_div_module Under module test_div.py file
The code is as follows :
import allure from appium.webdriver.webdriver import By @allure.epic(' Android computer project ') @allure.feature('V1.0 edition ') class TestAddSub(): @allure.story(' Division operations ') @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 in the Android system app'): driver = start_app with allure.step('2、 Press in turn 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 results are correct '): # Assertion The actual result == 2.0 assert actual_result == '2.0'
It is hoped that more specifications of the project need to be introduced PO Design patterns , secondly base_page.py You can also encapsulate more methods , Only a few methods are encapsulated in the above code , A basic frame is built , Interested partners can try . If you don't understand something, you can also add a technology group : Please click here 「 Check the bulletin board 」 How to join the community ,(← You can click directly to the announcement Office ), Discuss learning together !
Chinese Valentines Day!Its tim
One 、 Introduction to the topi