pytest使用

  1. 使用介绍
    2.1. 安装
    pip install pytest

2.2. 示例代码
编写规则
编写pytest测试样例非常简单,只需要按照下面的规则:

测试文件以test_开头(以test结尾也可以)
测试类以Test开头,并且不能带有 init 方法
测试函数以test
开头
断言使用基本的assert即可

如何执行
pytest # run all tests below current dir
pytest test_mod.py # run tests in module file test_mod.py
pytest somepath # run all tests below somepath like ./tests/
pytest -k stringexpr # only run tests with names that match the

the "string expression", e.g. "MyClass and not method"

will select TestMyClass.test_something

but not TestMyClass.test_method_simple

pytest test_mod.py::test_func # only run tests that match the "node ID",

e.g "test_mod.py::test_func" will be selected

only run test_func in test_mod.py

Console参数介绍
-v 用于显示每个测试函数的执行结果
-q 只显示整体测试结果
-s 用于显示测试函数中print()函数输出
-x, --exitfirst, exit instantly on first error or failed test
-h 帮助

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 概述 pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点: 1、简单灵活,容易上手...
    红薯爱帅阅读 263,108评论 6 81
  • pytest使用 1、pytest简介 官档是最好的教程 pytest是一个非常成熟的全功能的Python测试框架...
    道无虚阅读 9,724评论 1 5
  • 使用pytest框架执行测试样例的方法很多种,我们可以看下pytest的帮助命令 C:\Users\DELL>py...
    mysimplebook阅读 11,768评论 0 0
  • pytest是第三方开发的一个python测试模块,可以轻松地编写小型测试,而且可以扩展以支持应用程序和库的复杂功...
    何小有阅读 13,240评论 0 3
  • 写在前面 文档中一部分摘抄了pytest中文文档和百度的搜索结果,记录下来方便自己以后查阅。 参数选项 -s 允许...
    Lacia阅读 968评论 0 0