测试代码
App 自动化测试用例¶
测试用例编写¶
使用 pytest 测试类组织测试用例。在测试类中编写具体的测试方法,每一个测试方法是一个测试用例。
App 自动化测试需要在用例执行之前打开对应软件进入首页,在用例执行结束后关闭软件释放资源。所以需要用到 pytest 提供的测试装置 setup 和 teardown 来实现。
@allure.epic("雪球app")
@allure.feature("搜索功能")
class TestSearch:
driver = None
def setup(self):
"""
准备测试环境,打开雪球app
:return:
"""
desired_caps = {'platformName': 'Android', 'platformVersion': '6.0', 'deviceName': '127.0.0.1',
'appPackage': 'com.xueqiu.android', 'appActivity': '.view.WelcomeActivityAlias',
'noReset': 'true'}
logger.info(f"启动包名:{desired_caps['appPackage']},启动页面:{desired_caps['appActivity']}")
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
self.driver.implicitly_wait(10)
def teardown(self):
logger.info("保存当面页面截图tmp.png")
self.driver.save_screenshot("tmp.png")
logger.info("将截图添加到报告中")
allure.attach.file("tmp.png", name="页面截图",
attachment_type=allure.attachment_type.PNG)
self.driver.quit()
def test_jump(self):
"""
1. 进入雪球app首页
2. 点击上方搜索框
预期结果:正确跳转到搜索页面
:return:
"""
with allure.step("点击搜索框"):
logger.info("点击搜索框")
self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/tv_search").click()
with allure.step("验证页面是否跳转到搜索页面"):
logger.info("验证页面是否跳转到搜索页面")
result = self.driver.find_element(AppiumBy.XPATH, "//*[@text='今日热点']").text
with allure.step("断言"):
logger.info(f"断言: 跳转到搜索页面 ")
assert "今日热点" == result
添加日志¶
在测试用例中添加日志
logger.info(f"启动包名:{desired_caps['appPackage']},启动页面:{desired_caps['appActivity']}")
参数化¶
如果多个测试用例,测试步骤完全相同,只有测试数据不同,可以使用参数化的方式,在一个测试方法中完成多个测试用例的执行。
@pytest.mark.parametrize('search_key', [
'xnmc', '*@&'
])
def test_fail(self, search_key):
...
设置测试用例优先级¶
使用 pytest 添加标签的特性,为测试用例设置优先级。这样可以方便单独执行其中一种优先级的测试用例。
在 pytest.ini 文件中声明用例优先级
[pytest]
markers = P0
P1
P2
在测试用例中添加优先级
@pytest.mark.P0
def test_search(self):
...
添加测试报告¶
使用 allure,在测试用例的不同位置添加测试报告信息。
@allure.epic("雪球app")
@allure.feature("搜索功能")
class TestOwnerSearch:
...
代码¶
部分用例代码如下:
@allure.epic("雪球app")
@allure.feature("搜索功能")
class TestSearch:
driver = None
def setup(self):
"""
准备测试环境,打开雪球app,复用driver,节省构建时间
:return:
"""
if self.driver == None:
desired_caps = {'platformName': 'Android', 'platformVersion': '6.0', 'deviceName': '127.0.0.1',
'appPackage': 'com.xueqiu.android', 'appActivity': '.view.WelcomeActivityAlias',
'noReset': 'true'}
logger.info(f"启动包名:{desired_caps['appPackage']},启动页面:{desired_caps['appActivity']}")
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
self.driver.implicitly_wait(10)
else:
logger.info("复用driver")
self.driver.start_activity(app_package='com.xueqiu.android', app_activity='.view.WelcomeActivityAlias')
def teardown(self):
logger.info("保存当面页面截图tmp.png")
self.driver.save_screenshot("tmp.png")
logger.info("将截图添加到报告中")
allure.attach.file("tmp.png", name="页面截图",
attachment_type=allure.attachment_type.PNG)
self.driver.quit()
@pytest.mark.P0
@allure.story("【冒烟】")
@allure.title("输入系统中唯一的股票名称,搜索成功")
def test_only_result(self):
"""
1. 进入搜索页面
2. 输入框输入 【阿里巴巴】
3. 点击 搜索 内容
预期结果:搜索成功,展示搜索结果
:return:
"""
search = '阿里巴巴'
with allure.step("点击搜索框"):
logger.info("点击搜索框")
self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/tv_search").click()
with allure.step("输入搜索内容"):
logger.info("输入搜索内容")
self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/search_input_text").send_keys(search)
with allure.step("点击搜索结果"):
logger.info("点击搜索结果")
self.driver.find_elements(AppiumBy.XPATH, f"//*[@text='{search}']")[1].click()
with allure.step("验证结果页"):
logger.info("验证结果页")
result = self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/stockName").text
with allure.step("断言"):
logger.info(f"断言:\"{search}\" == {result}")
assert search == result
@pytest.mark.P0
@allure.story("【冒烟】")
@allure.title("输入系统中不唯一的股票名称,搜索成功")
def test_not_unique(self):
search = 'ETF'
"""
1. 进入搜索页面
2. 输入框输入 【ETF】
3. 点击 搜索 内容
预期结果:搜索成功,展示搜索结果
:return:
"""
with allure.step("点击搜索框"):
logger.info("点击搜索框")
self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/tv_search").click()
with allure.step("输入搜索内容"):
logger.info("输入搜索内容")
self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/search_input_text").send_keys(search)
with allure.step("点击搜索结果"):
logger.info("点击搜索结果")
self.driver.find_elements(AppiumBy.XPATH, f"//*[@text='{search}']")[1].click()
with allure.step("验证结果页"):
logger.info("验证结果页")
result = self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/stockName").text
with allure.step("断言"):
logger.info(f"断言:\"{search}\" in {result}")
assert search in result
完整的自动化测试用例代码请查看源码:app自动化测试代码