python - 如果从未在测试中直接调用函数,是否可以制作 pytest 报告?
问题描述
例子
def main(p):
if foo_a(p):
return False
return p**2
def foo_a(p):
return p % 11 == 0
现在您可以通过以下方式获得 100% 的测试覆盖率
import unittest
from script import main
class Foobar(unittest.TestCase):
def test_main(self):
self.assertEquals(main(3), 9)
但也许有人想foo_a
成为p % 2 == 0
。
问题
分支覆盖会对此有所了解,但我也想知道一个函数是否从未被测试“直接”调用(例如main
示例中的 is),而只是间接调用(例如foo_a
示例中)。
pytest可以做到这一点吗?
解决方案
首先,一般的思路也是进行单元foo_a
测试
import unittest
from script import main, foo_a
class Foobar(unittest.TestCase):
def test_main(self):
self.assertEquals(main(3), 9)
def test_foo_a(self):
self.assertEquals(foo_a(11), True)
您可能正在寻找可以与 pytest https://pypi.org/project/pytest-cov/一起使用的https://coverage.readthedocs.io/en/coverage-4.5.1/,这个工具可以准确地向您展示测试期间调用了哪些代码行
但是我认为还有另一种方法可以检查您的问题,它称为突变测试,这里有一些可以帮助您解决问题的库
https://github.com/sixty-north/cosmic-ray
https://github.com/mutpy/mutpy
并查看基于属性的测试库,例如https://github.com/HypothesisWorks/hypothesis/tree/master/hypothesis-python
推荐阅读
- asynchronous - 计算在有和没有异步的情况下在 akka 流中完成的时间流
- android - 使用 Retrofit POST 请求获取对象数组
- javascript - discordjs message.guild.roles.size 在 serverinfo 命令中返回 null
- mysql - 带列的 MySQL 格式字符串
- jenkins - extendedEmail 中的矩阵触发模式 - Jenkins 作业 DSL
- react-native - 更改反应开发工具的端口
- python - 没有名为 setuptools.dist 的模块
- angular - `interval` 的角度类型定义
- python - Int 不可下标
- python - 如何根据另一列减去一列的值,按其他列分组?