官方文檔 : http://tox.readthedocs.io/en/latest/example/basic.html
參考文檔 : http://www.tuicool.com/articles/UnQbyyv
tox是什么
tox是通用的虛擬環(huán)境管理和測試命令行工具吊履,旨在使Python測試標(biāo)準(zhǔn)化递览、自動化铐姚,可以使用它:
- 用不同的Python版本和解釋器檢查你的軟件包是否正確安裝
- 在不同的虛擬環(huán)境中運行測試孟辑,配置你選擇的測試工具
- 作為持續(xù)集成服務(wù)器的前端徐块,大大減少了樣板和合并CI和基于shell的測試
安裝
$ pip install tox
在需要運行的python項目目錄中建立tox.ini
文件此洲,在tox.ini
文件中配置你的項目的基本信息和你想要的測試環(huán)境畅厢。
你還可以通過運行tox-quickstart
來自動生成一個tox.ini
文件。
在Python2.6和Python2.7的環(huán)境來安裝和測試你的項目元莫,只需運行命令:
tox
這將打包源碼(sdist-package)到當(dāng)前的項目赖阻,創(chuàng)建虛擬環(huán)境,并在其中運行指定的命令tox -e py26
踱蠢。
首次運行需要安裝依賴創(chuàng)建環(huán)境火欧,運行時間較長,第二次開始,運行會很快布隔。
詳細(xì)配置示例:
[tox]
minversion = 1.6
#最低tox版本
skipsdist = True
#跳過本地軟件包安裝到virtualenv中步驟
envlist = py27,pep8,com
# envlist 表示 tox 中配置的環(huán)境都有哪些
[testenv]
# testenv 是默認(rèn)配置离陶,如果某個環(huán)境自身的 section 中沒有定義這些配置稼虎, 那么就從這個 section 中讀取
setenv = VIRTUAL_ENV={envdir}
PYTHONHASHSEED=0
PYCURL_SSL_LIBRARY=openssl
# setenv 列出了虛擬機環(huán)境中生效的環(huán)境變量衅檀,一些配色方案和單元測試標(biāo)志
usedevelop = True
# usedevelop 表示安裝 virtualenv 時, 項目自身是采用開發(fā)模式安裝的霎俩, 所以不會拷貝代碼到 virtualenv 目錄中哀军, 只是做個鏈接
install_command = pip install {opts} {packages}
# 表示構(gòu)建環(huán)境的時候要執(zhí)行的命令,一般是使用 pip 安裝
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
# deps 指定構(gòu)建環(huán)境時需要安裝的第三方依賴包
# 每個虛擬環(huán)境創(chuàng)建的時候打却, 會通過 pip install -r requirements.txt 和 pip install -r test-requirements.txt 安裝依賴包到虛擬環(huán)境
# 一般的項目會直接安裝 requirements 和 test-requirements 兩個文件中的所有依賴包
commands = ostestr {posargs}
# commands 表示構(gòu)建好 virtualenv 之后要執(zhí)行的命令
# 這里調(diào)用了 ostestr 指令來調(diào)用 testrepository 執(zhí)行單元測試用例
# {posargs} 參數(shù)就是可以將 tox 指令的參數(shù)傳遞給 ostestr
whitelist_externals = bash
passenv = http_proxy HTTP_PROXY https_proxy HTTPS_PROXY no_proxy NO_PROXY
[testenv:py34]
commands =
python -m testtools.run
# 這個 section 是為 py34 環(huán)境定制某些配置的杉适,沒有定制的配置,將會從 [testenv] 讀取
[testenv:pep8]
commands =
flake8 {posargs} ./egis egis/common
# Check that .po and .pot files are valid:
bash -c "find egis -type f -regex '.*\.pot?' -print0|xargs -0 -n 1 msgfmt --check-format -o /dev/null"
{toxinidir}/tools/config/check_uptodate.sh
{toxinidir}/tools/check_exec.py {toxinidir}/egis
# 執(zhí)行 tox -e pep8 進(jìn)行代碼檢查, 實際上是執(zhí)行了上述指令來進(jìn)行代碼的語法規(guī)范檢查
[tox:jenkins]
downloadcache = ~/cache/pip
# 定義了 CI server jenkins 的集成配置
# 指定了 pip 的下載 cache 目錄柳击,提高構(gòu)建虛擬環(huán)境的速度
[testenv:cover]
# Also do not run test_coverage_ext tests while gathering coverage as those
# tests conflict with coverage.
commands =
python setup.py testr --coverage \
--testr-args='^(?!.*test.*coverage).*$'
# 定義一個 cover 虛擬環(huán)境猿推,使單元測試的時候,自動應(yīng)用 coverage
...
其他常用配置:
setenv = VIRTUAL_ENV={envdir}
PYTHONHASHSEED=0
#設(shè)置環(huán)境變量
usedevelop = True
#項目應(yīng)該使用setup.py開發(fā)安裝到環(huán)境中捌肴,而不是使用setup.py install來構(gòu)建和安裝其源代碼蹬叭。
依賴requirements.txt文件
將requirements.txt文件添加到deps的三種方式:
deps = -r requirements.txt
deps = -c constraints.txt
deps = -r requirements.txt -c constraints.txt
進(jìn)行測試
所有的令都是在{toxinidir}(tox.ini所在的目錄)作為當(dāng)前工作目錄執(zhí)行的。
在當(dāng)前目錄執(zhí)行:
$ tox [-e py27] [subpath]
subpath以Python模塊形式用"."一級一級連接