pytest配置

安装

pip install pytest
// 自动生成HTML格式测试报告
pip install pytest-html

PyCharm配置pytest

点击PyCharm->Preferences->Tools->Python Intergrated Tools->Testing->Default test runner->选择pytest->点击OK 在有测试函数的文件中右键点击绿色小三角,可运行自动化测试

pytest检查项目

配置:点击PyCharm—>Preferences—>Tools—>Externals Tools—>点击+

  • Name:pytest
  • Program:/Library/Frameworks/Python.framework/Versions/3.8/bin/pytest(pytest所在目录,可通过which pytest查看路径)
  • Working directory:$FileDir$(表示文件路径,不包含文件名)
    配置完成,使用时点击PyCharm—>Tools—>External Tools—>pytest
  • pytest报告生成html

    配置:点击PyCharm—>Preferences—>Tools—>Externals Tools—>点击+

  • Name:pytest(可自行制定)
  • Program:/Library/Frameworks/Python.framework/Versions/3.8/bin/pytest(pytest所在目录,可通过which pytest查看路径)
  • Arguments:--html=testReport.html(具体路径可自行执行)
  • Working directory:$FileDir$(表示文件路径,不包含文件名)
    配置完成,使用时点击PyCharm—>Tools—>External Tools—>pytest
  • pytest检查当前文件

    配置:点击PyCharm—>Preferences—>Tools—>Externals Tools—>点击+

  • Name:pytest Current
  • Program:/Library/Frameworks/Python.framework/Versions/3.8/bin/pytest(pytest所在目录,可通过which pytest查看路径)
  • Arguments:FileName(表示当前右键选中的文件)
  • Working directory:$FileDir$(表示文件路径,不包含文件名)
    配置完成,使用时点击PyCharm—>Tools—>External Tools—>pytest Current
  • 测试样例规范

  • 测试文件以 test_ 开头(或以 _test 结尾)
  • 测试类以Test开头(不能包含init方法)
  • 测试函数以test_开头
  • 批量执行规则

  • pytest会查找当前以及递归查找子文件夹下的所有以test_ 开头(或以 _test 结尾)的文件
  • 在测试文件中
  • pytest会查找以Test开头的类,以test_开头的方法
  • pytest会查找不在类中的以test_开头的方法或函数
  • 指定测试用例

  • 指定测试文件路径pytest /path/to/test/file.py
  • 指定测试类pytest /path/to/test/file.py:TestCase