安装

pip install tox

tox 使用

tox 包含一个tox.ini 文件,此文件可以自动,或者手工编写

  • tox.ini 文件格式
# content of: tox.ini , put in same dir as setup.py
[tox]
envlist = py27,py36
[testenv]
# install pytest in the virtualenv where commands will be executed
deps = pytest
commands =
    # NOTE: you can run any command line tool here - not just tests
    pytest
  • 自动生成的方法

参考如下:

Welcome to the tox 3.12.1 quickstart utility.
This utility will ask you a few questions and then generate a simple configuration file to help get you started using tox.
Please enter values for the following settings (just press Enter to accept a default value, if one is given in brackets).
What Python versions do you want to test against?
            [1] py36
            [2] py27, py36
            [3] (All versions) py27, py34, py35, py36, pypy, jython
            [4] Choose each one-by-one
> Enter the number of your choice [3]: 
What command should be used to test your project? Examples: - pytest
"
            - python -m unittest discover
            - python setup.py test
            - trial package.module
> Type the command to run your tests [pytest]: 
What extra dependencies do your tests have?
default dependencies are: ['pytest']
> Comma-separated list of dependencies: hashids
Finished: ./tox.ini has been created. For information on this file, see https://tox.readthedocs.io/en/latest/config.html
Execute `tox` to test your project.

效果

# tox (https://tox.readthedocs.io/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# test suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.
[tox]
envlist = py27, py34, py35, py37
[testenv]
deps =
    hashids
    pytest
commands =
    pytest

项目配置

  • 项目结构

├── README.md
├── myids
│ └── __init__.py
├── setup.py
└── tox.ini
  • 项目说明
    myids/init.py
 
from hashids import Hashids
hashids = Hashids()
def generateID():
    hashid = hashids.encode(123, 456, 789)
    print(hashid)
if __name__ == '__main__':
    generateID()

setup.py

import setuptools
with open("README.md", "r") as fh:
    long_description = fh.read()
setuptools.setup(
    name="myids",
    version="0.0.3",
    author="dalongrong",
    author_email="[email protected]",
    description="myids package",
    long_description=long_description,
    install_requires=['hashids'],
    long_description_content_type="text/markdown",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
)
 

tox.ini

# tox (https://tox.readthedocs.io/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# test suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.
[tox]
envlist = py27,py37
[testenv]
deps =
    hashids
    pytest
commands =
    pytest

使用

  • 命令
tox
  • 效果

GLOB sdist-make: /Users/dalong/mylearning/tox-projects/setup.py
py27 inst-nodeps: /Users/dalong/mylearning/tox-projects/.tox/.tmp/package/1/myids-0.0.3.zip
py27 installed: atomicwrites==1.3.0,attrs==19.1.0,configparser==3.7.4,contextlib2==0.5.5,funcsigs==1.0.2,hashids==1.2.0,importlib-metadata==
0.17,more-itertools==5.0.0,myids==0.0.3,packaging==19.0,pathlib2==2.3.3,pluggy==0.12.0,py==1.8.0,pyparsing==2.4.0,pytest==4.6.2,scandir==1.1
0.0,six==1.12.0,wcwidth==0.1.7,zipp==0.5.1
py27 run-test-pre: PYTHONHASHSEED='1001942139'
py27 run-test: commands[0] | pytest
=========================================================== test session starts ============================================================
platform darwin -- Python 2.7.15, pytest-4.6.2, py-1.8.0, pluggy-0.12.0
cachedir: .tox/py27/.pytest_cache
rootdir: /Users/dalong/mylearning/tox-projects
collected 0 items                                                                                                                          
======================================================= no tests ran in 0.01 seconds =======================================================
ERROR: InvocationError for command /Users/dalong/mylearning/tox-projects/.tox/py27/bin/pytest (exited with code 5)
py37 inst-nodeps: /Users/dalong/mylearning/tox-projects/.tox/.tmp/package/1/myids-0.0.3.zip
py37 installed: atomicwrites==1.3.0,attrs==19.1.0,hashids==1.2.0,importlib-metadata==0.17,more-itertools==7.0.0,myids==0.0.3,packaging==19.0
,pluggy==0.12.0,py==1.8.0,pyparsing==2.4.0,pytest==4.6.2,six==1.12.0,wcwidth==0.1.7,zipp==0.5.1
py37 run-test-pre: PYTHONHASHSEED='1001942139'
py37 run-test: commands[0] | pytest
=========================================================== test session starts ============================================================
platform darwin -- Python 3.7.3, pytest-4.6.2, py-1.8.0, pluggy-0.12.0
cachedir: .tox/py37/.pytest_cache
rootdir: /Users/dalong/mylearning/tox-projects
collected 0 items                                                                                                                          
======================================================= no tests ran in 0.01 seconds =======================================================
ERROR: InvocationError for command /Users/dalong/mylearning/tox-projects/.tox/py37/bin/pytest (exited with code 5)
_________________________________________________________________ summary __________________________________________________________________
ERROR: py27: commands failed
ERROR: py37: commands failed

说明

tox 工具还是很方便的,可以简化我们测试以及构建

参考资料

https://tox.readthedocs.io/en/latest/
https://github.com/tox-dev/tox

05-22 18:09