您好,欢迎来到12图资源库!分享精神,快乐你我!我们只是素材的搬运工!!
  • 首 页
  • 当前位置:首页 > 开发 > WEB开发 >
    如何树立一个完美的 Python 项目(2)
    时间:2021-03-05 12:01 来源:网络整理 作者:网络 浏览:收藏 挑错 推荐 打印

    platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y 

    cachedir: $PYTHON_PREFIX/.pytest_cache 

    rootdir: $REGENDOC_TMPDIR 

    collected 1 item 

     

    test_sample.py F                                                     [100%] 

     

    ================================= FAILURES ================================= 

    _______________________________ test_answer ________________________________ 

     

        def test_answer(): 

    >       assert inc(3) == 5 

    E       assert 4 == 5 

    E        +  where 4 = inc(3

     

    test_sample.py:6: AssertionError 

    ========================= 1 failed in 0.12 seconds ========================= 

    我们一切的测试代码都放在 test 目录中,因此请将此目录添加到 setup.cfg :

    [tool:pytest] 

    testpaths=test 

    假设还想查看测试掩盖率。创立一个新文件 .coveragerc ,指定只前往我们的项目代码的掩盖率统计信息。比如示例的 best_practices 项目,设置如下:

    [run] 

    source = best_practices 

     

    [report] 

    exclude_lines = 

        # Have to re-enable the standard pragma 

        pragma: no cover 

     

        # Don't complain about missing debug-only code: 

        def __repr__ 

        if self\.debug 

     

        # Don't complain if tests don't hit defensive assertion code: 

        raise AssertionError 

        raise NotImplementedError 

     

        # Don't complain if non-runnable code isn't run: 

        if 0

        if __name__ == .__main__.: 

    如今,我们就可以运转测试并查看掩盖率了。

    pipenv run pytest --cov --cov-fail-under=100 

    --cov-fail-under=100 是设定项目的测试掩盖率假设小于 100% 那将认定为失败。

    pre-commit 的 Git hooks

    Git hooks 可让您在想要提交或推送时随时运转脚本。这使我们可以在每次提交/推送时,自动运转一切检测和测试。 pre-commit [9] 可轻松配置这些 hooks。

    Git hook 脚本关于在提交代码审查之前,辨认复杂成绩很有用。我们在每次提交时都将运转 hooks,以自动指出代码中的成绩,例如缺少分号、尾随空白和调试语句。经过在 code review 之前指出这些成绩,代码审查者可以专注于变更的代码内容,而不会糜费时间处置这些琐碎的样式成绩。

    (责任编辑:admin)