【问题标题】:pytest and codecov: no coverage report foundpytest 和 codecov:未找到覆盖率报告
【发布时间】:2019-10-31 00:09:58
【问题描述】:

我基于千篇一律的模板创建了一个全新的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 ????我想知道我做错了什么?

【问题讨论】:

标签: python unit-testing code-coverage pytest travis-ci


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 非常感谢您的建议!我添加了pip install coverage 并重命名了test file,但不幸的是,这两种解决方案都没有work...还有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    相关资源
    最近更新 更多