Python自动化框架入门-框架与执行
pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点:
- 1、简单灵活,容易上手,文档丰富;
- 2、支持参数化,可以细粒度地控制要测试的测试用例;
- 3、能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests);
- 4、pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等;
- 5、测试用例的skip和xfail处理;
- 6、可以很好的和CI工具结合,例如jenkins
安装
pip install pytest
编写规范:
- 测试文件以test_开头(以_test结尾也可以)
- 测试类以Test开头,并且不能带有 init 方法
- 测试函数以test_开头
- 断言使用基本的assert或者hamcerst库断言
import pytest
def add(x,y):
return x+y;
#测试类
class Testclass:
def test_1(self):
x="this"
assert "h" in x
def test_2(self):
x="x"
assert hasattr(x,"check")
def test_add1(self):
assert add(1, 20) == 21
def test_add2(self):
assert add(1, 2) == 3
运行结果:
使用命令行的运行指定用例
项目结构:
- pytest -v -s test_cass.py -v表示打印详细的信息 -s表示输出print打印信息
- pytest -v -s ./ 行批量文件夹(运行当前文件夹包括子文件夹所有用例)
- pytest -v -s .\path1\ 运行测试包下所有用例
- pytest -v -s 测试类.py::测试方法
- pytest -v -s 测试类.py::测试方法::测试用例
- pytest -v -s -x test_cass.py -x 表示测试用例执行出现错误,将停止执行
- pytest --maxfail=2 test_cass.py -maxfail=2为自定义的失败阈值
错误重试 pytest --reruns 2 错误再执行二次 --reruns2 表示重试的次数
需要装第三方库 pip install pytest-rerunfailures
错误重试 pytest --reruns 2 --reruns-delay 5 错误后等待5s执行二次
pytest框架结构
- 1.模块级:(setup_module、teardown_module)在模块始末调用
- 2.函数级:(setup_function、teardown_function)在函数始末调用 在类外部
- 3.类级:(setup_class、teardown_class)在类始末调用 在类中
- 4.方法级:(setup_method、teardown_method)在方法始末调用 在类中
- 5.方法级:(setup、teardown)在方法始末调用 在类中
调用顺序:
setup_module>setup_class>setup_method>setup>teardown>teardown_method>teardown_class>teardown_module
实例:
def setup_module():
print("\nsetup_module, 只执行一次,当有多个测试类的时候使用")
def teardown_module():
print("\nteardown_module, 只执行一次,当有多个测试类的时候使用")
class TestDemo(object):
@classmethod
def setup_class(cls):
print("\nsetup_class1, 只执行一次")
@classmethod
def teardown_class(cls):
print("\nteardown_class1,只执行一次")
def setup_method(self):
print("\nsetup_method, 每个测试方法执行一次")
def teardown_method(self):
print("\nteardown_method, 每个测试方法执行一次")
def test_three(self):
print("test_three, 测试用例")
def test_four(self):
print("test_four, 测试用例")
class TestPytest2(object):
@classmethod
def setup_class(cls):
print("\nsetup_class2, 只执行一次")
@classmethod
def teardown_class(cls):
print("\nteardown_class2,只执行一次")
def setup_method(self):
print("\nsetup_method2, 每个测试方法执行一次")
def teardown_method(self):
print("\nteardown_method2, 每个测试方法执行一次")
def test_one(self):
print("test_one, 测试用例")
def test_two(self):
print("test_two, 测试用例")
执行结果:
collecting ... collected 2 items
pytest_moduledemo.py::TestDemo::test_three
setup_module, 只执行一次,当有多个测试类的时候使用
setup_class1, 只执行一次
setup_method, 每个测试方法执行一次
PASSED [ 50%]test_three, 测试用例
teardown_method, 每个测试方法执行一次
pytest_moduledemo.py::TestDemo::test_four
setup_method, 每个测试方法执行一次
PASSED [100%]test_four, 测试用例
teardown_method, 每个测试方法执行一次
teardown_class1,只执行一次
teardown_module, 只执行一次,当有多个测试类的时候使用
分类:
python自动化
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)