参考链接
allure 官网地址:http://allure.qatools.ru/
开营仪式
adb常用命令
appium inspector
编写第一个测试用例
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
# pip install appium-python-client
from time import sleep
import pytest
import yaml
from appium import webdriver
# pytest 单元测试框架
from appium.webdriver.common.mobileby import MobileBy
"""
改造1: 改造成pytest单元测试框架的模式
改造2: 加入setup,teardown
改造3: 将定位器改写成可维护型
改造4: 适当的加入断言来判断逻辑的正确性
改造5: 添加参数化与数据驱动,动态生成测试用例
"""
with open("./datas/searchkey.yml") as f:
datas = yaml.safe_load(f)
class TestCart:
def setup(self):
caps = {}
caps["platformName"] = "Android"
caps["deviceName"] = "hogwarts"
caps["appPackage"] = "com.dangdang.buy2"
caps["appActivity"] = ".StartupActivity"
caps["noReset"] = "True"
caps['udid'] = 'emulator-5554'
self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
# 隐式等待, 每次查找元素的时候,都会在5秒之内查找这个元素,每隔0.5秒 找一次,
# 直到5秒结束都没找到的话,才抛出未找到元素的异常
self.driver.implicitly_wait(5)
def teardown(self):
self.driver.quit()
@pytest.mark.parametrize('searchkey', datas)
def test_case1(self, searchkey):
# searchkey = "android"
el1 = self.driver.find_element(MobileBy.ID, "com.dangdang.buy2:id/home_view_flipper")
el1.click()
el2 = self.driver.find_element(MobileBy.ID, "com.dangdang.buy2:id/et_search")
el2.send_keys(searchkey)
el3 = self.driver.find_element(MobileBy.XPATH,
f"//*[@text='{searchkey}' and contains(@resource-id,'tv_sug')]")
el3.click()
el4 = self.driver.find_elements(MobileBy.ID,
"com.dangdang.buy2:id/product_img_iv")[0]
el4.click()
el5 = self.driver.find_element(MobileBy.ID, "com.dangdang.buy2:id/tv_magic_btn_right")
el5.click()
# 强制等待,死等
sleep(2)
print(self.driver.page_source)
ele = self.driver.find_element(MobileBy.XPATH, "//*[@class='android.widget.Toast']")
assert '商品已成功加入购物车' == ele.text
allure 的安装
1、安装 allure , 不同的系统安装方式不同,参照官方文档
2、安装 allure-pytest
pip install allure-pytest
pytest 常用插件
- pytest-ordering 用例顺序控制
- pytest-dependency 用例依赖
https://pytest-dependency.readthedocs.io/en/stable/
allure 运行
-
生成中间结果文件,json,txt文件
pytest test_a.py --alluredir result/ -
打开结果文件
allure serve ./result -
生成最终的报告
allure generate ./result