运行 通过 Travis-CI 覆盖测试目录

Run coverage on tests directory via Travis-CI

我无法在 Python 项目的测试目录中 运行 coverage.py。我有一个测试目录,其中包含一个 __init__.py 文件和一些 test_*.py 文件,我在每个测试文件的 __main__ 块中定义了我想要 运行 的测试。

测试目录与 .travis.yml 文件位于同一文件夹中,如下所示:

当我 运行 单个文件作为 coverage run tests/test_errors.py 时,script 块起作用,但每当我尝试 运行 中的所有文件时它不起作用的目录。

我尝试了以下解决方案:

每当我 运行 单个文件的覆盖率时,codecov 输出:

这就是我要找的东西,但是在测试测试目录中包含的多个 Python 文件时。因为如您所见,那是 coverage run tests/test_investpy.py 不是整个 `tests/' 目录时的 codecov 输出。

欢迎任何帮助或信息!提前致谢!

(注意:有关项目结构的更多信息,您可以在 https://github.com/alvarob96/investpy 中查看项目,因为它是一个开源项目)

经过更多的研究和测试,我发现 pytest-cov documentation has an appendix on generating multiple coverage reports for a single test run: https://pytest-cov.readthedocs.io/en/latest/reporting.html

要执行 tests/ 中包含的每个测试并获得 codecov, the script line inside .travis.yml should be: pytest --cov-report term --cov=investpy tests/, via pytest 中的代码覆盖率结果,您可以为指定 tests/ 目录的项目生成终端覆盖率报告。

所以 .travis.yml 当 运行 在同一个项目上进行多个测试以合并代码覆盖率结果时应该是:

因此,运行 Travis-CI 上的覆盖率测试所需的包是:

  • pip install pytest==4.5.0
  • pip install codecov==2.0.15
  • pip install pytest-cov==2.7.1

因为我已经解决了,所以如果你有同样的问题,请随时问我任何相关的问题,无论如何我都可以帮助你!如果您觉得这个答案有用,请给这个答案评分!