参数化和数据驱动
摘要:
参数化的使用
pytest.mark.parametrize()直接传参
数据驱动
外部文件:使用yaml
测试用例参数从外部文件获取
测试步骤从外部文件获取
参数化使用
@pytest.mark.parametrize(“search_words”, [‘游戏’, “hhdddx”])
具体用例:
from page.app import App
class TestDemo:
def setup(self):
self.main_page = App.start()
@pytest.mark.dependency()
def test_game_handle(self):
'''点击首页的游戏手柄,进入游戏中心'''
self.game_center = self.main_page.click_game_handle()
# @pytest.mark.dependency(depends=["TestDemo::test_game_handle"])
@pytest.mark.parametrize("keyword", ['游戏', "hhdddx"])
def test_game_search(self, keyword):
self.test_game_handle()
'''点击游戏中心页右上角的搜索,进行搜索查询'''
self.game_center.search(keyword)
sleep(5)
self.game_center.get_first_content(keyword)
def test_password_login(self):
'''从游戏中心-我的进入登录页面,进行登录'''
self.test_game_handle()
self.my_page = self.game_center.click_bottom_button_mine()
self.mobile_login = self.my_page.click_no_login_content()
self.password_login = self.mobile_login.switch_to_password_login()
self.password_login.password_login()
def teardown(self):
sleep(20)
App.quit()
这里会重启多次APP,要想不重启APP,则不使用setup和teardown,而是使用setupclass
数据驱动
参数化数据读取外部文件:使用yaml,JSON读取
测试步骤读取外部文件:定制执行引擎
断言步骤读取外部文件:定制执行引擎
整个用例读取外部文件:动态创建用例
参数化数据读取外部文件:
举例:使用yaml文件
1 安装yaml
使用pycharm工具安装
windows电脑-打开pycharm-setting-project-Project Interpreter-点击加号-在搜索框内查找pyyaml进行安装
使用命令安装,从pypi源获取安装包
pip3 install pyyaml
使用命令安装,使用国内源获取安装包
pip3 install pyyaml -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip使用国内源下载安装包:pip使用国内源下载安装包
yaml的使用教程:点击查看
2 导入yaml
import yaml
search_data = yaml.salf_load(open(“search.yaml”, “r”))
print(search_data)
from time import sleep
import pytest
import yaml
from page.app import App
class TestDemo:
search_data = yaml.safe_load(open("search.yaml", "r"))
def setup(self):
self.main_page = App.start()
@pytest.mark.dependency()
def test_game_handle(self):
'''点击首页的游戏手柄,进入游戏中心'''
self.game_center = self.main_page.click_game_handle()
# @pytest.mark.dependency(depends=["TestDemo::test_game_handle"])
@pytest.mark.parametrize("keyword", search_data)
def test_game_search(self, keyword):
self.test_game_handle()
'''点击游戏中心页右上角的搜索,进行搜索查询'''
self.game_center.search(keyword)
sleep(5)
self.game_center.get_first_content(keyword)
search.yaml文件
- ['游戏']
- [game]
参考文章:
pip使用国内源下载安装包==:pip使用国内源下载安装包
yaml的使用教程:点击查看
测试步骤读取外部文件
参考文章:
视频:视频链接点击跳转
文章:
Junit5 + YAML 轻松实现参数化和数据驱动,让 App 自动化测试更高效(一) 点击跳转
*Junit5 + YAML 轻松实现参数化和数据驱动,让 App 自动化测试更高效(二)
点击跳转