pytest和codecov: 没有发现覆盖率报告

4
我创建了一个基于cookie-cutter模板的全新Python仓库,其存储在Github上。现在我正在尝试使用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

我发现codecov在travis中似乎没有通过测试。此外,在travis上,它显示出Error: No coverage report found。我想知道我做错了什么?


1
我认为问题之一是pytest无法正确定位您的测试文件;默认情况下,它需要具有形式test_*.py*_test.py,如果我没有弄错的话。 - Anne
2个回答

4

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

注意:您需要代码覆盖率的目录名称必须在项目目录内。

1
看起来你的安装中缺少coverage。虽然在脚本中有它,但可能没有运行。尝试在你的travis.yml文件中添加pip install coverage。还可以尝试这个: codecov

非常感谢您的建议!我已经添加了 pip install coverage 并且已经重命名了测试文件,但不幸的是,这两个解决方案都没有起作用(https://travis-ci.org/neuropsychology/NeuroKit/jobs/605461342#L336)...还有其他建议吗? - Dominique Makowski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接