Pytest

来自tomtalk
跳转至: 导航搜索

如何编写pytest测试样例

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

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

运行模式

# 1.运行后生成测试报告(htmlReport)
pip install -U pytest-html
pytest --html=report.html
 
#2.运行指定的case
pytest test_se.py
pytest test_se.py::TestClassOne
pytest test_se.py::TestClassTwo::test_one
 
#3.多进程运行cases
pip install -U pytest-xdist
pytest test_se.py -n NUM   #其中NUM填写并发的进程数。
 
#4.重试运行cases
pip install -U pytest-rerunfailures
pytest test_se.py --reruns NUM    #NUM填写重试的次数。
 
#5.显示print内容
pytest test_se.py -s
 
#另外,pytest的多种运行模式是可以叠加执行的,比如说,你想同时运行4个进程,又想打印出print的内容。可以用:
pytest test_se.py -s -n 4