pytest重构性学习

pytest作为一个测试框架,基于python自带unittest进行迭代优化,是作为测试,测开,开发的测试用例不二之选

python 安装 pytest
pip install pytest
注释
pip 是对python 第三方包的管理工具 具体参考资料请在确保设置正确的环境变量后使用 pip -help 查看
image.png
pytest 0.0.1 pytest简单使用
使用pycharm创建一个python项目
image.png
如图所示,本次使用在main函数中进行pytest为例
pytes.main(['main.py'])
其中 main后跟了相关函数,确保pytest扫描main.py文件中的test_文件,此时扫描到了一个函数,
def test_01()
run:产生相关结果 failures 1确实是不等于2的 使用assert进行判断是python常用的判断抛出函数image.png
pytest.main(['./testpath']) 仅测试固定包下的test文件
image.png
-s: 显示程序中的 print/logging 输出
-v: 丰富信息模式, 输出更详细的用例执行信息
-k: 运行包含某个字符串的测试用例。如:pytest -k add XX.py 表示运行 XX.py 中包含 add 的测试用例。
-q: 简单输出模式, 不输出环境信息
-x: 出现一条测试用例失败就退出测试。在调试阶段非常有用,当测试用例失败时,应该先调试通过,而不是继续执行测试用例。
pytest fixture断言使用
image.png
通过 @pytest.fixture对pytest_01进行标记,使得test_01先调用getnum后得到4再进行判断

image.png
使用pytest.fixture调用两个函数

@pytest.fixture(autouse=True) 多层级调用时建议使用,自动进行数值传递并回调
image.png
@pytest.fixture(scope=‘class’)
image.png
scope作用域 从小到大 function class module package session
其中 function:默认作用域,fixture在测试结束时销毁
yield user
mail_admin.delete_user(user)

def test_email_received(sending_user, receiving_user):
email = Email(subject=“Hey!”, body=“How’s it going?”)
sending_user.send_email(email, receiving_user)
assert email in receiving_user.inbox
使用yield 返回pytest.fixture的值 但在返回后函数将执行yield之后的语句,删掉相关的user

pytest参数化
@pytest.fixture(scope=“module”, params=[“smtp.gmail.com”, “mail.python.org”])

params smt.qmial.com=mail.python.org
yield smtp_connection
@pytest.mark.parametrize函数的使用,一样是标记参数的
image.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值