1. 程式人生 > 實用技巧 >自動化測試之Pytest入門

自動化測試之Pytest入門

pytest是一個功能非常全面的Python自動化測試框架

特點:

1、簡單靈活,支援引數化,可以細粒度的控制測試用例;

2、不僅支援簡單的單元測試,還支援複雜的功能測試,不僅可以用來做selenium/appium的UI自動化測試,還可以用作做基於Python+requests的介面自動化測試;

3、第三方外掛非常豐富,如pytest-selenium(集成了selenium)、pytest-html(完美的html測試報告)、pytest-reunfailures(失敗case重複測試)等;

4、測試用例跳躍式(skip)執行以及標記後選擇性處理(mark);

5、很好的與CI工具(jenkins)結合;

編寫規則:

1、測試檔案以test_開頭,以_test結尾也可以;

2、測試類以Test開頭,並且不能帶有__init__方法;

3、測試函式(方法)以test_開頭;

4、斷言使用assert;

一、安裝pytest

$ pip install pytest

二、執行一個測試

測試通過

# test_demo8.py

def test_pass():
    assert 1 == 1

通過pytest來執行上面測試函式

在pycharm的Terminal中直接輸入命令pytest test_demo8.py執行

MacBook-Pro:test_demo fyuanyuan$ pytest test_demo8.py

========================================================================================= test session starts ==========================================================================================
platform darwin -- Python 3.7.2, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: /Users/fujinjie/PycharmProjects/test_demo
plugins: html-2.0.1, allure-pytest-2.8.10, metadata-1.8.0
collected 1 item

test_demo8.py . [100%]

========================================================================================== 1 passed in 0.01s ===========================================================================================

執行結果為test_demo8.py測試通過(pytest使用 . 標識測試通過PASSED)

下面使用-v選項來展示測試的詳細資訊

MacBook-Pro:test_demo fujinjie$ pytest -v test_demo8.py
========================================================================================= test session starts ==========================================================================================
platform darwin -- Python 3.7.2, pytest-5.3.5, py-1.8.1, pluggy-0.13.1 -- /usr/local/bin/python3.7
cachedir: .pytest_cache
metadata: {'Python': '3.7.2', 'Platform': 'Darwin-19.6.0-x86_64-i386-64bit', 'Packages': {'pytest': '5.3.5', 'py': '1.8.1', 'pluggy': '0.13.1'}, 'Plugins': {'html': '2.0.1', 'allure-pytest': '2.8.10', 'metadata': '1.8.0'}}
rootdir: /Users/fujinjie/PycharmProjects/test_demo
plugins: html-2.0.1, allure-pytest-2.8.10, metadata-1.8.0
collected 1 item                                                                                                                                                                                       

test_demo8.py::test_passing PASSED                                                                                                                                                               [100%]

========================================================================================== 1 passed in 0.01s ===========================================================================================

測試失敗

# test_demo8.py

def test_fail():
    assert 1 ==2 

同樣在命令列執行pytest test_demo8.py命令

MacBook-Pro:test_demo fujinjie$ pytest test_demo8.py
========================================================================================= test session starts ==========================================================================================
platform darwin -- Python 3.7.2, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: /Users/fujinjie/PycharmProjects/test_demo
plugins: html-2.0.1, allure-pytest-2.8.10, metadata-1.8.0
collected 1 item                                                                                                                                                                                       

test_demo8.py F                                                                                                                                                                                  [100%]

=============================================================================================== FAILURES ===============================================================================================
_____________________________________________________________________________________________ test_failing _____________________________________________________________________________________________

    def test_fail():
>       assert 1 == 2
E       assert 1 == 2

test_demo8.py:8: AssertionError
========================================================================================== 1 failed in 0.05s ===========================================================================================

測試結果為失敗,pytest使用 F 來標識測試失敗FAILED

pytest中使用assert來進行斷言。

三、標記函式

pytest預設查詢當前目錄下所有以test開始或者結尾的Python檔案,並執行其中所有以test開始或結束的函式(方法)

run.py

#--coding:utf-8 --
#run.py
import pytest if __name__ == '__main__': pytest.main(['-v'])

test_demo9.py

#--coding:utf-8 --
#test_demo9.py

def test_demo9_func1():
    assert 1 == 1

def test_demo9_func2():
    assert 2 == 2

test_demo10.py

#--coding:utf-8 --
#test_demo10.py

def test_demo10_func3():
    assert 3 == 3

def test_demo10_func4():
    assert 4 == 4

因為run.py/test_demo9.py/test_demo10.py在同一個demo目錄下,所以執行run.py時pytest會查詢該目錄下所有以test開頭或結尾的Python檔案,並執行檔案中所有以test開頭或結尾的函式和方法

============================= test session starts ==============================
platform darwin -- Python 3.7.2, pytest-5.3.5, py-1.8.1, pluggy-0.13.1 -- /usr/local/bin/python3.7
cachedir: .pytest_cache
metadata: {'Python': '3.7.2', 'Platform': 'Darwin-19.6.0-x86_64-i386-64bit', 'Packages': {'pytest': '5.3.5', 'py': '1.8.1', 'pluggy': '0.13.1'}, 'Plugins': {'html': '2.0.1', 'allure-pytest': '2.8.10', 'metadata': '1.8.0'}}
rootdir: /Users/fujinjie/PycharmProjects/test_demo/templates/demo
plugins: html-2.0.1, allure-pytest-2.8.10, metadata-1.8.0
collecting ... collected 4 items

test_demo10.py::test_demo10_func3 PASSED                                 [ 25%]
test_demo10.py::test_demo10_func4 PASSED                                 [ 50%]
test_demo9.py::test_demo9_func1 PASSED                                   [ 75%]
test_demo9.py::test_demo9_func2 PASSED                                   [100%]

============================== 4 passed in 0.02s ===============================

由於某些原因,我只想執行test_demo9_func2()測試用例看是否通過,通過pytest有以下幾種方法:

方法一,執行時指定函式名,通過 :: 標記

#--coding:utf-8 --
#run.py
import pytest


if __name__ == '__main__':
    pytest.main(['test_demo9.py::test_demo9_func2','-v'])

或者在命令列中輸入