【问题标题】:pytest-cov cover many applications at oncepytest-cov 一次涵盖许多应用程序
【发布时间】:2016-04-21 21:39:21
【问题描述】:

我用许多应用程序构建了 django 项目。现在我想为这些应用程序生成一个覆盖率报告。出于测试目的,我使用 py.test、pytest-django 和 pytest-cov。到目前为止,我只能通过在命令行中手动输入所有应用名称来生成报告:

py.test --cov-report html --cov=app1 --cov=app2 --cov=app3 --cov=app4 */tests.py

pytest-cov 有没有办法用一个简单的表达式来指定所有的应用程序?

【问题讨论】:

    标签: django pytest coverage.py pytest-django


    【解决方案1】:

    假设您使用的是 bash,您可以使用它来扩展参数:

    py.test --cov-report html --cov=app{1,2,3,4} */tests.py
    

    您还可以将这些参数添加到pytest.ini,以便在每次调用时自动传递它们。

    【讨论】:

    • 是的,这种方法解决了我的问题。真的,我认为在 pytest-cov 中存在类似 --cov=*--cov=all 的所有已安装应用程序的参数,但实际上你的解决方案对我来说已经足够了。
    【解决方案2】:

    @the-compiler 的回答对我不起作用。如果您想测试所有应用程序,可以使用以下方法:

    pytest --cov-report html --cov=. */tests.py
    

    来源:https://medium.com/@harshvb7/using-pytest-to-write-tests-in-django-6783674c55b8

    【讨论】:

    • 这个包括.venv目录
    • @freezed 您可以在setup.cfg 中指定要避免使用的文件夹,例如:norecursedirs = build docs/_build *.egg .tox *.venv *migrations* .*migrations* */migrations/*
    【解决方案3】:

    之前

    之后

    pytest.ini

    中更改这一行
    python_files = tests.py test_*.py */tests_*.py
    

    coverage run -m pytest
    

    【讨论】:

      猜你喜欢
      • 2023-02-17
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      • 2020-01-05
      • 2018-07-17
      • 2022-06-11
      • 1970-01-01
      • 2013-01-30
      相关资源
      最近更新 更多