iOS自动化探索(四)自动化测试框架pytest - 安装和使用
自動(dòng)化測(cè)試框架 - pytest
pytest是Python最流行的單元測(cè)試框架之一, 幫助更便捷的編寫(xiě)測(cè)試腳本, 并支持多種功能復(fù)雜的測(cè)試場(chǎng)景, 能用來(lái)做app測(cè)試也能用作函數(shù)測(cè)試
官方文檔:?https://docs.pytest.org/en/latest/
?
pytest具有以下優(yōu)點(diǎn):
- 允許使用assert進(jìn)行斷言?
- 自動(dòng)識(shí)別測(cè)試腳本、類(lèi)、函數(shù)
- 可用于管理小型或者參數(shù)類(lèi)型的測(cè)試數(shù)據(jù)或資源
- 兼容unittest和nose測(cè)試框架
- 支持Python2.7/Python3.4+
- 豐富的插件支持,超過(guò)315個(gè)插件支持
?
pytest安裝
pip install -U pytest如果提示下面的錯(cuò)誤,說(shuō)明是pip的版本太老了, 要更新下:
Could not find a version that satisfies the requirement pytest (from versions: ) No matching distribution found for pytest更新方式:
easy_install --upgrade pip?
官方示例
準(zhǔn)備一個(gè)test_sample.py, 內(nèi)容如下:
def inc(x):return x + 1def test_answer():assert inc(3) == 5在文件所在目錄執(zhí)行:
pytest這里我們做下說(shuō)明:
pytest腳本都以test_xxx.py為文件名;
inc方法是我們定義的一個(gè)自增函數(shù),該函數(shù)將傳遞進(jìn)來(lái)的參數(shù)加1后返回;
test_answer是我們編寫(xiě)的一個(gè)測(cè)試函數(shù),其中我們使用基本的斷言語(yǔ)句assert來(lái)對(duì)結(jié)果進(jìn)行驗(yàn)證,測(cè)試函數(shù)以test_xxx作為命名
?
執(zhí)行結(jié)果如下:
============================================================ test session starts ============================================================ platform darwin -- Python 2.7.15, pytest-4.1.0, py-1.7.0, pluggy-0.8.0 rootdir: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test, inifile: collected 1 item test_sample.py F [100%]================================================================= FAILURES ================================================================== ________________________________________________________________ test_answer ________________________________________________________________def test_answer(): > assert inc(3) == 5 E assert 4 == 5 E + where 4 = inc(3)test_sample.py:5: AssertionError ========================================================= 1 failed in 0.05 seconds ========================================================== (wda_python) bash-3.2$當(dāng)執(zhí)行到assert inc(3) == 5時(shí),報(bào)錯(cuò)
?
執(zhí)行pytest會(huì)在當(dāng)前目錄和子目錄中尋找test_xx.py的測(cè)試文件,并進(jìn)入到測(cè)試文件中尋找test_xx開(kāi)頭的測(cè)試函數(shù)開(kāi)始執(zhí)行
執(zhí)行pytest -q ?test_xxx.py是執(zhí)行執(zhí)行的腳本
?
在看一個(gè)例子,測(cè)試指定錯(cuò)誤: (Assert that a certain exception is raised)
import pytestdef f():raise SystemExit(1)def test_mytest():with pytest.raises(SystemExit):f()執(zhí)行指令:
pytest -q test_sysexit.py輸出:
(wda_python) bash-3.2$ pytest -q test_sysexit.py . [100%] 1 passed in 0.04 seconds (wda_python) bash-3.2$?
如果要開(kāi)發(fā)多個(gè)測(cè)試方法,可以把方法寫(xiě)進(jìn)一個(gè)class中
class TestClass(object):def test_one(self):x = 'this'assert 'h' in xdef test_two(self):x = 'hello'assert hasattr(x, 'check')pytest能夠自動(dòng)識(shí)別類(lèi)中的測(cè)試方法, 也不用我們?nèi)?chuàng)建子類(lèi)或者實(shí)實(shí)例, 運(yùn)行結(jié)果如下:
(wda_python) bash-3.2$ pytest -q test_sample.py .F [100%] ================================================================== FAILURES ================================================================== _____________________________________________________________ TestClass.test_two _____________________________________________________________self = <test_sample.TestClass object at 0x102e151d0>def test_two(self):x = 'hello' > assert hasattr(x, 'check') E AssertionError: assert False E + where False = hasattr('hello', 'check')test_sample.py:8: AssertionError 1 failed, 1 passed in 0.08 seconds (wda_python) bash-3.2$?
除了直接在腳本路徑執(zhí)行pytest外, 還可以用以下方式
python -m pytest xxx.py?
出現(xiàn)第一個(gè)(或第N個(gè))錯(cuò)誤時(shí)停止
pytest -x # stop after first failure pytest --maxfail=2 # stop after two failures?
運(yùn)行執(zhí)行測(cè)試腳本
pytest test_mod.py?
運(yùn)行指定目錄下的所有腳本
pytest testing/?
運(yùn)行包含指定關(guān)鍵字的測(cè)試方法, 可以是文件名、類(lèi)名、測(cè)試函數(shù)名
pytest -k "MyClass and not method"?
執(zhí)行node id運(yùn)行測(cè)試腳本,每一個(gè)被收集的測(cè)試方法都會(huì)分配一個(gè)指定的id, 我們可以用一下方式運(yùn)行執(zhí)行的測(cè)試方法:
# To run a specific test within a module pytest test_mod.py::test_func # To run a test within a class pytest test_mod.py::TestClass::test_method?
日志打印的不同方式
pytest --showlocals # show local variables in tracebacks pytest -l # show local variables (shortcut)pytest --tb=auto # (default) 'long' tracebacks for the first and last# entry, but 'short' style for the other entries pytest --tb=long # exhaustive, informative traceback formatting pytest --tb=short # shorter traceback format pytest --tb=line # only one line per failure pytest --tb=native # Python standard library formatting pytest --tb=no # no traceback at all?
測(cè)試報(bào)告
pytest默認(rèn)是完整的測(cè)試報(bào)告, 我們可以加上-r標(biāo)簽顯示簡(jiǎn)短測(cè)試報(bào)告,可再搭配一下參數(shù)
Here is the full list of available characters that can be used:f - failed E - error s - skipped x - xfailed X - xpassed p - passed P - passed with output a - all except pP可以多個(gè)參數(shù)一起使用
?
Debug模式
pytest --pdb示例:
(wda_python) bash-3.2$ pytest --pdb ========================================================== test session starts =========================================================== platform darwin -- Python 2.7.15, pytest-4.1.0, py-1.7.0, pluggy-0.8.0 rootdir: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test, inifile: collected 3 items test_sample.py .F >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> traceback >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>self = <test_sample.TestClass object at 0x10e928610>def test_two(self):x = 'hello' > assert hasattr(x, 'check') E AssertionError: assert False E + where False = hasattr('hello', 'check')test_sample.py:8: AssertionError >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> > /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test/test_sample.py(8)test_two() -> assert hasattr(x, 'check') (Pdb) print x hello (Pdb) print hasattr(x,'check') False (Pdb)?
還可以指定第幾次失敗開(kāi)始進(jìn)入debug:
pytest -x --pdb # drop to PDB on first failure, then end test session pytest --pdb --maxfail=3 # drop to PDB for first three failures任何失敗的異常信息都會(huì)存儲(chǔ)在sys.last_value,sys.last_type 以及 sys_last_traceback
在debug中可以通過(guò)以下方式獲取最后報(bào)錯(cuò)的內(nèi)容
(Pdb) import sys (Pdb) sys.last_traceback.tb_lineno 1357 (Pdb) sys.last_value AssertionError(u"assert False\n + where False = hasattr('hello', 'check')",) (Pdb)?
在執(zhí)行一開(kāi)始就進(jìn)入到debug模式
pytest --trace輸入next執(zhí)行下一步, exit退出
?
腳本中設(shè)置斷點(diǎn)
import pdbpdb.set_trace()例如:
import pdbclass TestClass(object):def test_one(self):x = 'this'pdb.set_trace()assert 'h' in xdef test_two(self):x = 'hello'assert hasattr(x, 'check')?
獲取執(zhí)行最慢的n個(gè)測(cè)試步驟
pytest --durations=10 ======================================================= slowest 10 test durations ========================================================(0.00 durations hidden. Use -vv to show these durations.)但如果所有腳本的運(yùn)行時(shí)間都小于0.01s, 就不顯示了, 除非帶上-vv參數(shù)
pytest --durations=10 -vv輸出結(jié)果:
======================================================= slowest 10 test durations ======================================================== 0.00s call test_sample.py::TestClass::test_two 0.00s setup test_sysexit.py::test_mytest 0.00s setup test_sample.py::TestClass::test_two 0.00s setup test_sample.py::TestClass::test_one 0.00s teardown test_sample.py::TestClass::test_two 0.00s teardown test_sample.py::TestClass::test_one 0.00s call test_sysexit.py::test_mytest 0.00s teardown test_sysexit.py::test_mytest 0.00s call test_sample.py::TestClass::test_one =================================================== 1 failed, 2 passed in 0.06 seconds =================================================== (wda_python) bash-3.2$?
將日志保存到指定文件
pytest --resultlog=path?
Disabling plugins
To disable loading specific plugins at invocation time, use the?-p?option together with the prefix?no:.
Example: to disable loading the plugin?doctest, which is responsible for executing doctest tests from text files, invoke pytest like this:
pytest -p no:doctest?
我們也可以在pytestdemo腳本中去啟動(dòng)pytest:
import pytestpytest.main()執(zhí)行python pytestdemo.py就可以執(zhí)行pytest
main()不會(huì)拋出SystemExit的異常, 但會(huì)返回exitcode, 一共有6種exitcode
Exit code 0: All tests were collected and passed successfully Exit code 1: Tests were collected and run but some of the tests failed Exit code 2: Test execution was interrupted by the user Exit code 3: Internal error happened while executing tests Exit code 4: pytest command line usage error Exit code 5: No tests were collected我們?cè)囍由洗蛴?/p> import pytestprint pytest.main()
輸出:
(wda_python) bash-3.2$ python pytestDemo.py ========================================================== test session starts =========================================================== platform darwin -- Python 2.7.15, pytest-4.1.0, py-1.7.0, pluggy-0.8.0 rootdir: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test, inifile: collected 3 items test_sample.py .F [ 66%] test_sysexit.py . [100%]================================================================ FAILURES ================================================================ ___________________________________________________________ TestClass.test_two ___________________________________________________________self = <test_sample.TestClass object at 0x1038ba650>def test_two(self):x = 'hello' > assert hasattr(x, 'check') E AssertionError: assert False E + where False = hasattr('hello', 'check')test_sample.py:11: AssertionError =================================================== 1 failed, 2 passed in 0.05 seconds =================================================== 1 (wda_python) bash-3.2$?
我們還可以在main中傳遞參數(shù):
pytest.main(['-q','test_sample.py'])?
給pytest.main添加plugin, 如下示例在執(zhí)行的開(kāi)頭和結(jié)尾, 添加打印信息
import pytestclass MyPlugin(object):
def pytest_sessionfinish(self):
print '*** Test run reporting finishing'
def pytest_sessionstart(self):
print '*** Test run report beginning'
pytest.main(['-q','test_sample.py'], plugins=[MyPlugin()])
輸出:
(wda_python) bash-3.2$ python pytestDemo.py
*** Test run report beginning
.F [100%]*** Test run reporting finishing
================================================================ FAILURES ================================================================
___________________________________________________________ TestClass.test_two ___________________________________________________________
self = <test_sample.TestClass object at 0x1090843d0>
def test_two(self):
x = 'hello'
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_sample.py:11: AssertionError
1 failed, 1 passed in 0.05 seconds
?
轉(zhuǎn)載于:https://www.cnblogs.com/zhouxihi/p/10244320.html
與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的iOS自动化探索(四)自动化测试框架pytest - 安装和使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: maven 上传jar 包含源码
- 下一篇: 人脸识别云服务