美文网首页
tox-simple-example

tox-simple-example

作者: DesertCactus | 来源:发表于2021-03-09 21:08 被阅读0次

    Preface

    What's the tox?  Here, we give the official link: 

    https://tox.readthedocs.io/en/latest/

    Context

    Preconditions: Python and Pytest have been installed on machine .

    1.Install tox

    pip install -i http://pypi.mirrors.ustc.edu.cn/simple/ tox --trusted-host pypi.mirrors.ustc.edu.cn

    2.Create one directory and  some  sub-directory with source file , test file as follow:

    directory organization

    3. Files contents:

    1)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)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))

    3)tests/test_app.py

    import pytest

    from src.app import math_ceil,math_fabs

    def test_math_fabs():

        assert math_fabs(-1.2) == 1.2

        assert math_fabs(0) == 0

        assert math_fabs(2.4) == 2.4

    4) tox.ini

    [tox]

    envlist = py38

    skipsdist = True

    indexserver =

        default = http://pypi.mirrors.ustc.edu.cn/simple

    [testenv]

    install_command = pip install -i http://pypi.mirrors.ustc.edu.cn/simple/ tox --trusted-host pypi.mirrors.ustc.edu.cn {opts} {packages}

    deps =

        -rrequirements.txt

    commands = coverage erase

              py.test --cov={toxinidir}/src -sx tests

              coverage html

    setenv =

        PYTHONPATH = {toxinidir}/py38

    [testenv:dev]

    deps = pytest

    commands = {posargs:py.test}

    5)   src/__init__.py  and tests/__init__.py  are both left empty

    4. After all files are finished, then run tox in tree directry. 

    console output

    5. we can check the directory again, maybe some new folders are created.

    htmlcov are created for web browser

    相关文章

      网友评论

          本文标题:tox-simple-example

          本文链接:https://www.haomeiwen.com/subject/uagbqltx.html