1. 程式人生 > 其它 >Pytest 測試框架

Pytest 測試框架

Pytest 是什麼?

  • pytest 能夠支援簡單的單元測試和複雜的功能測試;
  • pytest 可以結合 Requests 實現介面測試; 結合 Selenium、Appium 實現自動化功能測試;
  • 使用 pytest 結合 Allure 整合到 Jenkins 中可以實現持續整合。
  • pytest 支援 315 種以上的外掛;

為什麼要選擇 Pytest

  • 豐富的第三方外掛
    • 報告
    • 多執行緒
    • 順序控制
  • 簡單靈活
  • 相容 unittest
  • 定製化外掛開發

Pytest 安裝與準備

Pytest 環境安裝

  • 前提:本地已配置完成Python環境
  • 第一種方式 pip install pytest
  • 第二種方式 PyCharm 直接安裝

執行第一個指令碼

# content of test_sample.py
def inc(x):    
    return x + 1  
def test_answer():    
    assert inc(3) == 5

 執行輸入:

D:\Project\WebDemo>pytest test_demo.py
======================================== test session starts ========================================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\Project\WebDemo
collected 1 item                                                                                     

test_demo.py F                                                                                 [100%]

============================================= FAILURES ==============================================
____________________________________________ test_answer ____________________________________________

    def test_answer():
>       assert inc(3) == 5
E       assert 4 == 5
E        +  where 4 = inc(3)

test_demo.py:7: AssertionError
====================================== short test summary info ======================================
FAILED test_demo.py::test_answer - assert 4 == 5
========================================= 1 failed in 0.08s =========================================