pytest的安装和执行(一)

1、安装pytest并导入:
pip install pytest
然后在自己的.py文件中导入import pytest
(这些都不详细讲了,基础)

2、如果想让pytest去测试你写的测试用例的话,必须满足pytest的规则,它才能识别

  • 文件名以 test_.py 文件和_test.py
  • 以 test_ 开头的函数
  • 以 Test 开头的类,不能包含 init 方法
  • 以 test_ 开头的类里面的方法
  • 所有的包 pakege 必项要有__init__.py 文件
  1. 控制台直接运行pytest
    ①会查找当前目录及其子目录下以 test_*.py 或 *_test.py 文件, 找到文件后,在文件中找到以 test 开头函数并执行
  2. 只想执行某个文件,可以 pytest test_demo.py
  3. 加上-q,就是显示简单的结果: pytest -q test_demo.py

Pytest执行用例规则
下面以windows系统为例,使用命令来来执行pytest

1、指定目录下的所有用例
pytest
2、执行某一个py文件下用例
pytest 文件名.py
3、运行test_demo.py文件中模块里面的某个函数,或者某个类,某个类里面的方法
说明:加v和不加-v都可以,加-v的话,打印的信息更详细

pytest -v test_demo.py::TestClass::test_ad
pytest test_demo.py::TestClass::test_not_in
pytest test_demo.py::test_in

4、运行test_demo.py 模块里面,测试类里面的某个方法

pytest test_demo.py::test_in

5、-m 标记表达式(后面有详解)

pytest -m login

将运行用 @pytest.mark.login 装饰器修饰的所有测试,后面有详解!

6、-q 简单打印,只打印测试用例的执行结果

pytest -q test_demo.py

7、-s 详细打印

pytest -s test_demo.py

8、-x 遇到错误时停止测试

pytest test_demo.py -x

9、—maxfail=num,当用例错误个数达到指定数量时,停止测试

pytest test_demo.py --maxfail=1

10、-k 匹配用例名称

pytest -s -k _in test_demo.py

11、-k 根据用例名称排除某些用例

pytest -s -k "not _in" test_demo.py

12、-k 同时匹配不同的用例名称

pytest -s -k "add or _in" test_demo.py

pytest兼容unittest脚本,所以不影响之前使用unittest编写的脚本


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值