Py.Test:报告使用--cov,在报告中排除一些'def'并且不显示任何失败

时间:2013-07-17 20:16:01

标签: 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行视为缺失。并且,我意识到这是一个覆盖率报告工具,但它不应该显示任何失败的测试吗?修辞,是的,它应该。但是,我该如何表现呢。我已经阅读了文档但找不到。

1 个答案:

答案 0 :(得分:4)

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

  

要进一步控制覆盖范围,请使用coverage配置文件。

创建这样一个文件,如果你没有,并在其中:

[run]
exclude_lines =

    raise NoSuchElementException

py.test --cov-config .coveragerc [other parameters]。有关更多选项,请参阅http://nedbatchelder.com/code/coverage/config.html。请注意,如果coverage配置确实是名称--cov-config.coveragerccoverage.py的默认值)

,则您实际上并未明确添加pytest-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,它给了我:

  

姓名Stmts Miss Cover

     

主要5 0 100%

相关问题