pytest 使用

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文件的里面的写法

导入pytest 的包
pytest.main(["列表的参数,可以是生成测试报告的命令,也可以过滤测试用例,参数可以是多个"])

conftest 这个文件pytest 可以自动识别,不需要引入进去
可以再这个文件定义fixture的函数

fixture : 即测试用例执行的环境准备和清理
定义fixture :
把一个函数定义为Fixture很简单,在函数声明之前加上@pytest.fixture(scop="四个级别,默认是函数级别的",autouse=False 自动识别,默认关闭状态
如果改为True,每个测试用例执行之前都会使用的前置)
那么在一个fixture 内部如何区分环境准备、环境清理呢
在函数内使用yield 关键字。
yield 关键字以后得代码,就是环境清理的代码,即在测试用例执行完成之后会执行的代码

yield 后面跟函数的返回值可以是一个列表、也可以是一个元祖

调用fixture
在要调用的类前面加上
@pytest.mark.usefixtures("里面是定义的fixture的函数名")

接受返回值
直接在类里面的函数参数的地方,传入定义的fixture的函数名,就可以传参
如果是元祖取值 函数名[下标]

pytest的断言使用
assert 后面加表达式 N == True


pytest - 重运行机制
插件名称:rerunfailures
安装方式:pip install pytest-rerunfailures

使用方式:
命令行参数形式:
命令:pytest --reruns 重试次数
失败之后运行间隔时间:
pytest --reruns 重试次数 --reruns-delay 次数之间的延时设置(单位:秒)

猜你喜欢

转载自www.cnblogs.com/666666pingzi/p/10686976.html