1.pytest-html报告
安装:pip install pytest-html==2.1.1
运行:pytest 文件名 --html=路径/文件名称
2.pytest-ordering:控制函数执行的顺序
安装:pip install pytest-ordering
@pytest.mark.run(order=X):用于标记被测试函数
根据order传入的参数解决运行顺序
order值全为正数或全为负数时,运行顺序:值越小,优先级越高
正数和负数同事存在时,正数优先级高
2.pytest-rerunfailures
在配置行的命令参数中增加 --reruns n,如:
pytest -s 文件名 --reruns 3 --reruns-delay 1(失败重跑次数3,失败后延迟一秒)
3.跳过测试函数、
在需要跳过的脚本标记:@pytest.mark.skip(reason="原因")
4.预期失败
应用场景:功能尚未实施或尚未修复的错误,结果会预计失败;希望的某种情况会失败
@pytest.mark.xfail()
class Test_xfail:
@pytest.mark.xfail()
def test_cass01(self):
print("失败")
assert 1==2
def test_cass02(self):
assert 1==1
5.数据参数化
应用场景:如登录时需要多个账户名和密码是,可用参数化实现该功能
@pytest.mark.parametrize(参数名,参数对应值)
参数名:为字符串
参数对应值:为列表
单个参数的传递:
class Test_per:
@pytest.mark.parametrize("age",[1,44,23,34])
def test_a(self,age):
print(age)
多个参数的传递:
class Test_per1:
@pytest.mark.parametrize(("name","age"),[("nini",12),("qq",1),("as",34)])
def test_a(self,name,age):
print(name,age)
6.pytest-fixture
应用场景:完成预置处理和重复操作
@pytest.fixture
如:登录,用户a需要先登录,用户b不需要登录,用户c需要登录
class Test_fixture:
@pytest.fixture()
def login(self):
print("登录操作")
uname="lulu"
return uname
def test_a(self,login):
print(f"test_a{login}")
def test_b(self):
print("不需要登录操作")
def test_c(self,login):
print(f"test_c{login}")


