pytest【一、启动测试和常用参数】

1、命令行启动

pytest框架是python经典单元测试框架,运行会自动识别测试项目中的测试用例,包含测试模块module(以test_开头)、测试类class(以Test开头)、测试函数function(以test开头)

命令: pytest [options] [file_or_dir] [file_or_dir] [...]

  • 指定当前项目下的测试所有module: pytest [option] .

  • 指定某个文件:                   pytest [option] test1.py

  • 指定某个测试类:                 pytest [option] test1.py::TestCaseClass

  • 指定某个测试函数:      pytest [option] test1.py::TestCaseClass::test_func

  • 常用option参数:

  • -v: 显示测试的详细信息
  • -s: 打印测试函数用例中的print信息,方便调试
  • -q: 与-v相反, 静默输出信息
  • -k: 允许使用表达式指定希望执行的测试用例, 批量执行用例, 例如: demo.py有三个测试用例函数,分别为: test_result、test_answer、test_func,
    pytest -s -v -k "func or result" demo.py 则只会执行test_func 和 test_result用例函数
  • -x: 失败一次后不再执行后面的测试用例
  • -maxfail=num: 最大失败次数,失败超过次数不再执行后面的用例
  • -m: 配合pytest.mark使用,用作测试用例分组

-k参数实例:
在这里插入图片描述
-m参数实例:

在新版本的pytest中, 需要在pytest.ini中添加mark, 或者通过conftest.py中定义pytest_configure()函数添加mark(后面记录), 否则在执行测试用例过程中会产生PytestUnknownMarkWarning告警, pytest.ini应放在项目的根路径
pytest.ini添加如下:

[pytest]
markers =
   finish: has finished
   over: has over

在这里插入图片描述

在这里插入图片描述

2、pytest.main()启动

直接调用pytest.main()方法启动, 以列表形式传入各个命令行参数

# -*- coding: UTF-8 -*-
# ---------------------------------------------------------
# @Author : Bingo
# @Time   : 2021/4/17 0:03
# @File   : demo1.py  
# ---------------------------------------------------------
# There is no bug, have a great day. Please start your code.
import pytest


@pytest.mark.finish
def test_result():
    assert 1 == 1


@pytest.mark.over
def test_answer():
    assert 2 + 0 == 2


def test_func():
    assert "hello".startswith("he")


if __name__ == "__main__":
    pytest.main(["-s", "-v", "-m finish or over", "test_demo1.py"])

运行结果如下:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

八戒无戒i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值