python 自動化測試 pytest 的使用


pytest 是一款以python為開發語言的第三方測試,主要特點如下:

  1.  比自帶的 unittest 更簡潔高效,兼容 unittest框架

  2. 支持參數化

  3. 可以更精確的控制要測試的測試用例

  4. 豐富的插件,已有300多個各種各樣的插件,也可自定義擴展,如pytest-selenium、pytest-html、pytest-rerunfailures、pytes-xdish

  5. 可很好的和CI工具結合

 

安裝

pip install pytest
 
        

 

測試用例編寫規則

  1. 測試文件以test_開頭 或者 _test結尾

  2. 測試類以Test開頭,並且不能帶有 init 方法

  3. 測試文件以 test_開頭

  4. 斷言使用基本的 assert 即可

pytest會遞歸查找當前目錄及子目錄下所有 以test_開始 或者 _test結尾的python腳本,執行其中符合規則的函數和方法,不需要顯示調用

 

運行命令:(cmd進入用例所在目錄)

  1. pytest folder_name ======》直接運行文件夾內符合規則的所有用例
  2. pytest test_file.py ======》執行某個py文件中的用例
  3. pytest test_file.py::test_func ======》執行模塊內的某個函數(節點運行)
  4. pytest test_file.py::TestClass::test_method ======》執行模塊內測試類的某個方法(節點運行)
  5. pytest test_file.py::TestClass ======》執行模塊內某個測試類(節點運行)
  6. pytest test_file.py::TestClass test_file2.py::test_mothod ======》多節點運行,中間用空格隔開
  7. pytest -k pass ======》匹配用例名稱的表達式,含有“pass”的被執行,其他的deselected
  8. pytest -k "pass or fail" ======》組合匹配,含有“pass” 和 “fail”的被執行
  9. pytest -k "not pass" ======》排除運行,不含“pass”的被執行
  10. pytest -m finished ======》標記表達式,運行用@pytest.mark.finished 標記的用例
  11. pytest -m "finished and not merged" ======》多個標記邏輯匹配,運行含有finished 不含 merged標記的用例
  12. pytest -v ======》運行時顯示詳細信息
  13. pytest -s ======》顯示打印消息
  14. pytest -x  ======》遇到錯誤就停止運行
  15. pytest -x --maxfail=2 ======》遇到兩個錯誤就停止運行
  16. pytest --setup-show ======》跟蹤固件運行
  17. pytest -v --reruns 5 --reruns-delay 1 ======》運行失敗的用例間隔1s重新運行5次  pip install pytest-rerunfailures
  18. pytest ======》多條斷言,報錯后,后面的依然執行, pip install pytest-assume,斷言 pytest.assume(2==4)
  19. pytest -n 3 ======》3個cpu並行執行測試用例,需保證測試用例可隨機執行, pip install pytest-xdist分布式執行插件,多個cpu或主機執行
  20. pytest -v -n auto ======》自動偵測系統里cpu的數目
  21. pytest --count=2 ======》重復運行測試 pip install pytest-repeat
  22. pytest --html=./report/report.html ======》生成報告,此報告中css是獨立的,分享時會丟失樣式,pip install pytest-html
  23. pytest --html=report.html --self-containd-html ======》合並css到html報告中,除了passed所有行都被展開
  24. pytest --durations=10 ======》獲取最慢的10個用例的執行耗時

 

用例執行順序控制

pytest 用例執行順序默認是按字母順序去執行,要控制執行順序,需要安裝插件 pytest-ordering:pip install pytest-ordering

在測試方法上加上裝飾器:

@pytest.mark.last 最后一個執行

@pytest.mark.run(order=n) n=1則是第一個執行

 

Mark

標簽的使用方法:

注冊標簽名 / 內置標簽---> 在測試用例  / 測試類 / 模塊文件 前面加上 @pytest.mark.標簽名

注冊方法:

1. 在conftest.py 文件中添加代碼

# 單個標簽文件內容
def pytest_configure(config):
    config.addinivalue_line("markers", "demo:demo標簽名稱")

# 多個標簽文件內容
def pytest_configure(config):
    marker_list = ["p0:p0級別用例", "p1:p1級別用例", "p2:p2級別用例"] # 標簽名稱
    for markers in marker_list:
        config.addinivalue_line("markers", markers)

2. 項目中添加pytest.ini配置文件

[pytest]
markers = 
    p0:p0級別用例
    p1:p1級別用例
    p2:p2級別用例

使用方法

import pytest


@pytest.mark.p0
def test_mark01():
    print("函數級別的mark_p0")

@pytest.mark.p1
def test_mark02():
    print("函數級別的mark_p1")

@pytest.mark.P2
class TestDemo:
    def test_mark03(self):
        print("mark_p2")

    def test_mark04(self):
        print("mark_p2")

運行方式:

1. 命令行運行

pytest -m "p0 and p1"

2. 文件運行

pytest.main(["-m", "P0", "--html=report.html"])

 

內置標簽:

參數化:@pytest.mark.parametrize(argnames, argvalues)

無條件跳過用例:@pytest.mark.skip(reason="xxx")

有條件跳過用例:@pytest.mark.skipif(version < 0.3, reason = "not supported until 0.3")

預測執行失敗進行提示標記:@pytest.mark.xfail(version < 0.3, reason = "not supported until 0.3"),運行結果為X(通過xpassed,失敗xfailed)

# 參數化
import hashlib
@pytest.mark.parametrize(
"x", list(range(10))) def test_somethins(x): time.sleep(1) @pytest.mark.parametrize("passwd",["123456", "abcdefgfs", "as52345fasdf4"]) def test_passwd_length(passwd): assert len(passwd) >= 8 @pytest.mark.parametrize('user, passwd',[('jack', 'abcdefgh'),('tom', 'a123456a')]) def test_passwd_md5(user, passwd): db = { 'jack': 'e8dc4081b13434b45189a720b77b6818', 'tom': '1702a132e769a623c1adb78353fc9503' } assert hashlib.md5(passwd.encode()).hexdigest() == db[user] # 如果覺得每組測試的默認參數顯示不清晰,可以使用 pytest.param 的 id 參數進行自定義 @pytest.mark.parametrize("user, passwd", [pytest.param("jack", "abcdefgh", id = "User<Jack>"), pytest.param("tom", "a123456a", id = "User<Tom>")]) def test_passwd_md5_id(user, passwd): db = { 'jack': 'e8dc4081b13434b45189a720b77b6818', 'tom': '1702a132e769a623c1adb78353fc9503' } assert hashlib.md5(passwd.encode()).hexdigest() == db[user]

 

Fixture

固件:是一些函數,pytest會在執行函數之前或者之后加載運行它們,相當於預處理和后處理。

fixture的目的是提供一個固定基線,在該基線上測試可以可靠地、重復的執行。

  1. 名稱:默認為定義時的函數名,可以通過 @pytest.fixture(name="demo") 給fixture重命名
  2. 定義:在固件函數定義前加上@pytest.fixture();fixture是有返回值的,沒return則返回None
  3. 使用:作為參數、使用usefixtures、自動執行(定義時指定autouse參數)
    • def test_demo(fixture_func_name)
    • @pytest.mark.usefixtures("fixture_func_name1", "fixture_func_name2") 標記函數或者類
  4. 預處理和后處理:用yield關鍵詞,yield之前的代碼是預處理,之后的是后處理
  5. 作用域:通過scope參數控制作用域
    • function:函數級,每個測試函數都會執行一次(默認)
    • class:類級別,每個測試類執行一次,所有方法都共享這個fixture
    • module:模塊級別,每個模塊.py執行一次,模塊中所有測試函數、類方法 或者 其他fixture 都共享這個fixture
    • session:會話級別,每次會話只執行一次,一次會話中所有的函數、方法都共享這個fixture
  6. 集中管理:使用文件conftest.py 集中管理,在不同層級定義,作用於在其所在的目錄和子目錄,pytest會自動調用

scope、yield、auto的使用

# scope、yield、auto使用
@pytest.fixture(scope = "function", autouse=True)
def function_scope():
    pass

@pytest.fixture(scope = "module", autouse=True)
def module_scope():
    pass

@pytest.fixture(scope = "session")
def session_scope():
    pass

@pytest.fixture(scope = "class", autouse=True)
def class_scope():
    pass

import time

DATE_FORMAT = '%Y-%m-%d %H:%M:%S'

@pytest.fixture(scope='session', autouse=True)
def timer_session_scope():
    start = time.time()
    print('\nsession start: {}'.format(time.strftime(DATE_FORMAT, time.localtime(start))))

    yield

    finished = time.time()
    print('\nsession finished: {}'.format(time.strftime(DATE_FORMAT, time.localtime(finished))))
    print('session Total time cost: {:.3f}s'.format(finished - start))

def test_1():
    time.sleep(1)

def test_2():
    time.sleep(2)

'''
執行命令:pytest --setup-show -s
固件執行結果: test_pytest_study.py session start: 2020-04-16 17:29:02 SETUP S timer_session_scope SETUP M module_scope SETUP C class_scope SETUP F function_scope test_pytest_study.py::test_3 (fixtures used: class_scope, function_scope, module_scope, timer_session_scope). TEARDOWN F function_scope TEARDOWN C class_scope SETUP C class_scope SETUP F function_scope test_pytest_study.py::test_4 (fixtures used: class_scope, function_scope, module_scope, timer_session_scope). TEARDOWN F function_scope TEARDOWN C class_scope TEARDOWN M module_scope session finished: 2020-04-16 17:29:05 session Total time cost: 3.087s TEARDOWN S timer_session_scope '''

使用文件conftest.py 集中管理

# conftest.py
# conding=utf-8
import pytest

@pytest.fixture()
def postcode():
    print("執行postcode fixture")
    return "010"
# test_demo.py
# coding=utf-8
import pytest

class TestDemo():

    def test_postcode(self, postcode):
        assert postcode == "010"

if __name__=="__main__":
    pytest.main(["--setup-show", "-s", "test_demo.py"])
python test_demo.py
執行過程:
test_demo.py 執行postcode fixture

        SETUP    F postcode
        test_demo.py::TestDemo::test_postcode (fixtures used: postcode).
        TEARDOWN F postcode
# 如果整個文件都用一個fixture,可以用pytestmark標記
pytestmark = pytest.mark.usefixtures("login")

 

fixture參數化

固件參數化需要使用pytest內置的固件request,並通過 request.param 獲取參數。

# test_demo.py
@pytest.fixture(params=[
    ("user1", "passwd1"),
    ("user2", "passwd2")
    ])
def param(request):
    return request.param

@pytest.fixture(autouse=True)
def login(param):
    print("\n登錄成功 %s %s" %param)
    yield
    print("\n退出成功 %s %s" %param)

def test_api():
    assert 1 == 1

'''
pytest -s -v test_demo.py
運行結果:
test_demo.py::test_api[param0]
登錄成功 user1 passwd1
PASSED
退出成功 user1 passwd1

test_demo.py::test_api[param1]
登錄成功 user2 passwd2
PASSED
退出成功 user2 passwd2
'''

 

assert

assert "h" in "hello"
assert 3==4
assert 3!=4
assert f()==4
assert 5>6
assert not xx
assert {"0", "1", "2"} == {"0", "1", "2"}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM