我有应该从生成的报告中排除的函数,显示为缺失。现在我明白了:
没有遗漏什么。它正在读取支持功能并报告它们。
例如:
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.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
,这给了我:
Name Stmts Miss Cover
main 5 0 100%
关于python - Py.Test : Report using --cov, 排除了报告中的一些 'def' 并且没有显示任何失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17709385/