python接口自動化11-pytest入門

前言

pytest是一個很是成熟的全功能的Python測試框架,適合從簡單的單元到複雜的功能測試,主要特色有如下幾點:html

  • 簡單靈活,容易上手;
  • 支持參數化;
  • 可以支持簡單的單元測試;
  • 標記測試功能與屬性
  • 複雜的功能測試,好比能夠作selenium等自動化測試、接口自動化測試(pytest+requests);
  • pytest具備不少第三方插件,而且能夠自定義擴展,比較好用的如pytest-selenium(集成selenium)、pytest-html(完美html測試報告生成)等;
  • Skip和xfail:處理不成功的測試用例;
  • 能夠很好的和jenkins集成;
  • 經過xdist插件分發測試到多個CPU

1、簡介

一、環境搭建推薦版本匹配:pip install pytest==3.6.3python

  • Python3.6.x + pytest 3.6.3
  • Python3.7.x + pytest 4.0.2

二、查看版本:pytest --versionapi

C:\Users\Administrator>pytest --version
This is pytest version 三、六、3, imported from d:\path_python\lib\site-packages\pytest、py

三、pytest 命名規則:session

  • 文件名以test_*、py 或 *_test、py
  • 類已 Test* 開頭
  • 函數/方法以 test_* 開頭

四、pytest 直接寫用例,寫完 cmd 運行,不須要導入其餘模塊。框架

G:\python_study\study\pytest_demo\study>pytest -s test_demo1.py
================================================= test session starts =================================================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 2 items
test_demo1.py 我是用例:a . 我是用例:b .
============================================== 2 passed in 0.02 seconds ===============================================

2、pytest 命令行參數介紹

一、運行規則:pytest py文件路徑函數

C:\Users\Administrator>pytest G:\python_study\study\pytest_demo\study\test_demo.py
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0
rootdir: C:\Users\Administrator, inifile:
collected 4 items

test_demo.py ....                                                        [100%]

========================== 4 passed in 0.03 seconds ===========================

二、顯示打印信息(否則不會看到打印內容):pytest -s xxx單元測試

G:\python_study\study\pytest_demo\study>pytest -s test_demo1.py
================================================= test session starts =================================================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 2 items

test_demo1.py
我是用例:a
.
我是用例:b
.

============================================== 2 passed in 0.02 seconds ===============================================

三、顯示詳細信息:pytest -v xxx測試

G:\python_study\study\pytest_demo\study>pytest test_demo1.py -v
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0 -- d:\path_python\python.exe
cachedir: .pytest_cache
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 2 items

test_demo1.py::Test_api::test_a PASSED                                   [ 50%]
test_demo1.py::Test_api::test_b PASSED                                   [100%]

========================== 2 passed in 0.02 seconds ===========================

四、簡潔顯示信息:pytest -q xxxspa

G:\python_study\study\pytest_demo\study>pytest test_demo1.py -q
..                                                                       [100%]
2 passed in 0.02 seconds

五、運行指定用例:pytest -k case_name    (case_name可類可函數,模糊匹配關鍵字),以下匹配 demo插件

G:\python_study\study\pytest_demo\study>pytest -k demo -v
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0 -- d:\path_python\python.exe
cachedir: .pytest_cache
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 11 items / 5 deselected

test_demo.py::test_ab PASSED                                             [ 16%]
test_demo.py::test_aba PASSED                                            [ 33%]
test_demo.py::Test_api::test_aa PASSED                                   [ 50%]
test_demo.py::Test_api::test_b PASSED                                    [ 66%]
test_demo1.py::Test_api::test_a PASSED                                   [ 83%]
test_demo1.py::Test_api::test_b PASSED                                   [100%]

=================== 6 passed, 5 deselected in 0.06 seconds ====================

六、命令行參數不分順序,還有其餘命令行參數,不一一細說:

  • 運行類用例且不運行類某個用例:pytest -v -k "Test_api1 and not test_a"
  • 失敗中止測試:pytest -x
  • 指定個數失敗後中止測試:pytest --maxfail=2
  • 運行上一次失敗用例(或沒失敗的):pytest --last-failed
  • 等等

七、pycharm 設置 pytest 運行用例:

更多請查看 pytest -h 或者找度娘,咱們通常用以上的參數夠平常使用了。歡迎來QQ交流羣:482713805

相關文章
相關標籤/搜索