框架(Framework)是整個或部分系統的可重用設計,
框架是用來解決代碼的組織及運行控制問題的。html
在咱們編寫自動化腳本的時候,常常須要讀取配置文件,讀取數據文件,發送請求,記錄日誌,鏈接並對比數據庫數據。每一個腳本里都重寫一遍各類方法不只工做量大並且易錯。因此咱們須要把公共的方法提取出來,封裝成單獨的模塊,放到公用方法包裏。另外配置文件,數據文件,日誌等咱們也須要分類存到不一樣的文件夾下。這種對公共方法的封裝及對腳本及配置文件怎麼組織的設計就叫作框架。python
同時,通常框架除了完成對代碼及配置文件的組織以外還要提供運行的控制功能。好比批量執行,分模塊執行,生成報告,異常處理等等。web
總結爲如下3點:數據庫
一個完整的測試腳本(用例)通常包含如下幾個步驟:session
而測試框架通常還要完成用例加載,批量執行,異常控制,結果輸出等功能。基礎的測試框架通常只提供執行控制方面的功能。多線程
項目 | unittest | nose | pytest | robot framework |
---|---|---|---|---|
用例編寫 | 繼承unittest.TestCase類須要組織各類testSuite斷言種類繁多 | test開頭的方法便可 | test開頭的方法即 | 可 robot格式,文本文件 |
執行器 | 本身寫run_all_tests+discover+CommandParser+... | nosetests ... | py.test ... | pybot ... |
用例發現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使得函數將默認執行 | [Setup] ...[Teardown] ... |
用例標籤tags | 藉助unittest.skip()+comandParser實現 | attrib標籤from nose.plugins.attrib import attr@attr(speed='slow')def test_big_download(): pass$ nosetests -a speed=slow | @pytest.mark.webtest自定義一個mark,以下,而後 py.test -v -m webtest 只運行標記了webtest的函數, py.test -v -m "not webtest" 來運行未標記webtest的 | [Tags] test level1pybot -i/--include tagName C:\TF-Testpybot -e/--exculde level1 *.robot排除 |
超時機制Timeout | 本身實現 | 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 |
參數化 | 結合ddt使用 | 結合ddt使用 | @pytest.mark.parametrize("a,b,expected", testdata)def test_timedistance_v0(a, b, expected):diff = a - bassert diff == expected | [Template] 1 2 3 |
報告 | HTMLTestRunner | pip install nose-htmloutput--with-html --html-file= | pip install -U pytest-htmlpy.test --html=./report.html | 支持,默認自動生成 |
日誌log | 本身實現 | --nologcapture 不使用log--logging-format=FORMAT使用自定義的格式顯示日誌--logging-datefmt=FORMAT 和上面類相似,多了日期格式--logging-filter=FILTER日誌過濾,通常不多用,能夠不關注--logging-clear-handlers 也能夠不關注--logging-level=DEFAULT log的等級定義 | pytest test_add.py --resultlog=./log.txtpytest test_add.py --pastebin=all | 支持,默認自動生成 |
只列出用例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對比 | 無 | 無 | 無 | 無 |
併發 | 改造unittest使用協程併發,或使用線程池+Beautiful Report | 命令行併發 | pytest-xdist:分發到不用的cpu或機器上 | 命令行併發 |
xml報告 | 無 | --with-xunit | --xunit-file=... /pytest+Allure | --junit-xml= |
Selenium支持 | 無 | 無 | pytest-selenium | robotframework-seleniumlibraryrobotframwork-selenium2library |
總結:整體來講,unittest比較基礎,二次開發方便,適合高手使用;pytest/nose更加方便快捷,效率更高,適合小白及追求效率的公司;robot framework因爲有界面及美觀的報告,易用性更好,靈活性及可定製性略差。併發
unittest框架
上海一悠悠: selenium+python自動化91-unittest多線程生成報告(BeautifulReport)單元測試
pytest