什么是框架?
框架(Framework)是整個(gè)或部分系統(tǒng)的可重用設(shè)計(jì)蝌箍,
框架是用來解決代碼的組織及運(yùn)行控制問題的族吻。
在我們編寫自動(dòng)化腳本的時(shí)候,經(jīng)常需要讀取配置文件姐浮,讀取數(shù)據(jù)文件谷遂,發(fā)送請(qǐng)求,記錄日志卖鲤,連接并對(duì)比數(shù)據(jù)庫數(shù)據(jù)肾扰。每個(gè)腳本里都重寫一遍各種方法不僅工作量大而且易錯(cuò)。所以我們需要把公共的方法提取出來扫尖,封裝成單獨(dú)的模塊白对,放到公用方法包里掠廓。另外配置文件换怖,數(shù)據(jù)文件,日志等我們也需要分類存到不同的文件夾下蟀瞧。這種對(duì)公共方法的封裝及對(duì)腳本及配置文件怎么組織的設(shè)計(jì)就叫做框架沉颂。
同時(shí),一般框架除了完成對(duì)代碼及配置文件的組織之外還要提供運(yùn)行的控制功能悦污。比如批量執(zhí)行铸屉,分模塊執(zhí)行,生成報(bào)告切端,異常處理等等彻坛。
總結(jié)為以下3點(diǎn):
- 封裝公共方法
- 代碼及配置文件的組織
- 執(zhí)行控制
什么是測(cè)試框架?
一個(gè)完整的測(cè)試腳本(用例)一般包含以下幾個(gè)步驟:
- 環(huán)境準(zhǔn)備或檢查
- 執(zhí)行業(yè)務(wù)操作
- 斷言結(jié)果
- 清理環(huán)境
而測(cè)試框架一般還要完成用例加載,批量執(zhí)行昌屉,異掣泼桑控制,結(jié)果輸出等功能间驮」幔基礎(chǔ)的測(cè)試框架一般只提供執(zhí)行控制方面的功能。
測(cè)試框架應(yīng)具有的特點(diǎn)
- 易用性:編寫用例竞帽,執(zhí)行用例扛施,生成報(bào)告及定位問題方便
- 健壯性:穩(wěn)定,比如timeout機(jī)制等
- 擴(kuò)展性:插件
- 靈活性:用例組織或執(zhí)行的靈活性屹篓,F(xiàn)ixture功能(不同范圍的setUp和tearDown)等
- 定制性:二次開發(fā)方便
unittest,pytest,nose,robot framework簡(jiǎn)介
- unittest: Python自帶疙渣,最基礎(chǔ)的單元測(cè)試框架
- nose: 基于unittest開發(fā),易用性好堆巧,有許多插件
- pytest: 同樣基于unittest開發(fā)昌阿,易用性好,信息更詳細(xì)恳邀,插件眾多
- robot framework:一款基于Python語言的關(guān)鍵字驅(qū)動(dòng)測(cè)試框架懦冰,有界面,功能完善谣沸,自帶報(bào)告及l(fā)og清晰美觀
項(xiàng)目 | unittest | nose | pytest | robot framework |
---|---|---|---|---|
用例編寫 | 繼承unittest.TestCase類需要組織各種testSuite斷言種類繁多 | test開頭的方法即可 | test開頭的方法即 | 可 robot格式刷钢,文本文件 |
執(zhí)行器 | 自己寫run_all_tests+discover+CommandParser+... | nosetests ... | py.test ... | pybot ... |
用例發(fā)現(xiàn)Discover | 支持 | 支持 | 支持 | 支持 |
跳過用例 | unittest.skip()unittest.skipIf()raise uniitest.SkipTest | from nose.plugins.skip import SkipTestraise SkipTest | @pytest.mark.skipif( condition)@pytest.mark.xfail | - |
Fixtures | setUp/tearDown@classmethodsetUpClass... | 支持 | @pytest.fixture(session="session", autouse=True)fixture的作用域:function、module乳附、session 内地,autouse=True使得函數(shù)將默認(rèn)執(zhí)行 | [Setup] ...[Teardown] ... |
用例標(biāo)簽tags | 借助unittest.skip()+comandParser實(shí)現(xiàn) | attrib標(biāo)簽from nose.plugins.attrib import attr@attr(speed='slow')def test_big_download(): pass$ nosetests -a speed=slow | @pytest.mark.webtest自定義一個(gè)mark,如下赋除,然后 py.test -v -m webtest 只運(yùn)行標(biāo)記了webtest的函數(shù)阱缓, py.test -v -m "not webtest" 來運(yùn)行未標(biāo)記webtest的 | [Tags] test level1pybot -i/--include tagName C:\TF-Testpybot -e/--exculde level1 *.robot排除 |
超時(shí)機(jī)制Timeout | 自己實(shí)現(xiàn) | from nose.tools import timedimport time@timed(1)def test_lean_5():time.sleep(2)pass | pip install pytest-timeout@pytest.mark.timeout(60)或 pytest --timeout=300 | [Timeout] 3 seconds |
參數(shù)化 | 結(jié)合ddt使用 | 結(jié)合ddt使用 | @pytest.mark.parametrize("a,b,expected", testdata)def test_timedistance_v0(a, b, expected):diff = a - bassert diff == expected | [Template] 1 2 3 |
報(bào)告 | HTMLTestRunner | pip install nose-htmloutput--with-html --html-file= | pip install -U pytest-htmlpy.test --html=./report.html | 支持,默認(rèn)自動(dòng)生成 |
日志log | 自己實(shí)現(xiàn) | --nologcapture 不使用log--logging-format=FORMAT使用自定義的格式顯示日志--logging-datefmt=FORMAT 和上面類類似举农,多了日期格式--logging-filter=FILTER日志過濾荆针,一般很少用,可以不關(guān)注--logging-clear-handlers 也可以不關(guān)注--logging-level=DEFAULT log的等級(jí)定義 | pytest test_add.py --resultlog=./log.txtpytest test_add.py --pastebin=all | 支持颁糟,默認(rèn)自動(dòng)生成 |
只列出用例collect-only | 無 | nosetests --collect-onlynosetests -v --with-id | --collect-only -v | - |
失敗用例重跑rerun failures | 無 | nosetests -v --failed | pip install -U pytest-rerunfailures@pytest.mark.flaky(reruns=5)py.test --rerun=3 | robot --rerunfailed |
baseline對(duì)比 | 無 | 無 | 無 | 無 |
并發(fā) | 改造unittest使用協(xié)程并發(fā)航背,或使用線程池+Beautiful Report | 命令行并發(fā) | pytest-xdist:分發(fā)到不用的cpu或機(jī)器上 | 命令行并發(fā) |
xml報(bào)告 | 無 | --with-xunit | --xunit-file=... /pytest+Allure | --junit-xml= |
Selenium支持 | 無 | 無 | pytest-selenium | robotframework-seleniumlibraryrobotframwork-selenium2library |
總結(jié):總體來說,robot framework由于有界面及美觀的報(bào)告棱貌,易用性更好玖媚,靈活性及可定制性略差;unittest比較基礎(chǔ)婚脱,二次開發(fā)方便今魔,適合高手使用勺像;pytest/nose插件多更簡(jiǎn)潔;pytest》nose》 unittest
pytest優(yōu)點(diǎn):
1.社區(qū)更活躍错森,學(xué)習(xí)資料詳細(xì)(node2不支持node全部功能)
2.pytest 支持更復(fù)雜的測(cè)試功能
3.pytest 報(bào)錯(cuò)調(diào)試更易查看