当前位置: 首页 > 知识库问答 >
问题:

在测试pytest插件时如何获得覆盖率报告?

柳星晖
2023-03-14

我正在更新一个继承的存储库,它的测试覆盖率很差。回购本身是一个pytest插件。我已经改变了回购使用tox随着pytest-cov,并转换原始测试使用pytester作为测试插件时pytest留档建议。

测试和毒物构建等。工作得很好。然而,覆盖是报告错误的错失,例如类定义、导入等。这是因为代码本身作为pytest实例化的一部分被导入,直到测试真正开始才被“覆盖”。

我已经阅读了pytest文档、pytest cov和coverage文档以及tox文档,并尝试了几种配置,但都没有效果。我已经用尽了我的谷歌关键词组合池,这可能会让我找到一个好的解决方案。

pkg_root/
    .tox/
        py3/
            lib/
                python3.7/
                    site-pacakges/
                        plugin_module/
                            supporting_module.py
                            plugin.py
                            some_data.dat
    plugin_module/
        supporting_module.py
        plugin.py
        some_data.dat
    tests/
        conftest.py
        test_my_plugin.py
    tox.ini
    setup.py
    

一些相关的评论片段:

[pytest]
addopts = --cov={envsitepackagesdir}/plugin_module --cov-report=html
testpaths = tests

此配置给我一个错误,即没有收集数据;在这种情况下没有创建htmlcov。

pytest_plugins = ['pytester']  # Entire contents of file!
def test_a_thing(testdir):
    testdir.makepyfile(
        """
            def test_that_fixture(my_fixture):
                assert my_fixture.foo == 'bar'
        """
    )
    result = testdir.runpytest()
    result.assert_outcomes(passed=1)

如何才能得到准确的报告?有没有办法推迟插件加载,直到pytester测试要求它?

共有2个答案

红鸿运
2023-03-14

您无需使用pytest-cov即可实现所需。

❯ coverage run --source=<package> --module pytest --verbose <test-files-dirs> && coverage report --show-missing
❯ coverage run --source=<package> -m pytest -v <test-files-dirs> && coverage report -m
❯ coverage run --source=plugin_module -m pytest -v tests && coverage report -m
======================= test session starts ========================
platform darwin -- Python 3.9.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /Users/johndoe/.local/share/virtualenvs/plugin_module--WYTJL20/bin/python
cachedir: .pytest_cache
rootdir: /Users/johndoe/projects/plugin_module, configfile: pytest.ini
collected 1 items

tests/test_my_plugin.py::test_my_plugin PASSED               [100%]

======================== 1 passed in 0.04s =========================
Name                            Stmts   Miss  Cover   Missing
-------------------------------------------------------------
plugin_module/supporting_module.py  4      0   100%
plugin_module/plugin.py             6      0   100%
-------------------------------------------------------------
TOTAL                              21      0   100%

要获得更好的输出,您可以使用:

❯ coverage html && open htmlcov/index.html
❯ coverage -h
❯ pytest -h

运行Python程序并测量代码执行情况。

-m-module——显示每个模块中未执行的语句的行号。

--source=SRC1,SRC2,--要度量的代码包或目录列表。

Report--报告模块的覆盖率统计信息。

-m--显示缺失的--显示每个模块中未执行的语句的行号。

创建超文本标记语言报表。

-v,--详细--增加详细程度。

傅星光
2023-03-14

使用coverage来运行pytest,而不是使用pytest cov插件:

coverage run -m pytest ....

这样,覆盖将在pytest之前开始。

 类似资料:
  • 当使用 生成覆盖率报告后,我的测试在代码覆盖率报告中显示为100%覆盖,如下所示 我尝试将测试移动到它自己的文件夹中,但仍然会得到相同的结果 实际的单元测试不应该出现在覆盖率报告中,并且扭曲了我的覆盖率。它应该只显示实际程序的覆盖范围。

  • v2.0 Codecov报表 , 从2017-12-29后开始统计 测试覆盖率为 : , 可以从 https://codecov.io/gh/apache/dubbo 页面得到覆盖率报表 v1.0 基于 2.0.12 版本,统计于 2012-02-03

  • 我正在用pytest--cov测试我的代码,但是我的一个模块得到了0%的覆盖率。 该模块有一个类声明: 该测试执行以下操作: 测试覆盖率为0%-我做错了什么?

  • 我很难设置我的项目,以便SonarQube报告每个测试的测试覆盖率。 在声纳扫描仪的分析过程中,我只看到 在读取了JaCoCo执行数据之后。 这工作的要求是什么?显示每个测试覆盖范围的最小示例的外观如何。 我当前的环境如下所示: 声纳奎比 6.4 索纳贾瓦 4.12.0 声纳扫描仪 3.0.3 日食霓虹灯 埃克莱艾玛 3.0.0/雅可 0.7.9 我的测试项目如下所示: 如您所见,每个测试都存在执

  • 我正在tomcat上运行我的Web应用程序,并且在单独的项目中,我有我的测试用例,它调用了我的应用程序的所有API,现在我想检查代码覆盖率,在自动测试套件的api调用期间覆盖。 我遵循的步骤: 哈可可依赖在我的网络应用程序的pom.xml。 已下载.jar 在 catalina.sh 导出JAVA_OPTS中设置JAVA_OPTS=“$JAVA_OPTS - javaagent:/tmp/jars

  • [在这里输入图像描述][1][在这里输入图像描述][2]我知道很多人都有类似的问题。我展示了很多答案,尝试了在声纳网站上给出的示例代码。那个样品工作得很好。我还显示以下链接 maven 3.0.5 请帮我解决这个问题。 下面是父模块pom文件 我真的觉得这是jacoco或Sonarqube的bug。可能它不兼容Java8之类的东西。我几乎什么都试过了。sonar java插件2.5.1不推荐使用许