python 自動化測試 pytest 的使用

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

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

  2. 支持參數化html

  3. 能夠更精確的控制要測試的測試用例python

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

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

 

安裝

pip install pytest
 
  
 

 

測試用例編寫規則

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

  2. 測試類以Test開頭,而且不能帶有 init 方法分佈式

  3. 測試文件以 test_開頭函數

  4. 斷言使用基本的 assert 便可工具

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

 

運行命令:(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參數化

固件參數化須要使用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"}
相關文章
相關標籤/搜索