import pytest
from web_ui_YXBI.test_datas.common_datas import Common_Datas as c
from selenium import webdriver
from web_ui_YXBI.page_objects.test_login_page import loginPage
driver = None
@pytest.fixture
def init_page():
global driver
# 前置
print("==========整个测试用例都会执行的前置==========")
yield
# 后置
print("==========整个测试用例都会执行的后置==========")
driver.get(c.url)
@pytest.fixture(scope="class")
def init_driver():
global driver
# 前置
print("==========整个测试类只执行一次的前置==========")
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(c.url)
lg =loginPage(driver)
yield (driver,lg)
# 后置
print("==========整个测试类只执行一次的后置==========")
driver.quit()
pytest 生成测试报告
1.JunitXML格式的测试报告 :命令 : --junitxml=path
2.生成result log 格式测测试报告: 命令: --resultlog=report\log.txt
3.生成Html格式的测试报告: 命令: --html=跟生成报告所在的文件路径 report\test_one_func.html
main文件的里面的写法html
导入pytest 的包
pytest.main(["列表的参数,能够是生成测试报告的命令,也能够过滤测试用例,参数能够是多个"])web
conftest 这个文件pytest 能够自动识别,不须要引入进去
能够再这个文件定义fixture的函数 函数
fixture : 即测试用例执行的环境准备和清理
定义fixture :
把一个函数定义为Fixture很简单,在函数声明以前加上@pytest.fixture(scop="四个级别,默认是函数级别的",autouse=False 自动识别,默认关闭状态
若是改成True,每一个测试用例执行以前都会使用的前置)
那么在一个fixture 内部如何区分环境准备、环境清理呢
在函数内使用yield 关键字。
yield 关键字之后得代码,就是环境清理的代码,即在测试用例执行完成以后会执行的代码测试
yield 后面跟函数的返回值能够是一个列表、也能够是一个元祖ui
调用fixture
在要调用的类前面加上
@pytest.mark.usefixtures("里面是定义的fixture的函数名")url
接受返回值
直接在类里面的函数参数的地方,传入定义的fixture的函数名,就能够传参
若是是元祖取值 函数名[下标]插件
pytest的断言使用
assert 后面加表达式 N == True命令行
pytest - 重运行机制
插件名称:rerunfailures
安装方式:pip install pytest-rerunfailurescode
使用方式:
命令行参数形式:
命令:pytest --reruns 重试次数
失败以后运行间隔时间:
pytest --reruns 重试次数 --reruns-delay 次数之间的延时设置(单位:秒)xml