1. 概述
tox是通用的虛擬環(huán)境管理和測(cè)試命令行工具。tox能夠讓我們?cè)谕粋€(gè)Host上自定義出多套相互獨(dú)立且隔離的python環(huán)境(tox是openstack社區(qū)最基本的測(cè)試工具舌劳,比如python程序的兼容性卿城、UT等)菊匿。它的目標(biāo)是提供最先進(jìn)的自動(dòng)化打包狭莱、測(cè)試和發(fā)布功能奥裸。
- 作為持續(xù)集成服務(wù)器的前端就轧,大大減少測(cè)試工作所需時(shí)間证杭;
- 檢查軟件包能否在不同的python版本或解釋器下正常安裝;
- 在不同的環(huán)境中運(yùn)行測(cè)試代碼妒御。
2. 使用介紹
2.1. 安裝
$ pip install tox
2.2. 創(chuàng)建tox測(cè)試項(xiàng)目
項(xiàng)目目錄
$ tree .
.
├── requirements.txt
├── src
│ ├── app.py
│ ├── app.pyc
│ ├── __init__.py
│ └── __init__.pyc
├── tests
│ ├── __init__.py
│ ├── __init__.pyc
│ └── test_app.py
└── tox.ini
2 directories, 9 files
src/app.py
from math import fabs, ceil
def math_fabs(x):
return fabs(x)
def math_ceil(x):
return ceil(x)
if __name__ == '__main__':
print math_fabs(-1.2)
print math_ceil(-2.3)
tests/test_app.py
# -*- coding:utf-8 -*-
import pytest
from src.app import math_fabs, math_ceil
def test_math_fabs():
assert math_fabs(-1.2) == 1.2
assert math_fabs(0) == 0
assert math_fabs(2.4) == 2.4
def test_math_ceil():
assert math_ceil(-1.2) == -1
assert math_ceil(0) == 0
assert math_ceil(2.4) == 3
tox.ini
[tox]
envlist = py27
skipsdist = True
indexserver =
default = https://pypi.doubanio.com/simple
[testenv]
install_command = pip install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com {opts} {packages}
deps =
-rrequirements.txt
commands = coverage erase
py.test --cov={toxinidir}/src -sx tests
coverage html
setenv =
PYTHONPATH = {toxinidir}/py27
[testenv:dev]
deps = pytest
commands = {posargs:py.test}
- skipsdist解愤,tox默認(rèn)會(huì)使用sdist構(gòu)建包,對(duì)于測(cè)試來(lái)說(shuō)沒(méi)有必要乎莉,而且構(gòu)建還會(huì)要求存在README送讲、setup.py等文件,并且保證setup.py的格式符合要求等惋啃,所以跳過(guò)此步
- [testenv]哼鬓,默認(rèn)的集成方案
- [testenv:dev],非默認(rèn)的集成方案边灭,需要使用tox -e dev才能invoke
- deps = pytest异希,集成需要的依賴
- commands = {posargs:py.test},可以把調(diào)用的命令的參數(shù)通過(guò)posargs傳給tox來(lái)使用
- commands = coverage erase
py.test --cov={toxinidir}/src -sx tests
coverage html
相當(dāng)于執(zhí)行三步 - install_command = pip install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com {opts} {packages}
安裝需要的依賴包绒瘦,替換原有的安裝命令 - setenv =
PYTHONPATH = {toxinidir}/py27
設(shè)置python程序運(yùn)行的環(huán)境變量
requirements.txt
pytest==3.0.0
mock==2.0.0
coverage==4.1
pytest-cov==2.0
pytest-randomly==1.0.0
pytest-mock==1.2
2.3. 測(cè)試執(zhí)行
$ tox
py27 installed: bumpversion==0.5.3,click==6.7,coverage==4.1,Flask==0.12.2,funcsigs==1.0.2,itsdangerous==0.24,Jinja2==2.10,MarkupSafe==1.0,mock==2.0.0,pbr==4.0.1,py==1.5.3,pytest==3.0.0,pytest-cov==2.0.0,pytest-mock==1.2,pytest-randomly==1.0.0,six==1.11.0,Werkzeug==0.14.1
py27 runtests: PYTHONHASHSEED='1191855235'
py27 runtests: commands[0] | coverage erase
py27 runtests: commands[1] | py.test --cov=/home/kevin/learn/python-web/tox/src -sx tests
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.12, pytest-3.0.0, py-1.5.3, pluggy-0.3.1
Using --randomly-seed=1522848244
rootdir: /home/kevin/learn/python-web/tox, inifile:
plugins: randomly-1.0.0, mock-1.2, cov-2.0.0
collected 2 items
tests/test_app.py ..
---------------------------------------------------------------- coverage: platform linux2, python 2.7.12-final-0 ----------------------------------------------------------------
Name Stmts Miss Cover
-------------------------------------
src/__init__.py 0 0 100%
src/app.py 8 2 75%
-------------------------------------
TOTAL 8 2 75%
============================================================================= pytest-warning summary =============================================================================
WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead.
================================================================== 2 passed, 1 pytest-warnings in 0.02 seconds ===================================================================
py27 runtests: commands[2] | coverage html
____________________________________________________________________________________ summary _____________________________________________________________________________________
py27: commands succeeded
congratulations :)
- 備注:
在tox.ini配置[testenv:dev]宠互,可以執(zhí)行tox -e dev,完成指定虛擬環(huán)境dev的test椭坚。
$ tox -e dev
dev recreate: /home/kevin/learn/python-web/tox/.tox/dev
dev installdeps: pytest
dev installed: attrs==17.4.0,funcsigs==1.0.2,more-itertools==4.1.0,pluggy==0.6.0,py==1.5.3,pytest==3.5.0,six==1.11.0
dev runtests: PYTHONHASHSEED='487614162'
dev runtests: commands[0] | py.test
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.12, pytest-3.5.0, py-1.5.3, pluggy-0.6.0
rootdir: /home/kevin/learn/python-web/tox, inifile:
collected 2 items
tests/test_app.py .. [100%]
============================================================================ 2 passed in 0.01 seconds ============================================================================
____________________________________________________________________________________ summary _____________________________________________________________________________________
dev: commands succeeded
congratulations :)
3. 參考
tox official documents
https://tox.readthedocs.io/en/latest/examples.htmlpython tox
https://blog.csdn.net/zhaole524/article/details/77844902tox github
https://github.com/tox-dev/tox