pytest是python的單測(cè)框架,使用靈活,插件豐富,以下是pytest常用的插件
pip install 就行,只有有這些插件,下面的某些命令行才生效
使用pytest.ini文件可以修改默認(rèn)規(guī)則
import pytest if __name__ == '__main__': pytest.main(["-vs", "./test_demo/test_demo1.py"])
這樣就可以運(yùn)行所有用例
pytest -vs ./test_demo/test_demo1.py
不論是主函數(shù)模式還是命令行模式都會(huì)讀取這個(gè)配置文件,該文件需要使用gbk編碼,下面是這個(gè)配置文件的例子
[pytest] # 命令行參數(shù),用空格分隔 addopts = -vs # 測(cè)試用例文件夾,可以自己配置 testpaths = ./test_demo # 配置測(cè)試搜索的模塊文件名稱 python_files = test*.py # 配置測(cè)試搜索的類名 python_classes = Test* # 配置搜索的函數(shù)名 python_functions = test
定義三個(gè)組,冒煙:smoke,用戶管理:user_manager,作業(yè)管理:worker_manager
目前有幾個(gè)用例給加個(gè)分組的裝飾器
import pytest class TestDemo: @pytest.mark.somke def test_case1(self): print("1") @pytest.mark.user_manage def test_case2(self): print("2") @pytest.mark.worker_manage def test_case3(self): print("3")
配置文件中加入分組信息
markers = smoke:冒煙測(cè)試 user_manage:用戶管理 worker_manage:作業(yè)管理
運(yùn)行
運(yùn)行多組
import pytest if __name__ == '__main__': pytest.main(["-vs", "-m smoke or usermanage"])
運(yùn)行單組
import pytest if __name__ == '__main__': pytest.main(["-vs", "-m smoke"])
無(wú)條件忽略
直接使用裝飾器@pytest.mark.skip(reason=“原因填寫”)
有條件忽略
使用裝飾器@pytest.mark.skipif(條件, 原因)
例子:
import pytest class TestDemo: age = 18 @pytest.mark.smoke def test_case1(self): print("1") @pytest.mark.usermanage @pytest.mark.skipif(age 18, "未成年") def test_case2(self): print("2") @pytest.mark.workermanage @pytest.mark.skip(reason="原因填寫") def test_case3(self): print("3")
pytest中的前后置處理
為什么需要前后置?比如執(zhí)行用例前需要做一些準(zhǔn)備工作,比如打開瀏覽器,在執(zhí)行用例后需要一些后置工作,比如關(guān)閉瀏覽器
模塊級(jí)別
在每個(gè)模塊執(zhí)行前會(huì)調(diào)用setup_module方法,在每個(gè)模塊執(zhí)行后會(huì)使用teardown_module方法。
例子:
import pytest def setup_module(): print("模塊用例前執(zhí)行") def teardown_module(): print("模塊用例后執(zhí)行") class TestDemo: def test_case1(self): print("1") def test_case2(self): print("2") def test_case3(self): print("3") class TestDemo2: def test_case4(self): print("4")
結(jié)果:
test_demo/test_demo2.py::TestDemo::test_case1 模塊用例前執(zhí)行 1 PASSED test_demo/test_demo2.py::TestDemo::test_case2 2 PASSED test_demo/test_demo2.py::TestDemo::test_case3 3 PASSED test_demo/test_demo2.py::TestDemo2::test_case4 4 PASSED模塊用例后執(zhí)行
類級(jí)別
類級(jí)別函數(shù) setup_class/teardown_class 對(duì)類有效,位于類中,在測(cè)試類中前后調(diào)用一次。
class TestDemo: def setup_class(self): print("類級(jí)別前置") def test_case1(self): print("1") def test_case2(self): print("2") def test_case3(self): print("3") def teardown_class(self): print("類級(jí)別后置")
test_demo/test_demo2.py::TestDemo::test_case1 模塊用例前執(zhí)行 類級(jí)別前置 1 PASSED test_demo/test_demo2.py::TestDemo::test_case2 2 PASSED test_demo/test_demo2.py::TestDemo::test_case3 3 PASSED類級(jí)別后置 模塊用例后執(zhí)行
方法級(jí)別
方法級(jí)別函數(shù) setup_method/teardown_method和setup/teardown對(duì)類有效,也位于類中,這兩個(gè)效果一樣,在測(cè)試類中每個(gè)測(cè)試方法前后調(diào)用一次。
class TestDemo: def setup_method(self): print("方法級(jí)別前置") def test_case1(self): print("1") def test_case2(self): print("2") def test_case3(self): print("3") def teardown_method(self): print("方法級(jí)別后置")
test_demo/test_demo3.py::TestDemo::test_case1 方法級(jí)別前置 PASSED方法級(jí)別后置 test_demo/test_demo3.py::TestDemo::test_case2 方法級(jí)別前置 PASSED方法級(jí)別后置 test_demo/test_demo3.py::TestDemo::test_case3 方法級(jí)別前置 PASSED方法級(jí)別后置
部分用例的前后置 pytest.fixture裝飾器
import pytest @pytest.fixture(scope="function", params=["1", "2", "3"], autouse=False, ids=None, name="new_name") def my_feature(request): i = request.param print("前置") yield i print("后置") class TestDemo: def test_case1(self, new_name): print(new_name) print("1")
結(jié)果
test_demo/test_demo4.py::TestDemo::test_case1[1] 前置
1
1
PASSED后置test_demo/test_demo4.py::TestDemo::test_case1[2] 前置
2
1
PASSED后置test_demo/test_demo4.py::TestDemo::test_case1[3] 前置
3
1
PASSED后置
pytest.fixture+conftest
fixture為session級(jí)別是可以跨.py模塊調(diào)用的,也就是當(dāng)我們有多個(gè).py文件的用例的時(shí)候,如果多個(gè)用例只需調(diào)用一次fixture,那就可以設(shè)置為scope=“session”,并且寫到conftest.py文件里。
conftest.py文件名稱時(shí)固定的,pytest會(huì)自動(dòng)識(shí)別該文件。放到項(xiàng)目的根目錄下就可以全局調(diào)用了,如果放到某個(gè)package下,那就在改package內(nèi)有效。
例子:
在包下創(chuàng)建conftest.py,注意,該配置只在本包生效
和之前一樣使用
結(jié)果還是和之前一樣。
pytest生成測(cè)試報(bào)告
pytest-html插件生成報(bào)告
pytest -vs --html ./report/report.html
參數(shù)化與數(shù)據(jù)驅(qū)動(dòng)
主要用的裝飾器是@pytest.mark.parametrize(argnames, argvalues)
不帶名字?jǐn)?shù)據(jù)驅(qū)動(dòng)
import pytest class TestDemo: @pytest.mark.parametrize("args",[(4399, 'AAAA'), (2012, 'BBBB')]) def test_case1(self, args): print(args)
結(jié)果:
test_demo/test_demo4.py::TestDemo::test_case1[args0] (4399, ‘AAAA')
PASSED
test_demo/test_demo4.py::TestDemo::test_case1[args1] (2012, ‘BBBB')
PASSED 帶名字的數(shù)據(jù)驅(qū)動(dòng)
import pytest class TestDemo: @pytest.mark.parametrize("arg1,arg2", [(4399, 'AAAA'), (2012, 'BBBB')]) def test_case1(self, arg1, arg2): print(arg1, arg2)
結(jié)果:
test_demo/test_demo4.py::TestDemo::test_case1[4399-AAAA] 4399 AAAA
PASSED
test_demo/test_demo4.py::TestDemo::test_case1[2012-BBBB] 2012 BBBB
PASSED
到此這篇關(guān)于python單測(cè)框架之pytest慣用法的文章就介紹到這了,更多相關(guān)python單測(cè)框架pytest內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:普洱 寧夏 林芝 盤錦 南平 漯河 海南 大同
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python單測(cè)框架之pytest常見(jiàn)用法》,本文關(guān)鍵詞 python,單測(cè),框架,之,pytest,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。