10_Pytest框架
一、pytest环境安装
1.cmd中安装
1.在cmd中当前目录下输入指令:pip install pytest
2.安装完成之后输入pytest --version
检查是否安装成功,显示pytest版本号就说明安装成功
2.pycharm中安装
-
选择File—>Settings
File -
选择“Project Interpreter”,发现没有pytest库,选择右侧“+”图标
Settings -
在弹出的“Available Package”中查找pytest
Available Package -
点击下方“Install Package”,可见库正在下载中(installing)
-
安装结束后,打开“Project Interpreter”,可以看到pytest库已经在列表中
Project Interpreter_2
二、pytest框架使用(windows系统,下面是本人实际遇到情况,不一定所有人都一样,仅做参考)
1.pycharm的运行方式(用例文件名一定是test开头)
-
Run xx.py方式执行:当执行的用例代码里面没用到unittest和pytest框架内容时,此时pycharm会以Run xx.py方式运行
以Run xx.py方式执行
-
Run Unittest in xx.py方式执行:情况如下
-
当执行的用例文件是第一次运行,pycharm默认运行器为unittest时,此时pycharm会以Run Unittest in xx.py方式运行
Settings
以Run Unittest in xx.py方式执行 - 当执行的用例文件之前以unittest方式运行过,不管默认运行器为unittest还是pytest,此时pycharm会以Run Unittest in xx.py方式运行
Edit Configurations
Edit Configurations_2
注意:如图“Edit Configurations”,点击“Edit Configurations”查看当前文件以哪种方式运行,点击“-”可以删除当前文件运行方式,如图“Edit Configurations_2”,删除之后要点击左下角的“Apply”保存
-
Run pytest in xx.py方式执行:情况和上面unittest类似
注意:修改工程设置默认的运行器:File->Setting->Tools->Python Integrated Tools->Testing->Default test runner->选择py.test
以Run pytest in xx.py方式执行
三、pytest使用
1.前置后置
- 没有固定的前置和后置名称,名称自定义
- 前置和后置在一个函数里面,在函数名称前面标明
@pytest.fixture
- 使用 yeild 作为分割线,区分前置代码和后置代码
- 在函数名称前面标明前置后置的作用域:测试函数(function)、测试类(class)、模块(module)、测试会话(session)
- 某一个测试类/函数需要用到XX个前置后置,在测试类或者测试用例方法面前声明
@pytest.mark.usefixtures("setup_login")
- 用例要使用前置当中的变量:
- 定义前置和后置函数的时候,要返回变量:
yield 返回值
- 定义前置和后置函数的时候,使用返回的变量:将前置和后置函数名称作为用例方法的参数
- 定义前置和后置函数的时候,要返回变量:
-
共享:
- 所有测试用例的前置和后置放在一个文件中,实现所有测试用例共享前置后置
- 放置共享前置后置的文件名为:conftest.py,文件名不能更改
- confest.py 文件和测试用例文件放在一个目录下,不需要导入,执行测试用例会自动搜索该文件
- 前置后置函数B可以继承前置后置函数A的代码,当调用前置后置函数B(假设函数B作用域为函数),测试方法执行顺序如下:
- 函数A前置->函数B前置->测试方法->函数B后置->函数A后置
import pytest
from selenium import webdriver
from PageObjects.page_login import HandleLoginPage
from TestDatas import common_datas
@pytest.fixture # 默认作用域是函数级别
def setup_login():
# 前置代码
driver = webdriver.Chrome()
driver.maximize_window()
# 访问系统登录页面
driver.get(common_datas.login_url)
use_login = HandleLoginPage(driver)
# 分界线
yield driver, use_login
# 后置代码
driver.quit()
@pytest.fixture # 默认作用域是函数级别
def setup_invest(setup_login):
# 前置代码(来自setup_login的前置)
setup_login[1].login(common_datas.account_datas[0]["username"], common_datas.account_datas[0]["pwd"])
# 分界线
yield setup_login
# 后置代码(无)
2.测试用例方法中使用形参
- 将前置和后置函数名称作为用例方法的参数
- 在测试用例方法面前标明:
@pytest.mark.parametrize(xx_1,xx_2)
- 遍历测试用例数据xx_2。每遍历一次,遍历的测试用例数据会传给xx_1,同时生成测试用例
- xx_1作为自动生成测试用例的形参,两者必须同名
@pytest.mark.parametrize("case", login_datas.wrong_datas)
def test_login_failed_by_wrong_data(self, case, setup_login):
if case["expected"] != "帐号或密码错误!":
# 第二步;登录操作
setup_login[1].login(case["username"], case["pwd"])
# 第三步:断言(比对实际结果和预期结果)
time.sleep(2)
assert setup_login[1].get_error_msg_fir() == case["expected"]
else:
print("此条用例没有执行!!!!")
3.收集用例(按照:目录-文件-函数/类)
默认从当前目录中搜集测试用例,即在哪个目录下运行pytest 命令,则从哪个目录当中搜索,进入目录后,按照搜索规则搜索用例
- 搜索规则:
- 文件: test*.py 或者*test.py 的文件(*代表表示任意字符)
- 函数/类:以test开头的函数/以Test开头的类名下的以test开头的用例
注意:
1.测试用例类以pytest方式运行,测试用例的执行顺序是根据测试用例在测试用例类中的顺序执行,不是根据名称的ASCII顺序执行
2.测试用例类以unittest方式运行,测试用例的执行顺序是根据测试用例名称的ASCII顺序执行
4.断言
assert 表达式:如assert True/ assert False/assert xx==xx
5.筛选用例和重运行
1.筛选用例
- 对测试用例打标签,在运行测试用例的时候,根据标签名来筛选要运行的用例
-
使用方法:
- 创建pytest.ini文件,在文件内填写内容来注册标签名
- 在测试用例/测试类前面加上:
@pytest.mark.已注册的标签名
- 在main文件中添加:
pytest.main(["-m", "要执行用例的标签名"])
2.重运行
- 重运行次数
- 安装重运行模块:
pip install pytest-rerunfailures
- 在main文件中添加:
pytest.main(["--reruns", "2"])
(2是重运行次数)
- 安装重运行模块:
- 重运行间隔时间
-
pytest.main(["-delay", “时间值”])
(单位是秒)
-
6.测试报告
1.JunitXML格式的测试报告
在main文件中添加:pytest.main(["--junitxml=path"])
2.log格式的测试报告
在main文件中添加:pytest.main(["--resultlog=xx.txt"])
3.HTML格式的测试报告
- 安装html模块:
pip install pytest-html
- 在main文件中添加:
pytest.main(["--html=xx.html"])
4.Allure报告
- 安装Allure报告模块:
pip install allure-pytest
- 在main文件中添加:
pytest.main(["--alluredir=path"])
- 查看allure的测试报告:在终端或者cmd中输入:
allure serve allure 报告目录
import pytest
import os
from Common.page_path import REPORTS_DIR
if __name__ == '__main__':
report_xml = os.path.join(REPORTS_DIR, "report.xml")
report_html = os.path.join(REPORTS_DIR, "report.html")
allure_report = os.path.join(REPORTS_DIR, "allure_report")
# main参数里面填写命令行
# pytest命令行——运行某一个文件
# pytest.main(["TestCases/test_01_login.py"])
# 筛选测试用例、重运行和输出测试报告
pytest.main(["-m", "smoke",
"--reruns", "2",
"--junitxml={}".format(report_xml),
"--html={}".format(report_html),
"--alluredir={}".format(allure_report)]
)
注意:pytest框架和unittest框架不要混用,在使用其中一种框架时,要把另一种框架的东西全部去掉,特别是在使用pytest框架时,确认测试类没有继承unittest的TestCase类