Pytest使用

安裝

pip install pytest

用例設計原則

  • 文件名 為test_*.py或*_test.py
  • test_* 為測試函數(shù)名
  • Test* 為測試類名
  • test_* 為測試方法名

用例執(zhí)行

  • <font color=red>執(zhí)行目錄下的所有文件</font>
pytest dirname/
  • 執(zhí)行某個測試文件
pytest test_mod.py
  • 執(zhí)行某個測試方法
pytest test_mod.py::TestClass::test_method
  • 按照關(guān)鍵字執(zhí)行(匹配模塊名、類名、函數(shù)名)
pytest -k "MyClass"
  • <font color=red>按照標記執(zhí)行</font>
@pytest.mark.slow
def test_slow():
    pass

@pytest.mark.fast
def test_fast():
    pass

>>>pytest -m "slow and fast"
  • 跳過測試
@pytest.mark.skip(reason="mark as skip")
def test_skip():
    pass

@pytest.mark.skipif(skip_flag=False, reason="skip determined by the flag")
def test_skip_if():
    pass

  • 第一個(N個)失敗后停止測試
pytest -x

pytest --maxfail=n
  • <font color=red>腳本調(diào)試使用</font>
pytest.main()

pytest.main(['test_mod.py', '-v'])

用例編寫

  • 斷言 assert
assert a == b
assert a != b
assert a >= b
assert b <= b
assert a in b
assert a not in b
assert a is b
assert a is not b
assert True
assert False

  • setup和teardown
import pytest

class TestClass:
    
    def setup_class(self):
        print("setup_class:類中所有用例執(zhí)行之前")

    def teardown_class(self):
        print("teardown_class:類中所有用例執(zhí)行之前")

    def setup_method(self):
        print("setup_method:  每個用例開始前執(zhí)行")

    def teardown_method(self):
        print("teardown_method:  每個用例結(jié)束后執(zhí)行")

    def setup(self):
        print("setup: 每個用例開始前執(zhí)行")

    def teardown(self):
        print("teardown: 每個用例結(jié)束后執(zhí)行")

    def test_one(self):
        print("執(zhí)行第一個用例")

    def test_two(self):
        print("執(zhí)行第二個用例")

def setup_module():
    print("setup_module:整個.py模塊只執(zhí)行一次")

def teardown_module():
    print("teardown_module:整個.py模塊只執(zhí)行一次")

def setup_function():
    print("setup_function:每個方法用例開始前都會執(zhí)行")

def teardown_function():
    print("teardown_function:每個方法用例結(jié)束前都會執(zhí)行")

  • <font color=red>參數(shù)化 pytest.mark.parametrize(argnames, args)</font>
@pytest.mark.parametrize('text', ['test1','test2','test3'])
def test_one(text):
    print(text)

@pytest.mark.parametrize(['username', 'pwd'], [('a', 1), ('b', 2), ('c', 3)])
def test_two(username, pwd):
    print(username, pwd)

Fixture

  • 基本使用
import pytest

@pytest.fixture()
def get_url():
    return "http://www.baidu.com"


def test_web(get_url):
    print(get_url)


if __name__ == '__main__':
    pytest.main()

  • 作用域
    • session: 會話級,一次測試只執(zhí)行一次,所有被找到的函數(shù)和方法都可用
    • module: 模塊級,每個模塊執(zhí)行一次,模塊內(nèi)函數(shù)和方法都可使用
    • function: 函數(shù)級,每個測試函數(shù)都會執(zhí)行一次固件
    • class: 類級別,每個測試類執(zhí)行一次,所有方法都可以使用
#./conftest.py

@pytest.fixture(scope="class")
def text():
    print("開始執(zhí)行")
    yield 'test'
    print("執(zhí)行完畢")

#./test_sample.py

@pytest.mark.usefixtures('text')
class TestClass:

    def test_one(self):
        print(text)

  • 參數(shù)化
#./conftest.py

@pytest.fixture(scope='module', params=['test1', 'test2'])
def text(request):
    print('開始執(zhí)行')
    yield request.param
    print('執(zhí)行完畢')

#./ test_sample.py

def test_one(text):
    print(text)

  • 配置文件
    • 所有的fixture放在conftest.py文件中,自動發(fā)現(xiàn),統(tǒng)一管理

生成報告

  • <font color=red>html報告、順序執(zhí)行、失敗重試</font>
pip install pytest-html           # 生成html報告
pip install pytest-ordering       # 按照順序執(zhí)行用例
pip install pytest-rerunfailures  # 失敗重試

pytest test_mod.py --html=./report.html

# pytest.mark.run(order=1)
# pytest.mark.run(order=2)

pytest test_mod.py # 按照mark標記的order順序執(zhí)行

pytest test_mod.py --reruns 3 # 失敗重試3次

  • xml報告
pytest --junitxml=report.xml
  • allure report
# install java
# install allure

pip install allure-pytest

pytest casedir/ --alluredir ./report

allure serve report
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • pytest是第三方開發(fā)的一個python測試模塊,可以輕松地編寫小型測試,而且可以擴展以支持應用程序和庫的復雜功...
    何小有閱讀 9,833評論 0 3
  • 1. 概述 pytest是一個非常成熟的全功能的Python測試框架,主要特點有以下幾點: 1、簡單靈活,容易上手...
    紅薯愛帥閱讀 263,267評論 6 81
  • 前言 pytest 是 python 的第三方單元測試框架,比自帶 unittest 更簡潔和高效,支持315種以...
    梵音11閱讀 335評論 0 0
  • pytest使用 1、pytest簡介 官檔是最好的教程 pytest是一個非常成熟的全功能的Python測試框架...
    道無虛閱讀 4,435評論 1 5
  • 使用介紹2.1. 安裝pip install pytest 2.2. 示例代碼編寫規(guī)則編寫pytest測試樣例非常...
    天才亮閱讀 238評論 0 0

友情鏈接更多精彩內(nèi)容