python - Py.Test : Report using --cov, 排除了报告中的一些 'def' 并且没有显示任何失败

标签 python webdriver code-coverage pytest

我有应该从生成的报告中排除的函数,显示为缺失。现在我明白了:

Shot of the report

没有遗漏什么。它正在读取支持功能并报告它们。

例如:

def is_element_present(self, how, what): 
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False 
        return True 

在上面给出的示例中,--cov 将 except NoSuchElementException, e: return False 行读取为缺失。而且,我意识到这是一个覆盖率报告工具,但它不应该显示任何失败的测试吗?修辞上,是的,应该如此。但是,我该如何表明这一点。我读过文档但找不到。

最佳答案

由于 pytest-cov 插件可以获取 .coveragerc 配置,并由 pytest-cov 推荐:

For further control of coverage use a coverage config file.

如果您还没有创建这样的文件,请在其中创建:

[run]
exclude_lines =

    raise NoSuchElementException

并执行py.test --cov-config .coveragerc [其他参数]。请参阅http://nedbatchelder.com/code/coverage/config.html以获得更多选择。请注意,如果覆盖配置的名称确实是 .coveragerc (它是 coverage.pypytest-cov)

我使用以下代码测试了该解决方案:

main.py

def main():
    try:
        print 1 / 0
    except:
        raise Exception('test')

test_main.py

import unittest
import main

def test_main():
    main.main()


if __name__ == '__main__':
    testcase = unittest.FunctionTestCase(test_main)
    unittest.main()

并执行了py.test --cov main.py,这给了我:

Name Stmts Miss Cover

main 5 0 100%

关于python - Py.Test : Report using --cov, 排除了报告中的一些 'def' 并且没有显示任何失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17709385/

相关文章:

xml - 是否有将 Emma XML 报告转换为 Cobertura XML 格式的工具?

python - Enthought - matplotlib(plot() 函数的问题)

python - keras datagen.flow_from_directory 类模式分类获取标签错误

python - CSS 文件未应用于瓶中的 TPL

javascript - 如何使用 javascript 和 Selenium 双击 WebElement

java - WebDriver 隐式等待未按预期工作

rust - 为什么 kcov 计算 Rust 程序的代码覆盖统计数据不正确?

python - 如何使用Python从LaTeX表中提取数据

ruby - 检查元素是否存在

laravel - 不配置白名单,不会产生代码覆盖错误