pytest只是單獨的一個單元測試框架,要完成app測試自動化需要把pytest
和appium進行整合,同時利用alltrue完成測試報告的產出。
編寫常規的線性腳本具體的步驟如下:
1、設計待測試APP的自動化測試用例
2、新建app測試項目
3、配置conftest.py文件等
4、編寫整體app測試用例運行文件
5、把設計好的自動化測試用例轉化成腳本
實戰一:設計待測試APP的自動化測試用例
實戰二:新建app測試項目
實戰三:
1. 配置外層conftest.py文件
import pytest @pytest.fixture(scope='session') def des_env(): # 裝飾函數的名稱 des_info = { 'platformName': 'Android', 'platformVersion': '9.0', # 填寫android虛擬機的系統版本 'deviceName': 'Samsung Galaxy Note2', # 填寫安卓虛擬機的設備名稱 'appPackage': 'com.android.calculator2', # 填寫被測試包名 'appActivity': 'com.android.calculator2.Calculator', # 填寫被測試app入口 'udid': '192.168.58.103:5555', # 填寫通過命令行 adb devices 查看到的 uuid 'noReset': True, 'unicodeKeyboard': True, 'resetKeyboard': True, } return des_info
2.配置內層conftest.py文件
from time import sleep import pytest from appium import webdriver driver = None @pytest.fixture() def start_app(des_env): # 裝飾函數的名稱 global driver driver = webdriver.Remote('http://localhost:4723/wd/hub', des_env) return driver @pytest.fixture(autouse=True) def close_driver(): sleep(3) yield driver driver.close_app()
3.配置pytest.ini文件用於分組
[pytest]
markers =
basic:marks tests as basic
smoketest:marks tests as smoketest
systemtest:marks tests as systemtest
實戰四:編寫整體運行測試用例的文件run_all_cases.py
import os import time import pytest now_time = time.strftime("%Y_%m_%d_%H_%M_%S") xml_result_path = os.path.join(os.path.dirname(__file__),'reports/xml',now_time) os.mkdir(xml_result_path) html_result_path = os.path.join(os.path.dirname(__file__),'reports/html',now_time) os.mkdir(html_result_path) #xml報告生成 pytest.main(['--alluredir={}'.format(xml_result_path),'-m basic','-s']) #html報告生成 os.system("E:/allure/allure-2.13.5/bin/allure generate {} -o {}".format(xml_result_path,html_result_path))
實戰五:編寫具體的測試用例
allure用例描述詳解: