pytest 和 codecov:未找到覆盖率报告

pytest and codecov: no coverage report found

我基于千篇一律的模板创建了一个全新的 Python repository。一切看起来都很好,所以我现在正在尝试使用 travis 和 codecov 设置测试和测试覆盖率。我是 pytest 的新手,但我正在努力做正确的事情。在网上看了之后,我最终得到了这个设置:

.travis.yml中,我添加了以下内容:

install: 
  - pip install -U tox-travis
  - pip install coverage
  - pip install codecov

script:
    - python setup.py install
    - tox
    - coverage run tests/test_foo.py

在我的 tox.ini 文件中:

[testenv]
passenv = CI TRAVIS TRAVIS_*
setenv =
    PYTHONPATH = {toxinidir}
    PIPENV_IGNORE_VIRTUALENVS=1
deps =
    pipenv
    codecov
    pytest
    {py27}: pathlib2
commands_pre = 
    pipenv install --dev --skip-lock
    codecov

我创建了一个包含以下内容的最小 tests/test_foo.py 文件(foo() 是目前包中唯一的功能)。

import pytest
import doctest
import neurokit2 as nk

if __name__ == '__main__':
    doctest.testmod()
    pytest.main()

def test_foo():
    assert nk.foo() == 4

我有 好像travis触发的codecov没有运行通过测试。此外,在 travis 上,它说 Error: No coverage report found 我想知道我做错了什么?

您的安装似乎缺少 coverage。您在脚本中有它,但它可能不是 运行。尝试在 travis.yml 文件中添加 pip install coverage。也试试这个:codecov

1) 在您的项目目录中创建 pytest.ini 文件并添加以下行

[pytest]
testpaths = tests
python_files = *.py
python_functions = test_*

2) 在项目目录中创建.coveragerc 文件并添加以下行

[report]
fail_under = 90
show_missing = True

3) 用于代码覆盖率的 pytest

pytest --verbose --color=yes --cov=Name of directory for which you need code coverage --assert=plain

注意:需要代码覆盖的目录名称必须在项目目录中