Pytest 单元测试框架入门

1、pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效

2、安装 pytest

pip install pytest

3、验证 pytest 是否安装成功

pip show pytest

4、使用 pytest 执行测试需要遵行的规则

  • .py 测试文件必须以 test_ 开头(或者以 _test 结尾) 

  • 测试类必须以 Test 开头,并且不能有 init 方法

  • 测试方法必须以 test_ 开头

  • 断言必须使用 assert,pytest 中没有自带的断言方法

5、pytest 执行方式

  • pytest –v filename(最高级别信息 — verbose)
  • pytest -s filename(输出打印)
  • pytest -v -s filename
  • pytest -q filename(静默输出,不会打印用例输出)

6、实例介绍一

  • dos 窗口中执行用例
# test_demo1.py

def test_01():
    print("深圳多测师")

def test_02():
    print("广州多测师")

def test_03():
    print("上海多测师")
    • 执行方法一:pytest -q test_demo1.py

    • 执行方法二:pytest -s test_demo1.py

    • 执行方法三:pytest -v test_demo1.py

    • 执行方法四:pytest -v -s test_demo1.py

7、实例介绍二

  • demo1
    • demo1 发现结果中没有用例的执行打印结果  
# test_demo.py

import pytest

def test_01():
    print("深圳多测师")

def test_02():
    print("广州多测师")

def test_03():
    print("上海多测师")

if __name__ == '__main__':
    pytest.main()

# 结果如下
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: D:\work_doc\CodeFile\practice\pytest_demo
plugins: html-2.1.1, metadata-1.9.0
collected 3 items

test_demo1.py ...                                                        [100%]
============================== 3 passed in 0.05s ==============================
Process finished with exit code 0
  • demo2
    • demo2 中在 main() 中加入 ["-s"],结果中就可以展示打印结果
import pytest

def test_01():
    print("深圳多测师")

def test_02():
    print("广州多测师")

def test_03():
    print("上海多测师")

if __name__ == '__main__':
    pytest.main(["-s"])    # 在 main() 中加入 ["-s"] 

# 结果如下
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: D:\work_doc\CodeFile\practice\pytest_demo
plugins: html-2.1.1, metadata-1.9.0
collected 3 items

test_demo1.py 深圳多测师
.广州多测师
.上海多测师
.
============================== 3 passed in 0.02s ==============================
Process finished with exit code 0
  • demo3
    • 运行指定的用例文件
# test_demo1.py

def test_01():
    print("深圳多测师")

def test_02():
    print("广州多测师")

def test_03():
    print("上海多测师")
# run_all.py

import pytest

if __name__ == '__main__':
    pytest.main(["D:/test_demo1.py"])

# 结果如下
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: D:\
plugins: html-2.1.1, metadata-1.9.0
collected 3 items

test_demo1.py ...                                                        [100%]
============================== 3 passed in 0.01s ==============================
Process finished with exit code 0
  • demo4
    • 运行指定目录下的用例文件
# demo1/test_demo1.py  demo1 目录下的 test_demo1.py 文件

def test_01():
    print("深圳多测师")

def test_02():
    print("广州多测师")

def test_03():
    print("上海多测师")
# demo2/test_demo2.py  demo2 目录下的 test_demo2.py 文件

def test_04():
    print("杭州多测师")

def test_05():
    print("北京多测师")

def test_06():
    print("重庆多测师")
import pytest

if __name__ == '__main__':
    pytest.main(["D:/demo1""D:/demo2"])    # 指定 D:/demo1 和 D:/demo2 目录

# 结果如下
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: D:
plugins: html-2.1.1, metadata-1.9.0
collected 6 items

demo1\test_demo1.py ...                                                  [ 50%]
demo2\test_demo2.py ...                                                  [100%]
============================== 6 passed in 0.04s ==============================
Process finished with exit code 0

 8、pytest-html 生成测试报告

  • 安装 pytest-html
pip install pytest-html
  • cmd 下执行用例且生成报告

  • 代码中生成测试报告
import pytest

def test_01():
    print("深圳多测师")

def test_02():
    print("广州多测师")

def test_03():
    print("上海多测师")

if __name__ == '__main__':
    pytest.main(["-q","test_demo1.py","--html=report.html"])    # --html=report.html 生成测试报告,也可以自定义报告路径如:--html=d://report.html

猜你喜欢

转载自www.cnblogs.com/ZhengYing0813/p/12877802.html