17792550360
掃描二維碼
關注卓目鳥學苑公眾號
掃描二維碼
關注卓目鳥學苑公眾號
什么是toxtox官方文檔的第一句話 standardize testing in Python,意思就是說標準化python中的測試,那是不是很適合測試人員來使用呢,我們來看看他究竟是什么?根據(jù)官方文檔的解釋,tox是一個管理測試虛擬環(huán)境的命 ...
什么是tox tox官方文檔的第一句話 standardize testing in Python,意思就是說標準化python中的測試,那是不是很適合測試人員來使用呢,我們來看看他究竟是什么? 根據(jù)官方文檔的解釋,tox是一個管理測試虛擬環(huán)境的命令行工具,可以支持穿件隔離的python環(huán)境,在里面可以安裝不同版本的python解釋器和項目的各種依賴庫,可以進行自動化測試、打包以及持續(xù)集成。 tox能做什么 創(chuàng)建測試虛擬環(huán)境運行靜態(tài)代碼分析與測試工具自動化構建包針對 tox 構建的軟件包運行測試檢查軟件包是否能在不同的 Python 版本/解釋器中順利安裝統(tǒng)一持續(xù)集成(CI)和基于命令行的測試怎么配置tox 安裝tox 使用pip install tox安裝,在命令行執(zhí)行tox -e envname運行指定的測試環(huán)境 tox配置 tox的行為既可以通過命令行來控制也可以通過配置文件進行控制,支持有以下三種形式的配置文件 pyproject.tomltox.inisetup.cfg# tox (https://tox.readthedocs.io/) is a tool for running tests# in multiple virtualenvs. This configuration file will run the# tests suite on all supported python versions. To use it, "pip install tox"# and then run "tox" from this directory.[tox]envlist = py36skipsdist = True# 設置pip源和依賴版本indexserver = default = http://mirrors.aliyun.com/pypi/simple/[testenv]deps = pytest records pymysql jinja2 requests objectpath arrow pytest-html redisinstall_command = pip install --trusted-host mirrors.aliyun.com {opts} {packages}[testenv:dev]setenv = env = dev; 告訴tox在每個測試環(huán)境里運行pytestcommands = pytest --junitxml=junit-{envname}.xml;只運行廣告相關的測試用例[testenv:t_a]setenv = env = devcommands = pytest -v tests/ad--junitxml=junit-{envname}.xml;只運行測試環(huán)境APP相關測試用例;只運行APP相關測試用例[testenv:t_i]setenv = env = devcommands = pytest -v tests/ivwen --junitxml=junit-{envname}.xml[testenv:t1_i]setenv = env = t1commands = pytest -v tests/ivwen --junitxml=junit-{envname}.xml[testenv:pro]setenv = env = pro; 通過command line往環(huán)境變量里寫測試還是線上的標識,config根據(jù)標識從環(huán)境變量里去讀取指定文件; 或者通過插件的形式,能夠配置各個環(huán)境的文件,根據(jù)命令行參數(shù)指定把那個文件放入指定讀取目錄command = pytest[testenv:smoke][pytest]markers = smoke getaddopts = -rsxX -l --tb=short --strictxfail_strict = trueminversion = 3.0norecursedirs = .* venv src *.egg dist buildtestpaths = testspython_classes = *Test Test* *Suitjunit_family=xunit1以上配置解釋如下: [tox]節(jié)點是對tox進行配置envlist指定環(huán)境列表,多個環(huán)境用逗號隔開,比如py36,py37 skipsdist 指定tox在運行過程中跳過打包環(huán)節(jié),因為當前這個項目沒有打包的需求,所以這里設置為true,這個和自動化測試框架的設計有關。 indexserver 指定pip的安裝源 [testenv]節(jié)點是對測試環(huán)境進行配置,這個是根測試環(huán)境的配置,下面還可以對不同的測試環(huán)境進行配置,都可以繼承這個節(jié)點deps 指定項目的python依賴的第三方包 install_command 定義pip安裝命令參數(shù) [testenv:dev]這個節(jié)點是定義測試環(huán)境,繼承根環(huán)境配置setenv 設置環(huán)境變量,在項目中可以讀取環(huán)境變量,從而決定要運行哪個環(huán)境的配置,比如tox -e dev,意思就是說在測試環(huán)境運行測試用例,tox -e prod在生產(chǎn)環(huán)境運行測試用例 commands 指定pytest的運行方式,其他環(huán)境的節(jié)點配置與此相似。 [pytest]節(jié)點可以對pytest進行配置addopts addopts 指定pytest的命令行參數(shù)xfail_strict 設置預期失敗的case如果通過了,則標記為失敗minversion 指定tox的最小版本norecursedirs 指定哪些目錄不用遞歸查找測試用例testpaths 指定測試用例的搜索目錄python_classes 指定測試用例的搜索規(guī)則當然以上的配置只是tox一部分,還有很多,關注官方文檔 tox項目實戰(zhàn) 下面我們以 tox、pytest打造一個自動化測試框架 項目搭建
ad和biz是對不同業(yè)務進行的封裝,里面包括接口調(diào)用以及數(shù)據(jù)庫相關操作 common是各個業(yè)務模塊公共的部分,包括請求發(fā)送、數(shù)據(jù)庫鏈接基礎操作封裝、配置等,主要來看一下config的里的內(nèi)容: class Config: '''公共配置'''class DevConfig(Config): '''測試環(huán)境配置'''class ProdConfig(Config): '''生產(chǎn)環(huán)境配置''' # 環(huán)境切換 _MAPPING = { 'dev': DevConfig, 't1': T1Config, 'pro': ProConfig,}# 這里根據(jù)tox設置的環(huán)境變量,來決定使用哪一個環(huán)境的配置,從而實現(xiàn)不同環(huán)境環(huán)境的切換config = _MAPPING.get(os.getenv("env"), DevConfig) 運行測試用例tox -e dev 以上是執(zhí)行過程以及測試結果,會生成junit.xml格式的測試報告,當然也可以使用pytest-html或者其他測試報告,都很方便。 |
分享本篇文章給更多人:
2020-05-27
2020-02-24
2020-05-27
2022-12-05
2020-05-27
請發(fā)表評論