PyTest

一、命令#

常用参数#

1. 忽略报警信息#

-p no:warnings

2. pdb调试#

ipdb有的版本可以有的不行

--pdb

3. 指定工作目录#

有时候需要使用python -m pytest才生效, 直接pytest不生效 总之不如使用sys.path.insert

--rootdir /home/path/

4. Django使用pytest测试#

pip install pytest-django
pytest -s -vv .\tests\test_step2.py  --rootdir X:\Code\workflows\ --ds project.settings
  • rootdir 指定项目根目录
  • ds 指向django setting.py 文件

二、泛论#

1、为什么需要pytest#

helps you write better programs

  • 提高阅读理解代码效率
  • 提高debug效率
  • 提高开发效率
  • 保证交付代码质量

简单例子#

入门例子:

  1. 了解使用test文件命名格式: test_前缀
  2. 了解断言assert
  3. 了解测试输出
# content of test_sample.py
def inc(x):
    return x + 1

def test_answer():
    assert inc(3) == 5

输出